简体   繁体   English

在 shell 脚本中迭代 arguments ( arguments 作为列表从 python 文件发送)

[英]Iterate the arguments in shell script ( arguments sent as a list from python file )

I have tried below:我在下面尝试过:

param_list = ("hi ", "life", "jam", "hello")

print param_list

subprocess.call(shlex.split('./test.sh *param_list'))

In my shell script, I want to loop through the list that I passed:在我的 shell 脚本中,我想遍历我通过的列表:

#!/bin/sh


for i in $*; do
  echo $i
done

But the above code prints *param_list not the ("hi ", "life", "jam", "hello").但上面的代码打印 *param_list 而不是 ("hi", "life", "jam", "hello")。 Can someone help me with this requirement !!有人可以帮我解决这个要求!

Others have discussed the python side.其他人讨论了 python 方面。 For the shell, you need to use对于 shell,您需要使用

for arg in "$@"; do

which will iterate over the args precisely as given, without being affected by word splitting or filename expansion.它将按照给定的方式精确地迭代 args,而不受分词或文件名扩展的影响。

To see the difference, try this:要查看差异,请尝试以下操作:

# set positional parameters
set -- "first arg" "second arg" "*"

# Now, echo the args
for arg in "$@"; do echo "$arg"; done

# versus
for word in $*; do echo "$word"; done

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

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