Eli Greenberg
Eli Greenberg

Reputation: 321

How do I make a bash script continue part of a loop only if there is no error in the first step of the loop?

I currently have a .command file for Mac that contains the following:

for f in ~/Desktop/Uploads/*.flv
do
     /usr/local/bin/ffmpeg -i "$f" -vcodec copy -acodec libfaac -ab 128k -ar 48000 -async 1 "${f%.*}".mp4
     rmtrash "$f"
done

How can I tell bash to only execute rmtrash if ffmpeg doesn't produce an error?

Upvotes: 1

Views: 927

Answers (1)

anubhava
anubhava

Reputation: 785146

Check for return value of ffmpeg command using $? or put && between 2 commands like this:

for f in ~/Desktop/Uploads/*.flv
do
     /usr/local/bin/ffmpeg -i "$f" -vcodec copy -acodec libfaac -ab 128k\
     -ar 48000 -async 1 "${f%.*}".mp4 && rmtrash "$f"
done

As per the bash manual:

command1 && command2
   command2 is executed if, and only if, command1 returns an exit status of zero.

Upvotes: 5

Related Questions