Syntax error near unexpected token 'do'

Hello all,

Please i have this command i used to zip different files in differents directory, but i have an error. Note that when i run the command in one directory it works fine.

/X5/WORK/BGH/INV/REG/pdf/SEND/BGH12523/1/*.fo
/X5/WORK/BGH/INV/REG/pdf/SEND/BGH24523/1/*.fo
/X5/WORK/BGH/INV/REG/pdf/SEND/BGH1023/1/*.fo

In the above path, i want to zip all files with extension .fo

Here is the commands, it works great.

for FN in /X5/WORK/BGH/INV/REG/pdf/SEND/BGH12523/1/*.fo; do gzip $FN;

But when trying to do it for all directories like below, i have an error

>nohup for FN in /X5/WORK/BGH/INV/REG/pdf/SEND/BGH*/1/*.fo; do gzip $FN; done &
bash: syntax error near unexpected token `do'

can somebody help?

Try enclosing the for loop in braces or parentheses (not the nohup nor the & ).

nohup bash -c 'for FN in /X5/WORK/BGH/INV/REG/pdf/SEND/BGH*/1/*.fo; do gzip $FN; done' &

Note the single quotes before the for and after the done.

Because you used nohup bash didn't recognise the for loop and tried to interpret the do gzip $FN as a separate command.

Andrew

And command arguments should be quoted.
And a test for file is appropriate.

for FN in /X5/WORK/BGH/INV/REG/pdf/SEND/BGH*/1/*.fo; do test -f "$FN" && gzip "$FN"; done
1 Like