9

I'm trying to see if a certain python-library is installed by grepping the output of pip list. If I try this

pip list | grep -q $package, it works fine. If I try pip list | grep -qi $package, I get the following error output

pi@pibox:~ $ pip list | grep -i -q pyyaml
Traceback (most recent call last):
  File "/usr/bin/pip", line 9, in <module>
    load_entry_point('pip==1.5.6', 'console_scripts', 'pip')()
  File "/usr/lib/python2.7/dist-packages/pip/__init__.py", line 248, in main
    return command.main(cmd_args)
  File "/usr/lib/python2.7/dist-packages/pip/basecommand.py", line 156, in main
    logger.fatal('Exception:\n%s' % format_exc())
  File "/usr/lib/python2.7/dist-packages/pip/log.py", line 111, in fatal
    self.log(self.FATAL, msg, *args, **kw)
  File "/usr/lib/python2.7/dist-packages/pip/log.py", line 164, in log
    consumer.flush()
IOError: [Errno 32] Broken pipe

this seems to be an error on the python side of things, what would the grepm flag to ignore case have to do with pip's ability to send information down a pipe?

This is on a Raspberry Pi 3 running pip 1.5.6 from /usr/lib/python2.7/dist-packages (python 2.7) and grep (GNU grep) 2.20.

1

1 Answer 1

15

With the -q flag the grep program will stop immediately when the first line of data matches.

However pip may still be trying to send data into the pipe. It will receive a SIGPIPE. And that causes the error traceback.

With the -i flag it's possible that the grep process is stopping sooner (earlier match), before pip has finished writing out the results.

Normally you shouldn't use -q in a pipeline like this unless you are sure the program at the other end can handle SIGPIPE.

So pip list | grep -i $packagename will work without error.

3
  • I was trying to use -q because it just gives the exit code that I need for the script, doing it without that flag results in it working. Thanks for your help. Commented Aug 24, 2016 at 21:33
  • 4
    The exit code will be the same; you can do grep -i $packagename >/dev/null to throw the output away. Commented Aug 24, 2016 at 21:42
  • Thankyou so much. Man, that was a painful hour trying to figure WTF was going on! Commented Jun 27, 2017 at 20:15

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .