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