Issue
I do the following at my Mac terminal:
sudo for i in `seq 0 9`; do nohup my_command > log_$i.txt & done
but I receive the following error:
-bash: syntax error near unexpected token `do'
What's the problem and how can I fix it?
P.S.
I tested some of the suggestions of people here and here they are:
1)
(base) user@SERVER:/directory$ sudo for i in `seq 0 9`; do nohup my_command > log_$i.txt &; done
-bash: syntax error near unexpected token `do'
2)
(base) user@SERVER:/directory$ sudo for i in `seq 0 9`; do nohup my_command > log_$i.txt ; done
-bash: syntax error near unexpected token `do'
3)
(base) user@SERVER:/directory$ sudo for i in `seq 0 9`; do nohup my_command > log_$i.txt done
-bash: syntax error near unexpected token `do'
4)
(base) user@SERVER:directory$ sudo bash -c for i in `seq 0 9`; do nohup my_command > log_$i.txt & done
-bash: syntax error near unexpected token `do'
Solution
Running the for
via sudo doesn't work as sudo expects a command. You can instead run the loop via bash:
sudo bash -c 'for i in {0..9}; do nohup command > log_$i.txt & done'
You wouldn't need to use seq
command as bash has the {0..9}
to support "range" loops.
See bash job control for more info on &
(which puts the "job" - the command you run - in the background).
Answered By - P.P