我有一个txt文件,每一行都是一个bash命令。我想执行每个。我在 Ubuntu 上这样做:
cat file_with_commands.txt | xargs -P10 -I {} bash -c '{}'
它有效。
-P10
标志确保最多可以同时运行 10 个进程。在 Mac 上同样的结果是:
bash: line 1: {}: command not found
bash: line 1: {}: command not found
bash: line 1: {}: command not found
bash: line 1: {}: command not found
bash: line 1: {}: command not found
bash: line 1: {}: command not found
bash --version
告诉我:GNU bash, version 5.2.0(1)-release (aarch64-apple-darwin21.6.0)
。同样在 Ubuntu 中给出:GNU bash, version 5.1.16(1)-release (x86_64-pc-linux-gnu)
如何在 Mac 上执行每行
file_with_commands.txt
?
我也试了
cat file_with_commands.txt | xargs -P10 -I {} bash -c "{}"
和cat file_with_commands.txt | xargs -P10 -I {} bash -c {}
,结果是一样的
在 Mac 上,
xargs --version
给出:
xargs: illegal option -- -
usage: xargs [-0opt] [-E eofstr] [-I replstr [-R replacements] [-S replsize]]
[-J replstr] [-L number] [-n number [-x]] [-P maxprocs]
[-s size] [utility [argument ...]]
cat file_with_commands.txt | xargs -t -P 10 -I {} bash -c '{}'
给出:
bash -c {}
bash -c {}
bash -c {}
bash -c {}
bash -c {}
bash -c {}
bash: line 1: {}: command not found
bash: line 1: {}: command not found
bash: line 1: {}: command not found
bash: line 1: {}: command not found
bash: line 1: {}: command not found
bash: line 1: {}: command not found
cat file_with_commands.txt | xargs -t -I {} echo {}
回报:
echo {}
{}
echo {}
{}
echo {}
{}
echo {}
{}
echo {}
{}
echo {}
{}
有趣的是,这种奇怪的行为只有在线路很长时才会出现。如果线路足够短,我会在
echo
时得到线路本身,而不是 {}
。 254个字符长的字符串似乎够短,更长的似乎太长了