如何通过python执行Shell脚本 [英] How to execute a shell script through python
本文介绍了如何通过python执行Shell脚本的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我有一个脚本,例如abc.sh,其中包含带有标志的命令列表. 例子
I have a script say abc.sh which has list of commands with flags. example
//abc.sh
echo $FLAG_name
cp $FLAG_file1 $FLAG_file2
echo 'file copied'
我想通过python代码执行此脚本. 说
I want to execute this script through python code. say
//xyz.py
name = 'FUnCOder'
filename1 = 'aaa.txt'
filename2 = 'bbb.txt'
subprocess.call([abc.sh, name, filename1, filname2], stdout=PIPE, stderr=PIPE, shell=True)
此呼叫无法正常工作.
还有哪些其他选择?
shell脚本文件也位于其他目录中.我希望输出记录在日志中.
Also the shell script file is in some other directory. And I want the output to go in logs.
推荐答案
通常,您要使用 Popen
,因为您之后具有流程控制权.试试:
Usually you want to use Popen
since you have process control afterwards. Try:
process = subprocess.Popen(['abc.sh', name, filename1, filname2], stdout=PIPE, stderr=PIPE)
process.wait() # Wait for process to complete.
# iterate on the stdout line by line
for line in process.stdout.readlines():
print(line)
这篇关于如何通过python执行Shell脚本的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文