如何将变量参数从 bash 脚本传递到 python 脚本
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/3955571/
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 variable arguments from bash script to python script
提问by certifiedNoob
I've been trying to solve this issue for sometime now with no luck. The crust of the situation is that I'm using a bash script to send parameters to a a python script:
我一直试图解决这个问题一段时间,但没有运气。情况的关键是我正在使用 bash 脚本将参数发送到 aa python 脚本:
Example:
例子:
foo.sh calls bar.py....the call looks like: bar.py $var1 $var2 ... $varn
foo.sh 调用 bar.py.... 调用看起来像: bar.py $var1 $var2 ... $varn
The python script then prints all the arguments using the sys.argv array. The python script works correctly from the command line, but when called from with the bash script (i.e foo.sh), I get no output from bar.py.
然后,python 脚本使用 sys.argv 数组打印所有参数。python 脚本从命令行正常工作,但是当使用 bash 脚本(即 foo.sh)调用时,我没有从 bar.py 得到任何输出。
Also, I started foo.sh with the "#!/bin/bash -x" option and watched the output as well.
此外,我使用“#!/bin/bash -x”选项启动 foo.sh 并观察输出。
TO summarize:
总结:
- Two scripts, foo.sh and bar.py
- foo.sh calls bar.py, passing variables of foo.sh as arguments to bar.py
- bar.py prints the arguments it sees using sys.argv
- bar.py works when run from its own terminal, doesn't work when called from foo.sh
- 两个脚本,foo.sh 和 bar.py
- foo.sh 调用 bar.py,将 foo.sh 的变量作为参数传递给 bar.py
- bar.py 使用 sys.argv 打印它看到的参数
- bar.py 从它自己的终端运行时工作,从 foo.sh 调用时不起作用
Any help would be awesome!!!!
任何帮助都是极好的!!!!
Thanks!
谢谢!
Edit: Hi all, thanks for the replies, the complete code is pretty long...but... the contents of the two scripts could be summed
编辑:大家好,感谢您的回复,完整的代码很长...但是...两个脚本的内容可以总结
foo.sh ____
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 ____
bar.py ___ _
#!/bin/python
import sys
for arg in sys.argv:
print arg
As I assume all the indents in the python are correct (not sure how StackOverflow does this yet :) ). These two represent the core of the issue i'm having. As stated, bar.py prints arguments correctly, when it it not called from foo.sh.
因为我假设 python 中的所有缩进都是正确的(不确定 StackOverflow 如何做到这一点:))。这两个代表了我遇到的问题的核心。如前所述,当 bar.py 不是从 foo.sh 调用时,它会正确打印参数。
PS: I did mean to say "crust"
PS:我的意思是说“地壳”
采纳答案by wkl
Edit, since code has been posted
编辑,因为代码已经发布
Your code is doing the correct thing - except that the output from your bar.pyscript is being captured into the array joined. Since it looks like you're not printing out the contents of joined, you never see any output.
您的代码正在做正确的事情 - 除了bar.py脚本的输出被捕获到数组中joined。由于看起来您没有打印出 的内容joined,因此您永远看不到任何输出。
Here's a demonstration:
这是一个演示:
File pybash.sh
文件 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[@]}
File pytest.py
文件 pytest.py
#!/usr/bin/python
import sys
for i in sys.argv:
print "hi"
This will print out a bunch of 'hi' strings if you run the bash script.
如果您运行 bash 脚本,这将打印出一堆 'hi' 字符串。
回答by nstehr
I have pretty much the exact setup that you are describing, and this is how my bash script looks:
我几乎有你描述的确切设置,这就是我的 bash 脚本的外观:
VAR1=...
VAR2=...
VAR3=...
python my_script.py $VAR1 $VAR2 $VAR3
回答by certifiedNoob
EDIT:
编辑:
I figured it out, I had some weired combo of characters, the line was not properly escaped. I changed it from
我想通了,我有一些奇怪的字符组合,该行没有正确转义。我把它从
var=( $( some commands) )
var=( $( 一些命令) )
to
到
var=( some commands ) // using backticks (still learning the SO editor...)
var=( some commands ) // 使用反引号(仍在学习 SO 编辑器...)
Bash escaping is some ride lol! To those who answered, thanks for all your help
Bash 转义是一些骑行,哈哈!对于那些回答的人,感谢您的所有帮助

