Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

running bash pipe commands in background with & ampersand

Tags:

bash

shell

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 ...

like image 347
Tathagata Avatar asked Jul 12 '11 14:07

Tathagata


3 Answers

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
like image 100
Fred Foo Avatar answered Oct 22 '22 21:10

Fred Foo


You can include the & in braces:

time for i in `ls /tmp/chunk*`; do
  {(cat $i | tr ' ' '\n' | sort | uniq > /tmp/line${i:10}) &};
done
like image 29
IcanDivideBy0 Avatar answered Oct 22 '22 21:10

IcanDivideBy0


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

for i in /tmp/chunk*; do tr ' ' '\n' <$i | sort -u > /tmp/line${i:10}& done
like image 34
pixelbeat Avatar answered Oct 22 '22 19:10

pixelbeat