Using sh library with grep command

L

Luca

I'm trying to use sh (https://pypi.python.org/pypi/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?

--
-- luca

twitter: http://twitter.com/keul
linkedin: http://linkedin.com/in/lucafbb
blog: http://blog.keul.it/
 
R

Roy Smith

Luca said:
I'm trying to use sh (https://pypi.python.org/pypi/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).
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,995
Messages
2,570,230
Members
46,819
Latest member
masterdaster

Latest Threads

Top