在bash脚本Python脚本的商店返回值 [英] store return value of a Python script in a bash script
问题描述
我想从一个bash脚本执行Python脚本,我想Python脚本的输出存储在一个变量。
在我的Python脚本,我打印一些东西到屏幕上,并在最后我返回一个字符串:
sys.exit(MyString的)
在我的bash脚本,我做了以下内容:
outputString =`蟒蛇myPythonScript ARG1参数3`
但后来当我检查 outputString
的值与回声$ outputString
我得到的一切,Python脚本有打印到屏幕,但的不的返回值的myString
!
我应该怎么做呢?
编辑:我需要的字符串,因为它告诉我在哪里,由Python脚本创建的文件的位置。我想要做的是这样的:
fileLocation =`蟒蛇myPythonScript1 ARG1 ARG2 arg1`
蟒蛇myPythonScript2 $ fileLocation
<一个href=\"http://docs.python.org/library/sys.html#sys.exit\">http://docs.python.org/library/sys.html#sys.exit
sys.exit
如果您使用的字符串ARGS将写入标准错误
。
做这样的事情。
蟒蛇yourscript 2 - ; return_file
您可以做这样的事情在你的脚本
输出= $((你的命令在这里)2 - ;&安培; 1)
例如:
test.py
打印东西
退出(OHOH)
t.sh
VA = $(蟒蛇test.py 2 - ;&安培; 1)
MKDIR $ VA
庆典t.sh
的修改的
不知道为什么,但在这种情况下,我会写一个主脚本和其他两个脚本......混合蟒蛇和bash是没有意义的,除非你真的需要。
进口SCRIPT1
进口SCRIPT2如果__name__ =='__main__':
文件名= script1.run(sys.args)
script2.run(文件名)
I want to execute a python script from a bash script, and I want to store the output of the python script in a variable.
In my python script, I print some stuff to screen and at the end I return a string with:
sys.exit(myString)
In my bash script, I did the following:
outputString=`python myPythonScript arg1 arg2 arg3 `
But then when I check the value of outputString
with echo $outputString
I get everything that the Python script had printed to screen, but not the return value myString
!
How should I do this?
EDIT: I need the string because that tells me where a file created by the Python script is located. I want to do something like:
fileLocation=`python myPythonScript1 arg1 arg2 arg1`
python myPythonScript2 $fileLocation
http://docs.python.org/library/sys.html#sys.exit
sys.exit
will write to stderr
if you use string args.
Do something like
python yourscript 2> return_file
Bash script - store stderr in variable
You could do something like that in your script
output=$((your command here) 2> &1)
example:
test.py
print "something"
exit('ohoh')
t.sh
va=$(python test.py 2>&1)
mkdir $va
bash t.sh
edit
Not sure why but in that case, I would write a main script and two other scripts... Mixing python and bash is pointless unless you really need to.
import script1
import script2
if __name__ == '__main__':
filename = script1.run(sys.args)
script2.run(filename)
这篇关于在bash脚本Python脚本的商店返回值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!