model 14
Posts: 15
Joined: Thu Aug 22, 2019 11:52 am

Why am I getting this syntax error?

Thu Oct 17, 2019 9:17 pm

When I run the following from the terminal of Raspbian in my Pi 4B I get a "syntax error near unexpected token 'do'":

sudo for f in *.flac; do ffmpeg -i "$f" "${f%.flac}.wav"; done
rm *.flac

This batch file statement comes from the GPIO Music Box example. Its purpose is to convert .flac files to .wav.

Any help is appreciated. Thanks.

User avatar
Paeryn
Posts: 2704
Joined: Wed Nov 23, 2011 1:10 am
Location: Sheffield, England

Re: Why am I getting this syntax error?

Thu Oct 17, 2019 9:44 pm

model 14 wrote:
Thu Oct 17, 2019 9:17 pm
When I run the following from the terminal of Raspbian in my Pi 4B I get a "syntax error near unexpected token 'do'":

sudo for f in *.flac; do ffmpeg -i "$f" "${f%.flac}.wav"; done
rm *.flac

This batch file statement comes from the GPIO Music Box example. Its purpose is to convert .flac files to .wav.

Any help is appreciated. Thanks.
Get rid of the sudo at the start, there shouldn't be any reason to run that line as root.

As to why you get a syntax error, sudo takes the first argument as a command to run and the the rest up to the semi-colon (end of current command) as its arguments. for is not a command that sudo can run, it is a bash internal. Bash never sees the for as a loop construct since it was an argument to sudo so when it sees the next command, do, it throws an error because do isn't valid without (in this case) a corresponding for.
She who travels light — forgot something.

model 14
Posts: 15
Joined: Thu Aug 22, 2019 11:52 am

Re: Why am I getting this syntax error?

Fri Oct 18, 2019 12:03 am

You solved my issue, thank you. I still needed sudo because these are write protected files, so I moved it in front of ffmpeg and it worked fine.

User avatar
Paeryn
Posts: 2704
Joined: Wed Nov 23, 2011 1:10 am
Location: Sheffield, England

Re: Why am I getting this syntax error?

Fri Oct 18, 2019 1:36 am

model 14 wrote:
Fri Oct 18, 2019 12:03 am
You solved my issue, thank you. I still needed sudo because these are write protected files, so I moved it in front of ffmpeg and it worked fine.
I would have thought that ffmpeg is creating new files rather than overwriting current files, but if you need to overwrite write-protected files you should really either write-enable or delete them first (and write-protect them afterwards if you need that). Using sudo just to bypass file protection isn't something you should be doing, you risk getting in to a habit of using sudo for all manner of things and any files created are owned by root so will need the use of sudo later on to change or delete them. Plus you loose pretty much any protection against accidentally modifying system files.

The use of sudo should be restricted to system administration or to cases where there is no other way around it, using it to overrule file protection is an accident waiting to happen (some people don't get this until they accidentally wipe the entire file system when they meant to just empty a directory and loose everything).
She who travels light — forgot something.

Return to “Raspbian”