Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]


Groups > comp.lang.python > #60305 > unrolled thread

Using sh library with grep command

Started byLuca <lucafbb@gmail.com>
First post2013-11-23 15:38 +0100
Last post2013-11-23 10:30 -0500
Articles 2 — 2 participants

Back to article view | Back to comp.lang.python


Contents

  Using sh library with grep command Luca <lucafbb@gmail.com> - 2013-11-23 15:38 +0100
    Re: Using sh library with grep command Roy Smith <roy@panix.com> - 2013-11-23 10:30 -0500

#60305 — Using sh library with grep command

FromLuca <lucafbb@gmail.com>
Date2013-11-23 15:38 +0100
SubjectUsing sh library with grep command
Message-ID<mailman.3088.1385218947.18130.python-list@python.org>
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/

[toc] | [next] | [standalone]


#60307

FromRoy Smith <roy@panix.com>
Date2013-11-23 10:30 -0500
Message-ID<roy-6190EE.10304723112013@news.panix.com>
In reply to#60305
In article <mailman.3088.1385218947.18130.python-list@python.org>,
 Luca <lucafbb@gmail.com> wrote:

> 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).

[toc] | [prev] | [standalone]


Back to top | Article view | comp.lang.python


csiph-web