如何将变量参数从bash脚本传递给python脚本
我一直在试图解决这个问题有一段时间没有运气.这种情况的结果是我使用bash脚本将参数发送到一个
python脚本:
例: foo.sh调用bar.py ….调用看起来像:bar.py $var1 $var2 … $varn 然后,python脚本使用sys.argv数组打印所有参数. python脚本从命令行正常工作,但是当使用bash脚本(即foo.sh)调用时,我没有从bar.py获取任何输出. 此外,我用“#!/ bin / bash -x”选项启动foo.sh,并观看输出. 总结: >两个脚本,foo.sh和bar.py 任何帮助都是极好的!!!! 谢谢! 编辑:嗨所有,感谢回复,完整的代码很长…但…两个脚本的内容可以总结 foo.sh ____ #!/bin/bash declare -a list1; declare -a list2; list1=("foo" "bar" "please"); list2=("foo" "bar" "please" "help"); declare -a joined; joined=( $(bar.py "${list1[@]}" "${list2[@]}" ) ); bar.py ____ #!/bin/python import sys for arg in sys.argv: print arg 因为我假设python中的所有缩进是正确的(不知道StackOverflow如何这样做:)).这两个代表着我所遇到的问题的核心.如上所述,bar.py正确地打印参数,当它不从foo.sh调用时. PS:我的意思是说“地壳”
编辑,因为代码已经发布
您的代码正在做正确的事情 – 除了bar.py脚本的输出被捕获到连接的数组中.因为看起来你没有打印出加入的内容,所以看不到任何输出. 这是一个示范: 文件pybash.sh #!/bin/bash declare -a list1 declare -a list2 list1=("Hello" "there" "honey") list2=("More" "strings" "here") declare -a joined joined=($(./pytest.py ${list1[@]} ${list2[@]})) echo ${joined[@]} 文件pytest.py #!/usr/bin/python import sys for i in sys.argv: print "hi" 如果您运行bash脚本,这将打印出一堆“hi”字符串. (编辑:李大同) 【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容! |