2

I have a script in which I am trying to use subprocess.call to execute a series of shell commands, but which appears to have some commands omitted when executed.

Specifically:

#!/usr/bin/python
import tempfile
import subprocess
import os
import re


grepfd, grepfpath = tempfile.mkstemp(suffix=".xx")
sedfd,  sedfpath  = tempfile.mkstemp(suffix=".xx")

# grepoutfile = open( grepfpath, 'w')
sedoutfile  = open( sedfpath,  'w' )

subprocess.call(['cp','/Users/bobby/Downloads/sample.txt', grepfpath])

sedcmd = [ 'sort', 
           grepfpath,
           '|', 
           'uniq',
           '|',
           'sed',
           '-e',
           '"s/bigstring of word/ smaller /"',
           '|',
           'column',
           '-t',
           '-s',
           '"=>"' ]

print "sedcmd = ", sedcmd
subprocess.call( ['ls', grepfpath ] )
subprocess.call( ['sort', '|', 'uniq' ], stdin = grepfd )
subprocess.call( sedcmd,  stdout = sedoutfile )

And it generates this as output:

python d3.py

sedcmd = ['sort', /var/folders/3h/_0xwt5bx0hx8tgx06cmq9h_4f183ql/T/tmp5Gp0ff.xx', '|', 'uniq', '|', 'sed', '-e', '"s/bigstring of word/ smaller /"', '|', 'column', '-t', '-s', '"=>"'] /var/folders/3h/_0xwt5bx0hx8tgx06cmq9h_4f183ql/T/tmp5Gp0ff.xx sort: open failed: |: No such file or directory
sort: invalid option -- e Try `sort --help' for more information.

The first 'sort: open failed: |:No such file... is from the first subprocess call ['sort','|','uniq'], stdin = grepfd ) The 'sort: invalid option -- e .. is from the second subprocess call (sedcmd).

I have seen a lot of examples that use pipes in this context -- so what am I doing wrong?
Thanks!

NotCharlie
  • 43
  • 2
  • 9
  • If you're trying to use shell features like pipes, you will need to pass a string (not a list) and set `shell=True`. Read the `subprocess` documentation for details. – larsks Sep 08 '17 at 13:34

2 Answers2

3

This is a class that will run a command with an arbitrary number of pipes:

pipeline.py

import shlex
import subprocess

class Pipeline(object):
    def __init__(self, command):
        self.command = command
        self.command_list = self.command.split('|')
        self.output = None
        self.errors = None
        self.status = None
        self.result = None

    def run(self):
        process_list = list()
        previous_process = None
        for command in self.command_list:
            args = shlex.split(command)
            if previous_process is None:
                process = subprocess.Popen(args, stdout=subprocess.PIPE)
            else:
                process = subprocess.Popen(args,
                                           stdin=previous_process.stdout,
                                           stdout=subprocess.PIPE)
            process_list.append(process)
            previous_process = process
        last_process = process_list[-1]
        self.output, self.errors = last_process.communicate()
        self.status = last_process.returncode
        self.result = (0 == self.status)
        return self.result

This example shows how to use the class:

harness.py

from pipeline import Pipeline

if __name__ == '__main__':
    command = '|'.join([
        "sort %s",
        "uniq",
        "sed -e 's/bigstring of word/ smaller /'",
        "column -t -s '=>'"
    ])
    command = command % 'sample.txt'
    pipeline = Pipeline(command)
    if not pipeline.run():
        print "ERROR: Pipeline failed"
    else:
        print pipeline.output

I created this sample file to for testing:

sample.txt

word1>word2=word3
list1>list2=list3
a>bigstring of word=b
blah1>blah2=blah3

Output

a       smaller   b
blah1  blah2      blah3
list1  list2      list3
word1  word2      word3
0

So if in a command you want to use shell pipes you can add shell=True in subprocess: so it will be like this:

sedcmd = 'sort /var/folders/3h/_0xwt5bx0hx8tgx06cmq9h_4f183ql/T/tmp5Gp0ff.xx | uniq | sed -e "s/bigstring of word/ smaller /" | column -t -s "=>" '
subprocess.call(sedcmd, shell=True)

But be carefull with shell=True, it's strongly discouraged to use it : subprocess official documentation

So if you want to use pipes without shell=True you can use subprocees.PIPE in the stdout , and here's an example on how to do it: stackoveflow answer

slim tabka
  • 43
  • 9