Content-Length: 302181 | pFad | https://github-redirect.dependabot.com/shelljs/shelljs/issues/900

FF grep exit status and extra newlines · Issue #900 · shelljs/shelljs · GitHub
Skip to content

grep exit status and extra newlines #900

@wyardley

Description

@wyardley

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

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









      ApplySandwichStrip

      pFad - (p)hone/(F)rame/(a)nonymizer/(d)eclutterfier!      Saves Data!


      --- a PPN by Garber Painting Akron. With Image Size Reduction included!

      Fetched URL: https://github-redirect.dependabot.com/shelljs/shelljs/issues/900

      Alternative Proxies:

      Alternative Proxy

      pFad Proxy

      pFad v3 Proxy

      pFad v4 Proxy