如何从python返回多个变量到bash [英] How to return multiple variables from python to bash

查看:58
本文介绍了如何从python返回多个变量到bash的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个bash脚本,它调用python脚本.起初我只是返回一个变量,这很好,但是现在我被告知要返回两个变量,而我想知道是否存在一种干净简单的方法来返回多个变量.

I have a bash script that calls a python script. At first I was just returning one variable and that is fine, but now I was told to return two variables and I was wondering if there is a clean and simple way to return more than one variable.

archiveID=$(python glacier_upload.py $archive_file_name $CURRENTVAULT)

这是我通过bash拨打的电话

Is the call I make from bash

print archive_id['ArchiveId']
archive_id['ArchiveId']

这会将存档ID返回到bash脚本

This returns the archive id to the bash script

通常我知道您可以在python中使用return语句来返回多个变量,但是它只是一个脚本,这是我发现返回变量的方式.我可以使它成为一个被调用的函数,但是即使那样,我将如何接收要传递回的多个变量?

Normally I know you can use a return statement in python to return multiple variables, but with it just being a script that is the way I found to return a variable. I could make it a function that gets called but even then, how would I receive the multiple variables that I would be passing back?

推荐答案

从您的python脚本中,每行输出一个变量.然后从bash脚本中,每行读取一个变量:

From your python script, output one variable per line. Then from you bash script, read one variable per line:

Python

print "foo bar"
print 5

重击

#! /bin/bash

python main.py | while read line ; do
    echo $line
done

最终解决方案:

感谢纪尧姆!您为我提供了一个很好的出发点.我将在这里为他人发布我的解决方案.

Final Solution:

Thanks Guillaume! You gave me a great starting point out the soultion. I am just going to post my solution here for others.

#! /bin/bash

array=()
while read line ; do
  array+=($line)
done < <(python main.py)
echo ${array[@]}

我在此处找到了所需的其余解决方案

I found the rest of the solution that I needed here

这篇关于如何从python返回多个变量到bash的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆