
Oops, I have an email backlog and did not see that this was already responded to several days ago :( I agree with the suggestions made by other replies :) On 15/11/2011, David <bouncingcats@gmail.com> wrote:
On 10/11/2011, David Zuccaro <zuccaro.david@gmail.com> wrote:
Anyone like to critique this script for grammatical efficiency and correctness? In particular the line indicated. I'm guessing I can do command substitution better than this.
title="test" ls -1 *.mp3 > mp3.txt while read filename do echo $filename song=$title${filename:5:4} echo $song id3ed -isq "`echo $filename`" id3ed -q -s "`echo $song`" "`echo $filename`" <---- id3ed -isq "`echo $filename`" done < mp3.txt
Hi David
Try my version of script below. It works here on bash 4.0.35. I tested on filenames which include whitespace. As shown it will echo for verification/testing, rather than run, the id3ed commands on lines 10,11,12. When you are satisfied they are what you require, remove the first word (echo) on those 3 lines.
I don't know id3ed, but a manpage online says its -s option requires a songname, which you don't provide on lines 10 and 12, so maybe double-check that.
Compared to your script, this one avoids creating and reading the mp3.txt file. It also does not use command substitution. Maybe I've missed something, but your use of it appears unnecessary so I'm wondering why you felt you needed that?
If you are concerned that filenames might contain whitespace characters, just double-quoting the variables as I have everywhere will suffice to preserve them as a single argument and prevent bash word-splitting.
As to how it works, in line 3 the *.mp3 is unquoted, so the shell expands the glob *.mp3 into a list of filenames in the for statement. Whereas in line 4, the double-quoting prevents this, and detects the case of "no files found" by recognising that the glob did not get expanded. This test will behave incorrectly if you have an actual file named *.mp3 (asterisk_character.mp3), but that would be verry mischievous of you :-)
Please advise if If this script does not work for you, let me know what happens David
#! /bin/bash title="test" for filename in *.mp3 ; do # line 3 if [[ "$filename" == "*.mp3" ]] ; then # line 4 echo "no files found" exit fi song="$title${filename:5:4}" echo "filename=$filename, song=$song" echo id3ed -isq "$filename" # line 10 echo id3ed -q -s "$song" "$filename" # line 11 echo id3ed -isq "$filename" # line 12 done