简体   繁体   English

如何将变量从 python 脚本传递到 bash 脚本

[英]How to pass variables from python script to bash script

I have a bash script, a.sh , and in it I have call a python script b.py .我有一个 bash 脚本 a.sh ,在其中我调用了一个 python 脚本 b.py 。 The python script calculates something, and I want it to return a value that will be used later in a.sh . python 脚本计算了一些东西,我希望它返回一个稍后将在 a.sh 中使用的值。 I know I can do我知道我能做到

In a.sh:在 a.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!感谢所有的答案!

I would print it to a file chosen on the command line then I'd get that value in bash with something like cat . 我会将它打印到命令行中选择的文件,然后我会用像cat这样的东西在bash中获得该值。

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 / tail to pipe only the first/last lines into your next program. 您的另一个选择是仔细格式化您的输出,这样您就可以使用head / tail等bash函数将第一行/最后一行输送到下一个程序。

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" 

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

I'm not sure about "better", but you could write 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. 这绝对是丑陋的,但要记住这一点,万一其他没有其他功能。

In your python script, redirect another messages to stderr, and print x to 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表示没有错误,这违反了约定。

You can write the output to a temporary file, and have the shell read and delete that file. 您可以将输出写入临时文件,并让shell读取并删除该文件。 This is even less convenient, but reserves stdout for communication with the user. 这甚至不太方便,但保留了与用户沟通的标准。

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. 或者,您可以为stdout使用某种格式:前n行是某些变量,其余的将由父shell回显给用户。 Also not convenient, but avoids using tempfiles. 也不方便,但避免使用临时文件。

In shell script you can use like this python_ret=$(python b.py) .在 shell 脚本中,您可以像这样使用python_ret=$(python b.py) It contains all print messages from python file b.py.它包含来自 python 文件 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例如,如果您正在寻找“例外”,您可以这样

if [[ $python_ret == *"Exception:"* ]]; then
    echo "Got some exception."
    exit 1
fi

Better to forward the print value from the python script to a temp file before assigning it in a bash value.最好将打印值从 python 脚本转发到临时文件,然后再将其分配为 bash 值。 I believe there's no need to remove the file if this is the case.如果是这种情况,我相信没有必要删除文件。

!#/bin/bash
python b.py > tempfile.txt
var=`cat tempfile.txt`

Then, get the value:然后,获取值:

echo $var

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM