Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]
Groups > comp.lang.python > #60316
| References | <CAGfGq=ax9rHaiVYuKmkF4b8fTN_VyXznyzkoHq4FHcJ_W6BhOQ@mail.gmail.com> <l6ql3s$fkc$1@ger.gmane.org> |
|---|---|
| From | Luca Fabbri <luca@keul.it> |
| Date | 2013-11-23 18:23 +0100 |
| Subject | Re: Using sh library with grep command |
| Newsgroups | comp.lang.python |
| Message-ID | <mailman.3097.1385227421.18130.python-list@python.org> (permalink) |
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/
Back to comp.lang.python | Previous | Next | Find similar | Unroll thread
Re: Using sh library with grep command Luca Fabbri <luca@keul.it> - 2013-11-23 18:23 +0100
csiph-web