use*_*677 1 python unix linux bash shell
我有一个python脚本,需要来自shell脚本的值.
以下是shell脚本(a.sh):
#!/bin/bash
return_value(){
value=$(///some unix command)
echo "$value"
}
return_value
Run Code Online (Sandbox Code Playgroud)
以下是python脚本:
Import subprocess
answer = Subprocess.call([‘./a.sh’])
print("the answer is %s % answer")
Run Code Online (Sandbox Code Playgroud)
但它不工作.错误是"ImportError:没有名为subprocess的模块".我想我的verison(Python 2.3.4)已经很老了.在这种情况下,是否可以替代可以应用的子进程?
用途subprocess.check_output:
import subprocess
answer = subprocess.check_output(['./a.sh'])
print("the answer is {}".format(answer))
Run Code Online (Sandbox Code Playgroud)
帮助subprocess.check_output:
>>> print subprocess.check_output.__doc__
Run command with arguments and return its output as a byte string.
Run Code Online (Sandbox Code Playgroud)
演示:
>>> import subprocess
>>> answer = subprocess.check_output(['./a.sh'])
>>> answer
'Hello World!\n'
>>> print("the answer is {}".format(answer))
the answer is Hello World!
Run Code Online (Sandbox Code Playgroud)
a.sh :
#!/bin/bash
STR="Hello World!"
echo $STR
Run Code Online (Sandbox Code Playgroud)