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


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

Re: Using sh library with grep command

Started byLuca Fabbri <luca@keul.it>
First post2013-11-23 18:23 +0100
Last post2013-11-23 18:23 +0100
Articles 1 — 1 participant

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

This discussion starts older than the indexed window; earlier articles aren't shown. The article labeled Started by below is the oldest one visible, not the original post.


Contents

  Re: Using sh library with grep command Luca Fabbri <luca@keul.it> - 2013-11-23 18:23 +0100

#60316 — Re: Using sh library with grep command

FromLuca Fabbri <luca@keul.it>
Date2013-11-23 18:23 +0100
SubjectRe: Using sh library with grep command
Message-ID<mailman.3097.1385227421.18130.python-list@python.org>
On Sat, Nov 23, 2013 at 5:29 PM, Peter Otten <__peter__@web.de> wrote:
> Luca 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?
>
> Did you run grep with or without the -r option?
>
> The code sample and the error message don't match. Maybe you accidentally
> left out the -r in your actual code.
>

Sorry all, it was a stupid error and I provided a bad example.

I was running...
   sh.grep('"abc"', os.getcwd(), '-r')

...and the output of the command inside the exception was exactly...
    RAN: '/usr/bin/grep "abc" /Users/keul/test_sh -r'

So, at first glance it was ok (copying/pasting it in in the terminal
return exactly was I expected).

The error? The doublequote inclusion.

Using this...
   sh.grep('abc', os.getcwd(), '-r')

...I  get this output...
    RAN: '/usr/bin/grep abc /Users/keul/test_sh -r'

But this time I get the expected result (both in terminal and python
env). So seems that when quoting you get a false positive right
command output but a wrong execution.


-- 
-- luca

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

[toc] | [standalone]


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


csiph-web