[英]How to pass a value to shell script from subprocess.Popen in Python
I have the following: 我有以下几点:
myscript.py myscript.py
#!/usr/bin/python
from threading import Thread
import time
import subprocess
subprocess.Popen("./hello.sh 1", shell=True)
subprocess.Popen("./hello.sh 2", shell=True)
subprocess.Popen("./hello.sh 3", shell=True)
hello.sh 你好
echo "Hello From Shell Script $1"
The output is: 输出为:
Hello From Shell Script 1
Hello From Shell Script 2
Hello From Shell Script 3
I want to do this in a for loop like so: 我想像这样在for循环中执行此操作:
for num in range(1,3):
subprocess.Popen(['./hello.sh', str(num)], shell=True)
But the output is: 但是输出是:
Hello From Shell Script
Hello From Shell Script
Hello From Shell Script
If I drop the shell=True so its now: 如果我放下shell = True ,现在放下它:
subprocess.Popen(['./hello.sh', str(num)])
I get the following: 我得到以下内容:
Traceback (most recent call last):
File "./myscript.py", line 12, in <module>
subprocess.Popen(['./hello.sh', str(num)])
File "/usr/lib64/python2.6/subprocess.py", line 623, in __init__
errread, errwrite)
File "/usr/lib64/python2.6/subprocess.py", line 1141, in _execute_child
raise child_exception
OSError: [Errno 8] Exec format error
How can I get this to pass in the correct value to the script? 如何获得此值以将正确的值传递给脚本?
Change it to read as follows: 将其更改为如下:
>>> for num in range(1,3):
... subprocess.Popen(['./hello.sh '+str(num)], shell=True)
如果要传递列表而不是字符串作为第一个参数,请不要使用shell=True
。
subprocess.Popen(['./hello.sh', str(num)])
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.