如何将变量从python脚本传递到bash脚本
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/4257098/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How to pass variables from python script to bash script
提问by Rivka
I have a bash script, a.sh , and in it I have call a python script b.py . The python script calculates something, and I want it to return a value that will be used later in a.sh . I know I can do
我有一个 bash 脚本 a.sh ,在其中我调用了一个 python 脚本 b.py 。python 脚本计算一些东西,我希望它返回一个稍后将在 a.sh 中使用的值。我知道我能做到
In a.sh:
在.sh中:
var=`python b.py`
In b.py:
在 b.py 中:
print x # when x is the value I want to pass
But this is not so convenient, because I also print other messages in b.py
但这不是那么方便,因为我也在b.py中打印了其他消息
Is there any better way to do it?
有没有更好的方法来做到这一点?
Edit:
编辑:
What I'm doing now is just
我现在正在做的只是
var=`python b.py | tail -n 1`
It means I can print many things inside b.py, but only the last line (the last print command, assuming it doesn't contain "\n" in it) will be stored in var.
这意味着我可以在 b.py 中打印很多东西,但只有最后一行(最后一个打印命令,假设它不包含“\n”)会存储在 var 中。
Thanks for all the answers!
感谢所有的答案!
采纳答案by Chris Pfohl
I would print it to a file chosen on the command line then I'd get that value in bash with something like cat.
我会将它打印到在命令行上选择的文件中,然后我会在 bash 中使用类似cat.
So you'd go:
所以你会去:
python b.py tempfile.txt
var=`cat tempfile.txt`
rm tempfile.txt
[EDIT, another idea based on other answers]
[编辑,基于其他答案的另一个想法]
Your other option is to format your output carefully so you can use bash functions like head/tailto pipe only the first/last lines into your next program.
您的另一个选择是仔细格式化您的输出,以便您可以使用head/ 之类的 bash 函数tail将第一行/最后一行通过管道传输到您的下一个程序中。
回答by Martin v. L?wis
You can write the output to a temporary file, and have the shell read and delete that file. This is even less convenient, but reserves stdout for communication with the user.
您可以将输出写入临时文件,然后让 shell 读取并删除该文件。这更不方便,但保留标准输出与用户通信。
Alternatively, you can use some kind of format for stdout: the first n lines are certain variables, the rest will be echoed by the parent shell to the user. Also not convenient, but avoids using tempfiles.
或者,您可以为 stdout 使用某种格式:前 n 行是某些变量,其余的将由父 shell 回显给用户。也不方便,但避免使用临时文件。
回答by Cameron
I'm not sure about "better", but you couldwrite the result to a file then read it back in in Bash and delete it afterwards.
我不确定“更好”,但您可以将结果写入文件,然后在 Bash 中读回,然后将其删除。
This is definitely ugly, but it's something to keep in mind in case nothing else does the trick.
这绝对是丑陋的,但要记住这一点,以防万一没有其他方法可以解决问题。
回答by khachik
In your python script, redirect another messages to stderr, and print xto stdout:
在您的 python 脚本中,将另一条消息重定向到 stderr,并打印x到 stdout:
import sys
...
print >>sys.stderr, "another message"
print x
in the bash script:
在 bash 脚本中:
...
var=`python b.py 2>/dev/null`
Also, if x is an integer between 0,255, you can use exit code to pass it to the bash:
此外,如果 x 是 0,255 之间的整数,则可以使用退出代码将其传递给 bash:
import sys
...
sys.exit(x)
in bash:
在 bash 中:
python b.py
var=$?
Please note that exit code is used to indicates errors, 0 means no error, and this breaks the convention.
请注意,退出代码用于指示错误,0 表示没有错误,这违反了约定。
回答by Jakob Bowyer
I believe the answer is
我相信答案是
.py
.py
import sys
a=['zero','one','two','three']
b = int(sys.argv[1])
###your python script can still print to stderr if it likes to
print >> sys.stderr, "I am no converting"
result = a[b]
print result
.sh
.sh
#!/bin/sh
num=2
text=`python numtotext.py $num`
echo "$num as text is $text"
回答by CESCO
On bash backsticks works
在 bash backsticks works
I usualy do something like
我通常会做类似的事情
PIP_PATH=`python -c "from distutils.sysconfig \
import get_python_lib; print(get_python_lib())"`
POWELINE_PATH=$PIP_PATH"/powerline"
echo $POWELINE_PATH
回答by chanduthedev
In shell script you can use like this python_ret=$(python b.py).
It contains all print messages from python file b.py. Then you can search for a string which you are looking for. For example, if you are looking for 'Exception', you can lieke this
在 shell 脚本中,您可以像这样使用python_ret=$(python b.py)。它包含来自 python 文件 b.py 的所有打印消息。然后您可以搜索您要查找的字符串。例如,如果您正在寻找“异常”,则可以这样
if [[ $python_ret == *"Exception:"* ]]; then
echo "Got some exception."
exit 1
fi

