使用子进程运行多个bash命令

时间:2021-01-05 00:25:19

If I run echo a; echo b in bash the result will be that both commands are run. However if I use subprocess then the first command is run, printing out the whole of the rest of the line. The code below echos a; echo b instead of a b, how do I get it to run both commands?

如果我运行echo a;在bash中回显b结果将是两个命令都运行。但是,如果我使用子进程,则运行第一个命令,打印出整个行的其余部分。下面的代码回声a; echo b而不是b,如何让它运行这两个命令?

import subprocess, shlex
def subprocess_cmd(command):
    process = subprocess.Popen(shlex.split(command), stdout=subprocess.PIPE)
    proc_stdout = process.communicate()[0].strip() 
    print proc_stdout

subprocess_cmd("echo a; echo b")

5 个解决方案

#1


47  

You have to use shell=True in subprocess and no shlex.split:

你必须在子进程中使用shell = True而没有shlex.split:

def subprocess_cmd(command):
    process = subprocess.Popen(command,stdout=subprocess.PIPE, shell=True)
    proc_stdout = process.communicate()[0].strip()
    print proc_stdout

subprocess_cmd('echo a; echo b')

returns:

a
b

#2


14  

I just stumbled on a situation where I needed to run a bunch of lines of bash code (not separated with semicolons) from within python. In this scenario the proposed solutions do not help. One approach would be to save a file and then run it with Popen, but it wasn't possible in my situation.

我偶然发现我需要在python中运行一堆bash代码行(不用分号分隔)。在这种情况下,建议的解决方案没有帮助。一种方法是保存文件然后用Popen运行它,但在我的情况下是不可能的。

What I ended up doing is something like:

我最终做的是:

commands = '''
echo "a"
echo "b"
echo "c"
echo "d"
'''

process = subprocess.Popen('/bin/bash', stdin=subprocess.PIPE, stdout=subprocess.PIPE)
out, err = process.communicate(commands)
print out

So I first create the child bash process and after I tell it what to execute. This approach removes the limitations of passing the command directly to the Popen constructor.

所以我首先创建子bash进程,然后告诉它要执行什么。此方法消除了将命令直接传递给Popen构造函数的限制。

#3


6  

Join commands with "&&".

使用“&&”加入命令。

os.system('echo a > outputa.txt && echo b > outputb.txt')

#4


1  

If you're only running the commands in one shot then you can just use subprocess.check_output convenience function:

如果您只是一次性运行命令,那么您可以使用subprocess.check_output方便功能:

def subprocess_cmd(command):
    output = subprocess.check_output(command, shell=True)
    print output

#5


0  

>>> command = "echo a; echo b"
>>> shlex.split(command);
    ['echo', 'a; echo', 'b']

so, the problem is shlex module do not handle ";"

所以,问题是shlex模块不处理“;”

#1


47  

You have to use shell=True in subprocess and no shlex.split:

你必须在子进程中使用shell = True而没有shlex.split:

def subprocess_cmd(command):
    process = subprocess.Popen(command,stdout=subprocess.PIPE, shell=True)
    proc_stdout = process.communicate()[0].strip()
    print proc_stdout

subprocess_cmd('echo a; echo b')

returns:

a
b

#2


14  

I just stumbled on a situation where I needed to run a bunch of lines of bash code (not separated with semicolons) from within python. In this scenario the proposed solutions do not help. One approach would be to save a file and then run it with Popen, but it wasn't possible in my situation.

我偶然发现我需要在python中运行一堆bash代码行(不用分号分隔)。在这种情况下,建议的解决方案没有帮助。一种方法是保存文件然后用Popen运行它,但在我的情况下是不可能的。

What I ended up doing is something like:

我最终做的是:

commands = '''
echo "a"
echo "b"
echo "c"
echo "d"
'''

process = subprocess.Popen('/bin/bash', stdin=subprocess.PIPE, stdout=subprocess.PIPE)
out, err = process.communicate(commands)
print out

So I first create the child bash process and after I tell it what to execute. This approach removes the limitations of passing the command directly to the Popen constructor.

所以我首先创建子bash进程,然后告诉它要执行什么。此方法消除了将命令直接传递给Popen构造函数的限制。

#3


6  

Join commands with "&&".

使用“&&”加入命令。

os.system('echo a > outputa.txt && echo b > outputb.txt')

#4


1  

If you're only running the commands in one shot then you can just use subprocess.check_output convenience function:

如果您只是一次性运行命令,那么您可以使用subprocess.check_output方便功能:

def subprocess_cmd(command):
    output = subprocess.check_output(command, shell=True)
    print output

#5


0  

>>> command = "echo a; echo b"
>>> shlex.split(command);
    ['echo', 'a; echo', 'b']

so, the problem is shlex module do not handle ";"

所以,问题是shlex模块不处理“;”