shell脚本: Hello.sh输入shell脚本变量的值以非交互方式使用python
#!/bin/bash
echo "Enter your name: "
read name
echo "Hello $name"
我想从蟒蛇中调用Hello.sh并填写变量“名”非交互方式。如何做呢?
shell脚本: Hello.sh输入shell脚本变量的值以非交互方式使用python
#!/bin/bash
echo "Enter your name: "
read name
echo "Hello $name"
我想从蟒蛇中调用Hello.sh并填写变量“名”非交互方式。如何做呢?
+1的pipes。一个更“壳十岁上下”的方法是:
import subprocess
the_name = 'the_name'
myproc = subprocess.Popen(['echo %s | bash Hello.sh' % the_name], stdin = subprocess.PIPE, stdout = subprocess.PIPE, shell=True)
out, err = myproc.communicate()
print out
你应该能够Popen.communicate
与子:
from subprocess import Popen,PIPE
p = Popen(['bash','./Hello.sh'],stdin=PIPE,stderr=PIPE,stdout=PIPE)
stdout_data,stderr_data = p.communicate("Hello World!\n")
这是一样的你[前一个问题(http://stackoverflow.com/questions/15548393/get-shell-script-read-value-从-Python的脚本)。 – chepner 2013-03-21 15:26:55