8

I am trying to parse some log files using a script, and since all of them can be parsed independently, I am trying to run them in the background. I am using the following command but I get the syntax error stated right after:

for k in {2..3}; do for i in {1..3}; do for j in {1..3}; do ./run_parser.sh ${k} ${i} ${j} > parse_${k}${i}${j}.log 2>&1 &; done; done; done
-bash: syntax error near unexpected token `;'

Although if I run the run_parser.sh script individually one by one without the loop, it runs fine. Something like:

./run_parser.sh 2 1 2 > parse_212.log 2>&1 &

works perfectly fine.

Also, I previously tried :

for k in {2..3}; do for i in {1..3}; do for j in {1..3}; do ./run_parser.sh ${k} ${i} ${j} & > parse_${k}${i}${j}.log ; done; done; done

which also worked but output redirection was not working and everything was being printed to screen.

0

2 Answers 2

8

You don't need a ; after a &, the & itself acts as a command separator. See the "List of commands" section in the Bash reference manual .

This will execute three concurrent ping:

ping -c 3 askubuntu.com & ping -c 3 askubuntu.com & ping -c 3 askubuntu.com &
2
  • This works too.
    – Wajahat
    Commented Mar 22, 2017 at 10:11
  • 3
    No, that's the other one (adding parentheses to compensate for a semicolon that shouldn't be there in the first place) that "works too". :)
    – xenoid
    Commented Mar 22, 2017 at 13:47
6

You should place the background command in () like this:

for k in {2..3}; do for i in {1..3}; do for j in {1..3}; do (./run_parser.sh ${k} ${i} ${j} > parse_${k}${i}${j}.log 2>&1 &); done; done; done
1
  • This spawn an additional but rather pointless sub-shell for every inner loop execution. Commented Apr 2, 2017 at 7:24

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .