Sitelet https://github.com/shelljs/shelljs/issues/900
Skip to content

grep exit status and extra newlines #900

Description

@wyardley

Node version (or tell us if you're using electron or some other framework):

v8.11.3

ShellJS version (the most recent version/Github branch you see the bug on):

0.8.2

Operating system:

Mac OS X

Description of the bug:

grep() returns 0, even if a string is not found, and the resulting string also contains a newline.

Example ShellJS command to reproduce the error:

(given a file testfile without the string asdfasdf at the beginning of a line):

const shell = require('shelljs');

console.log(shell.grep(/^asdfasdf/, 'testfile'))

Output of above:

{ [String: '\n']
  stdout: '\n',
  stderr: null,
  code: 0,
  cat: [Function: bound ],
  exec: [Function: bound ],
  grep: [Function: bound ],
  head: [Function: bound ],
  sed: [Function: bound ],
  sort: [Function: bound ],
  tail: [Function: bound ],
  to: [Function: bound ],
  toEnd: [Function: bound ],
  uniq: [Function: bound ] }

I don't believe you'll have even a newline if you grep for something that isn't found, and you'll definitely get a non-0 status code if the string isn't found.

$ grep -q ^asdfasdf testfile 
$ echo $?
1
$ grep ^asdfasdf testfile > /tmp/asdf
$ cat -v -e /tmp/asdf 
$ file /tmp/asdf 
/tmp/asdf: empty

As best I could tell, the only reliable way to detect the absence of a string to use something like:

shell.grep(/^asdfasdf/, 'testfile').stdout.trim())

Of course, at this point, it might be easier to use regexp on the file directly, but this still seems inconsistent with the behavior of the grep command.

Activity

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions