CoX*_*ier 4 python linux bash shell
我一直在研究bash和python脚本的脚本混合。bash 脚本可以接收未知的计数输入参数。例如 :
tinify.sh test1.jpg test2.jpg test3.jpg .....
Run Code Online (Sandbox Code Playgroud)
在 bash 接收到所有信息后,它将这些参数传递给tinify.py. 现在我想出了两种方法来做到这一点。
循环bash并调用python tinify.py testx.jpg
换句话说,python tinify test1.jpg那么python tinify test2.jpg,finalypython tinify test3.jpg
将所有参数传递给tinify.py然后循环python
但是有一个问题,我想过滤相同的参数,例如如果用户输入tinify.sh test1.jpg test1.jpg test1.jpg,我只想要tinify.sh test1.jpg。所以我认为用第二种方式更容易,因为 python 可能很方便。
如何将所有参数传递给 python 脚本?提前致谢!
你用$@在tinify.sh
#!/bin/bash
tinify.py "$@"
Run Code Online (Sandbox Code Playgroud)
消除 Python 脚本内的重复项比从 shell 中过滤掉重复项要容易得多。(当然,这会引发一个问题:您是否需要 shell 脚本。)
除了上面切普纳的回答:
#!/bin/bash
tinify.py "$@"
Run Code Online (Sandbox Code Playgroud)
在 python 脚本中,tinify.py:
from sys import argv
inputArgs = sys.argv[1:]
def remove_duplicates(l):
return list(set(l))
arguments=remove_duplicates(inputArgs)
Run Code Online (Sandbox Code Playgroud)
该列表arguments将包含传递给 python 脚本的参数(重复删除,因为set在 python 中不能包含重复值)。