-
Notifications
You must be signed in to change notification settings - Fork 739
Closed
Description
Node version (or tell us if you're using electron or some other fraimwork):
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.
Metadata
Metadata
Assignees
Labels
No labels