【发布时间】:2018-03-03 23:58:57
【问题描述】:
我一直在编写bash 和python 脚本的混合脚本。 bash 脚本可以接收未知计数输入参数。例如:
tinify.sh test1.jpg test2.jpg test3.jpg .....
bash 接收到所有参数后,将这些参数传递给tinify.py。现在我想出了两种方法来做到这一点。
-
循环进入
bash并调用python tinify.py testx.jpg换句话说,
python tinify test1.jpg然后是python tinify test2.jpg,最后是python tinify test3.jpg 将所有参数传递给
tinify.py,然后循环进入python
但是有一个问题,我想过滤相同的参数,例如如果用户输入tinify.sh test1.jpg test1.jpg test1.jpg,我只想要tinify.sh test1.jpg。所以我认为第二种方式更容易,因为python可能更方便。
如何将所有参数传递给 python 脚本?提前致谢!
【问题讨论】:
-
当然,在python中一次循环。无需为每个参数调用相同的 python 脚本
-
那么如何将所有参数传递给 python 脚本?@RomanPerekhrest
-
通过分隔符分隔的字符串或json字符串
-
在 bash 中将参数传递给 python:pythonscript.sh $@,然后使用“import sys print sys.argv[0]”作为 python 中的第一个参数,依此类推。你可以在python中循环。
-
@Ardit,必须是
"$@",而不是纯$@,否则您将拆分空格,扩展名称中的文字 glob,否则不会完全按照接收到的方式传递输入。