Hello,
Can anybody help me please with some coding of a script?
I regularly download mp3 files using get_iplayer which I then play in my Humax set top box. I find the Humax will display an image for the file if the image is embedded within the mp3 file.
So I wrote a script to go through a folder and add the relevant image file. Here's my script :-
for FILE in *.mp3 do bname=$(basename "$FILE") extension="${bname##*.}" filenamewoext="${bname%.*}" imagefile="${filenamewoext}.png" echo adding cover image ... eyeD3 --add-image $imagefile:FRONT_COVER:Picture "$FILE"
echo Stripping any version 1 tags id3v2 --delete-v1 "$FILE" # Now for a tidy up and delete the original image file # search and add image img=$(id3v2 -l "$FILE" | grep -i "APIC") if $img =""; then echo "Adding the image failed" exit 1 else rm -f $imagefile fi
done
The problem comes in the tidy up section. I don't want to delete the image file if adding it has failed but even though I'm getting an error "mp3image.sh: line 47: APIC: command not found", the image is still being deleted.
I've tried various ways of writing this code and all have failed. Writing code is far from my strong point so would appreciate some pointers as to where I'm going wrong particularly since if I type out the img=.. command in a console, it seems to work and produces a line like this :- APIC (Attached picture): (Picture)[, 3]: image/png, 58900 bytes so adding the image did work.
-----BEGIN PGP SIGNED MESSAGE----- Hash: SHA256
On 07/09, Chris Walker wrote:
for FILE in *.mp3 do bname=$(basename "$FILE") extension="${bname##*.}" filenamewoext="${bname%.*}" imagefile="${filenamewoext}.png" echo adding cover image ... eyeD3 --add-image $imagefile:FRONT_COVER:Picture "$FILE"
echo Stripping any version 1 tags id3v2 --delete-v1 "$FILE" # Now for a tidy up and delete the original image file # search and add image img=$(id3v2 -l "$FILE" | grep -i "APIC") if $img =""; then echo "Adding the image failed" exit 1 else rm -f $imagefile fi
done
The problem comes in the tidy up section. I don't want to delete the image file if adding it has failed but even though I'm getting an error "mp3image.sh: line 47: APIC: command not found", the image is still being deleted.
You can use the special bash variable `$?` to get the return code of the last command that run.
This means you can do something like:
img=$(id3v2 -l "$FILE" | grep -i "APIC")
if [ $? -ne 0 ]; then # clean up from failure fi
Steve
On 2015-09-07 11:15, Steve Engledow wrote:
You can use the special bash variable `$?` to get the return code of the last command that run.
That or adding "set -e" as one of the first lines in the script. Setting this option causes the shell to stop executing the script on the first error (i.e. the first non-zero return code).
Thomas
On Mon, 7 Sep 2015 11:15:32 +0100 Steve Engledow steve@offend.me.uk wrote:
-----BEGIN PGP SIGNED MESSAGE----- Hash: SHA256
On 07/09, Chris Walker wrote:
for FILE in *.mp3 do bname=$(basename "$FILE") extension="${bname##*.}" filenamewoext="${bname%.*}" imagefile="${filenamewoext}.png" echo adding cover image ... eyeD3 --add-image $imagefile:FRONT_COVER:Picture "$FILE"
echo Stripping any version 1 tags id3v2 --delete-v1 "$FILE" # Now for a tidy up and delete the original image file # search and add image img=$(id3v2 -l "$FILE" | grep -i "APIC") if $img =""; then echo "Adding the image failed" exit 1 else rm -f $imagefile fi
done
The problem comes in the tidy up section. I don't want to delete the image file if adding it has failed but even though I'm getting an error "mp3image.sh: line 47: APIC: command not found", the image is still being deleted.
You can use the special bash variable `$?` to get the return code of the last command that run.
This means you can do something like:
img=$(id3v2 -l "$FILE" | grep -i "APIC") if [ $? -ne 0 ]; then # clean up from failure fi
Steve
Thanks Steve, and also to Thomas for his suggestion.