5

Im trying to run this grep compound command that works fine on cmd

grep Rec sy.log | grep e612r2246s768 | grep 2013-07 | grep -oh "'.*'" | wc -c

But something is wrong here, and I can't see it yet:

import commands
commands.getstatus("""/bin/grep Rec /var/log/sy.log | /bin/grep e612r2246s768 | /bin/grep 2013-07 | /bin/grep -oh "'.*'" | /usr/bin/wc -c""")
Out[2]: 'ls: cannot access /bin/grep Rec /var/log/sy.log | /bin/grep e612r2246s768 | /bin/grep 2013-07 | /bin/grep -oh "\'.*\'" | /usr/bin/wc -c: No such file or directory'

Using subprocess:

import subprocess
cmd = ["""/bin/grep Rec /var/log/sy.log | /bin/grep e612r2246s768 | /bin/grep 2013-07 | /bin/grep -oh "'.*'" | /usr/bin/wc -c"""]
proc = subprocess.Popen(cmd, stdout=subprocess.PIPE)
---------------------------------------------------------------------------
OSError                                   Traceback (most recent call last)
/home/www-data/gpslistener/scripts/<ipython-input-24-0881e54c5eab> in <module>()
----> 1 proc = subprocess.Popen(cmd, stdout=subprocess.PIPE)

/usr/lib/python2.7/subprocess.pyc in __init__(self, args, bufsize, executable, stdin, stdout, stderr, preexec_fn, close_fds, shell, cwd, env, universal_newlines, startupinfo, creationflags)
    677                             p2cread, p2cwrite,
    678                             c2pread, c2pwrite,
--> 679                             errread, errwrite)
    680 
    681         if mswindows:

/usr/lib/python2.7/subprocess.pyc in _execute_child(self, args, executable, preexec_fn, close_fds, cwd, env, universal_newlines, startupinfo, creationflags, shell, p2cread, p2cwrite, c2pread, c2pwrite, errread, errwrite)
   1247                     if fd is not None:
   1248                         os.close(fd)
-> 1249                 raise child_exception
   1250 
   1251 

OSError: [Errno 2] No such file or directory

PD: The paths are ok, thanks

1
  • 1
    "Deprecated since version 2.6: The commands module has been removed in Python 3. Use the subprocess module instead." Commented Nov 15, 2013 at 15:19

2 Answers 2

11

Ok, this way worked to me:

>>>import subprocess
>>>cmd = ["""/bin/grep 'Rec' /var/log/sy.log | /bin/grep e612r2246s768 | /bin/grep 2013-07 | /bin/grep -oh "'.*'" | /usr/bin/wc -c"""]
>>>print subprocess.check_output(cmd,shell=True)
>>>365829
Sign up to request clarification or add additional context in comments.

2 Comments

can you please explain why 3 times """ in cmd ??
@AshishKarpe It's a bit late, but for anyone wondering, it makes it so that you can have quotes of any type inside the string. e.g. """hello"'"world""" would output hello"'"world.
8

With the usual caveat related to shell injection, the easiest way to execute a shell pipeline is by passing in shell=True

cmd = r'''/bin/grep Rec /var/log/syslog | \
    /bin/grep e612r2246s768 |\
    /bin/grep 2013-07 |\
/bin/grep -oh "'.*'" |\
/usr/bin/wc -c'''
subprocess.check_output(cmd, shell=True)

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.