如何将参数从 xargs 传递到 python 脚本?

How to pass parameters from xargs to python script?

我有 command.list 文件,其中包含我的 python 脚本 my_script.py 的命令参数,其中有 3 个参数。

其中一行看起来像:

<path1> <path2> -sc 4

看起来它不能像这样工作,因为应该拆分参数?

cat command.list | xargs -I {} python3 my_script.py {}

如何将字符串拆分为参数并将其传递给 python 脚本?

不确定,你想用 xargs -I {} python3 my_script.py {} 做什么。 但是你在找,

$ cat file
<path1> <path2> -sc 4
....
<path1n> <path2n> -sc 4

$ while read -r path1 path2 unwanted unwanted; do python3 my_script.py "$path2" ; done<file

来自 man xargs

-I 的文档

-I replace-str
Replace occurrences of replace-str in the initial-arguments with names read from standard input. Also, unquoted blanks do not terminate input items; instead the separator is the newline character. Implies -x and -L 1.

你要的是

xargs -L1 python3 my_script.py

顺便说一句:cat 不是必需的。使用以下命令之一

< command.list xargs -L1 python3 my_script.py
xargs -a command.list -L1 python3 my_script.py

cat command.list | xargs -L 1 python3 my_script.py呢?这将一次向您的脚本传递一行 (-L 1)。