加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 综合聚焦 > 服务器 > 安全 > 正文

在bash脚本中存储Python脚本的返回值

发布时间:2020-12-15 19:21:31 所属栏目:安全 来源:网络整理
导读:我想从一个bash脚本执行一个python脚本,我想把python脚本的输出存储在变量中。 在我的python脚本中,我打印一些东西到屏幕,最后我返回一个字符串: sys.exit(myString) 在我的bash脚本中,我做了以下: outputString=`python myPythonScript arg1 arg2 arg
我想从一个bash脚本执行一个python脚本,我想把python脚本的输出存储在变量中。

在我的python脚本中,我打印一些东西到屏幕,最后我返回一个字符串:

sys.exit(myString)

在我的bash脚本中,我做了以下:

outputString=`python myPythonScript arg1 arg2 arg3 `

但是当我使用echo $ outputString检查outputString的值时,我得到了Python脚本打印到屏幕的所有内容,但不是返回值myString!

我该怎么办?

编辑:我需要字符串,因为它告诉我Python脚本创建的文件所在的位置。我想做一些像:

fileLocation=`python myPythonScript1 arg1 arg2 arg1`
python myPythonScript2 $fileLocation
http://docs.python.org/library/sys.html#sys.exit

如果使用字符串参数,sys.exit将会写入stderr。

做某事

python yourscript 2> return_file

Bash script – store stderr in variable

你可以在你的脚本中做这样的事情

output=$((your command here) 2> &1)

例:

test.py

print "something"
exit('ohoh')

t.sh

va=$(python test.py 2>&1)                                                                                                                    
mkdir $va

bash t.sh

编辑

不知道为什么,但在这种情况下,我会写一个主要脚本和另外两个脚本…混合python和bash是没有意义的,除非你真的需要。

import script1
import script2

if __name__ == '__main__':
    filename = script1.run(sys.args)
    script2.run(filename)

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读