在单个shell中运行命令

时间:2016-10-13 09:29:33

标签: python bash shell

我有一个shell脚本(这只是一个示例,实际脚本很安静,所以请考虑所有场景)

source env.sh
binary1 -a 1 -b 2
binary2 -a 1 -b 2

binary1和2是环境脚本公开的函数

我需要使用python

执行这些操作

目前我正在使用此

bashShell = subprocess.Popen(['bash'], shell=True, stdin=subprocess.PIPE, stdout=subprocess.PIPE, stderr=subprocess.PIPE)
_commands = ["source env.sh", "binary1 -a 1 -b 2", "binary2 -a 1 -b 2"]
_commands.append("exit")
for cmd in self._commands:
    bashShell.stdin.write("%s\n" % cmd)

bashShell.communicate()

这样做是使用子进程打开一个bash shell,并立即将所有输入写入stdin。

binary1需要很长时间才能执行。如果我将binary2命令写入bash子进程的stdin而binary1指令仍在执行时会不会产生任何不利影响?

注意:在单个shell中运行这些内容非常重要,因为环境脚本会公开某些功能 感谢。

0 个答案:

没有答案
相关问题