使用 & 符号在后台运行 bash 管道命令

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/6666245/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-18 00:21:31  来源:igfitidea点击:

running bash pipe commands in background with & ampersand

bashshell

提问by Tathagata

time for i in `ls /tmp/chunk*`; do (cat $i | tr ' ' '\n' | sort | uniq > /tmp/line${i:10}) & ;done
bash: syntax error near unexpected token `;'

Whats the syntax error in the above command? I also tried using {}and ended the piped commands with ;. But same error shows up ...

上面命令中的语法错误是什么?我还尝试使用{}并以;. 但同样的错误出现...

回答by Fred Foo

You should put the &inside the (), if you want to run all the jobs in parallel in the background.

你应该把&里面的(),如果你要并行运行的所有作业的背景。

time for i in `ls /tmp/chunk*`; do
  (cat $i | tr ' ' '\n' | sort | uniq > /tmp/line${i:10} &)
done

回答by IcanDivideBy0

You can include the & in bracers:

您可以在括号中包含 & :

time for i in `ls /tmp/chunk*`; do
  {(cat $i | tr ' ' '\n' | sort | uniq > /tmp/line${i:10}) &};
done

回答by pixelbeat

& is a separator and so is redundant with ; I.E. remove the final ;

& 是一个分隔符,因此与 ; 是多余的 IE 删除最后;

for i in /tmp/chunk*; do tr ' ' '\n' <$i | sort -u > /tmp/line${i:10}& done