xargs 在 Linux 上不工作
xargs not working on Linux
我想同时 运行 多个 python 具有不同参数的脚本。
在尝试这样做时,我遇到了这个 xargs
命令;我想了解它。
尝试此示例命令时 echo {a..d} | xargs -n 1 -I % mv % %-01
它可以在我的 MacBook 上运行并提供所需的输出。
但是在我登录 VPS 运行ning Ubuntu 16.04 并发出相同的命令后,我得到了这个 mv: cannot stat 'a b c d': No such file or directory
我查看了手册页并用谷歌搜索找了一圈也没找到原因。
ps:我猜 xargs 是默认 Ubuntu 仓库的最新版本。
(这是我关于 SO 的第一个问题)。
您的 echo
命令生成一行 space 分隔的输出:
$ echo {a..d}
a b c d
通常 xargs
需要白色 space 分隔的输入,当使用 -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 in‐
put items; instead the separator is the newline character. Implies -x
and -L 1.
您现有的命令行会生成以下命令:
mv a b c d a b c d-01
您需要将 echo
命令的输出分成多行:
$ echo {a..d} | tr ' ' '\n' | xargs -n 1 -I % echo mv % %-01
mv a a-01
mv b b-01
mv c c-01
mv d d-01
正如您所指出的,您可以将上面的 tr ...
替换为 xargs -n1
,这会给您相同的结果:因为没有 -I
xargs 读取 whitespace 分隔的参数,这导致在单独的行上回显每个参数。
我想同时 运行 多个 python 具有不同参数的脚本。
在尝试这样做时,我遇到了这个 xargs
命令;我想了解它。
尝试此示例命令时 echo {a..d} | xargs -n 1 -I % mv % %-01
它可以在我的 MacBook 上运行并提供所需的输出。
但是在我登录 VPS 运行ning Ubuntu 16.04 并发出相同的命令后,我得到了这个 mv: cannot stat 'a b c d': No such file or directory
我查看了手册页并用谷歌搜索找了一圈也没找到原因。
ps:我猜 xargs 是默认 Ubuntu 仓库的最新版本。
(这是我关于 SO 的第一个问题)。
您的 echo
命令生成一行 space 分隔的输出:
$ echo {a..d}
a b c d
通常 xargs
需要白色 space 分隔的输入,当使用 -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 in‐
put items; instead the separator is the newline character. Implies -x
and -L 1.
您现有的命令行会生成以下命令:
mv a b c d a b c d-01
您需要将 echo
命令的输出分成多行:
$ echo {a..d} | tr ' ' '\n' | xargs -n 1 -I % echo mv % %-01
mv a a-01
mv b b-01
mv c c-01
mv d d-01
正如您所指出的,您可以将上面的 tr ...
替换为 xargs -n1
,这会给您相同的结果:因为没有 -I
xargs 读取 whitespace 分隔的参数,这导致在单独的行上回显每个参数。