输出是否因为 xargs 而像这样混合,我该如何解决?

tub*_*bis 4 scripting xargs output

我正在通过使用xargs列出一堆 IP 地址的 whois 记录来学习一些Bash。使用的命令是:

echo "$1" | tr "\n" "\0" | xargs -0 -n 1 -t -P 3 -I %  sh -c 'echo "\n 44rBegin whois record -- \n"; whois -h whois.arin.net % ; echo "\n 44rEnd whois record -- \n"'
Run Code Online (Sandbox Code Playgroud)

执行的命令是:

sh -c echo "\n 44rBegin whois record -- \n"; whois -h whois.arin.net 206.190.36.45 ; echo "\n 44rEnd whois record -- \n"
sh -c echo "\n 44rBegin whois record -- \n"; whois -h whois.arin.net 212.146.69.237 ; echo "\n 44rEnd whois record -- \n"
sh -c echo "\n 44rBegin whois record -- \n"; whois -h whois.arin.net 77.238.184.24 ; echo "\n 44rEnd whois record -- \n"
Run Code Online (Sandbox Code Playgroud)

我希望输出看起来好像使用sh -c执行的每个命令块都是按顺序执行的。相反,我的输出类似于:

44rBegin whois record

44rBegin whois record

44rBegin whois record

whois1 output

44rEnd whois record --

whois2 output

44rEnd whois record --

whois3 output

44rEnd whois record --
Run Code Online (Sandbox Code Playgroud)

我该如何解决这个问题?

phe*_*mer 6

在这种特定情况下,您将传递-P 3xargs.

   -P max-procs
          Run up to max-procs processes at a time; the default is 1.  If max-procs  is
          0,  xargs  will run as many processes as possible at a time.  Use the -n op?
          tion with -P; otherwise chances are that only one exec will be done.
Run Code Online (Sandbox Code Playgroud)

因为您并行运行它们,所以它们都将同时写入它们的输出。

如果您从其他地方复制了此命令,我建议您进行研究以了解您正在复制的内容。否则会很危险。