Using sh library with grep command

Roy Smith roy at
Sat Nov 23 16:30:47 CET 2013

In article <mailman.3088.1385218947.18130.python-list at>,
 Luca <lucafbb at> wrote:

> I'm trying to use sh ( for calling
> system grep command but it's now working as expected.
> An example:
>     import sh
>     sh.grep('abc', os.getcwd(), '-r')
> But I get the ErrorReturnCode_1: exception, that I learned is the
> normal exit code for grep command when it not found any match.
> The error instance object reported that the command run is:
>     *** ErrorReturnCode_1:
>       RAN: '/usr/bin/grep abc /Users/keul/test_sh'
> Obviously manually running the command I get some output and exit code 0.
> Where I'm wrong?

I'm not an expert on the sh module.  I took a quick look at the docs for 
it and didn't find anything about debugging or logging, but if there was 
something I missed which allows you to log what it's doing in greater 
detail, the first thing I would do is turn that on.

Here's some things I would try.

First, do:

dir = os.getcwd()
print dir

then pass dir as the second argument to grep().  It's one less variable 
in the equation.  Maybe os.getcwd() isn't returning what you think it 
is.  Not terribly likely, but easy to check, so eliminate that.

Next, try a simpler command.  Don't grep a whole directory, grep a 
single file.  Try:

sh.grep('abc', '/tmp/my-test-file')

and see what happens.  Get a simple case to work, then try the more 
complicated stuff.

Lastly, you might try pulling out the big guns.  Trace your entire 
process, with something like truss or strace (the details will be very 
system-dependent).  At some point, you're going to see your Python 
process do a fork/exec sequence on /bin/grep (or, perhaps, some other 
version of grep elsewhere in your path).  At that point, you'll know 
exactly what it ran, with what arguments.

Also, it helps when asking questions like this to tell us about your 
environment.  What version of Python are you running.  What version of 
the sh module did you download.  What operating system (I'm guessing 
OSX, just because of how /User/keul is capitalized).

More information about the Python-list mailing list