如何在python脚本中从shell脚本返回值

如何在python脚本中从shell脚本返回值,python,linux,bash,shell,unix,Python,Linux,Bash,Shell,Unix,我有一个python脚本,它需要来自shell脚本的值 以下是shell脚本(a.sh): 以下是python脚本: Import subprocess answer = Subprocess.call([‘./a.sh’]) print("the answer is %s % answer") 但它不工作。错误是“ImportError:没有名为subprocess的模块”。我想我的verison(Python2.3.4)已经很老了。在这种情况下是否可以应用子流程的任何替代品???使用子

我有一个python脚本,它需要来自shell脚本的值

以下是shell脚本(a.sh):

以下是python脚本:

Import subprocess
answer = Subprocess.call([‘./a.sh’])
print("the answer is %s % answer")  

但它不工作。错误是“ImportError:没有名为subprocess的模块”。我想我的verison(Python2.3.4)已经很老了。在这种情况下是否可以应用子流程的任何替代品???

使用子流程。检查输出而不是子流程。调用

Subprocess.call返回该脚本的返回代码。
子进程。check_output返回脚本输出的字节流


使用子流程。检查输出,而不是子流程。调用

Subprocess.call返回该脚本的返回代码。
子进程。check_output返回脚本输出的字节流


使用
子流程。检查输出:

import subprocess
answer = subprocess.check_output(['./a.sh'])
print("the answer is {}".format(answer))
>>> print subprocess.check_output.__doc__
Run command with arguments and return its output as a byte string.
有关
子流程的帮助。检查\u输出

import subprocess
answer = subprocess.check_output(['./a.sh'])
print("the answer is {}".format(answer))
>>> print subprocess.check_output.__doc__
Run command with arguments and return its output as a byte string.
演示:

a.sh

#!/bin/bash
STR="Hello World!"
echo $STR

使用
子流程。检查\u输出

import subprocess
answer = subprocess.check_output(['./a.sh'])
print("the answer is {}".format(answer))
>>> print subprocess.check_output.__doc__
Run command with arguments and return its output as a byte string.
有关
子流程的帮助。检查\u输出

import subprocess
answer = subprocess.check_output(['./a.sh'])
print("the answer is {}".format(answer))
>>> print subprocess.check_output.__doc__
Run command with arguments and return its output as a byte string.
演示:

a.sh

#!/bin/bash
STR="Hello World!"
echo $STR

谢谢ashwini的回答。。python脚本中的语句实际上不是打印语句,而是cvs命令。我只是为了提问而简化了它。因此,在这种情况下,我们可以使用{}来插值答案变量,还是需要使用%s???@user2475677被称为新型字符串格式,如果您只使用字符串格式,那么这两个选项都可以。非常感谢ashwini,但我刚刚意识到,我的Python版本(2.3.4)非常旧!!它没有“subprocess”,如果这个“a.sh”脚本是一个交互式脚本,会提示用户一些问题,该怎么办。在这种情况下,我们如何在python脚本中捕获这些值…?感谢ashwini的回答。。python脚本中的语句实际上不是打印语句,而是cvs命令。我只是为了提问而简化了它。因此,在这种情况下,我们可以使用{}来插值答案变量,还是需要使用%s???@user2475677被称为新型字符串格式,如果您只使用字符串格式,那么这两个选项都可以。非常感谢ashwini,但我刚刚意识到,我的Python版本(2.3.4)非常旧!!它没有“subprocess”,如果这个“a.sh”脚本是一个交互式脚本,会提示用户一些问题,该怎么办。在这种情况下,我们如何在python脚本中捕获这些值。。。?