1

I use a utility called mp3wrap which combines multiple mp3s into one. Say I have a directory "album 1" containing three files "track 1.mp3", "track 2.mp3" and "track 3.mp3". I will run the following:

mp3wrap album\ 1.mp3 album\ 1/*.mp3

which will combine all three tracks in the "album 1" directory (the second argument) into a new file "album 1.mp3" (the first argument).

I have a bunch of directories like this, "album 2", "album 3" etc., and I would like to write a script to automate the process. I tried this:

for i in album*
do
  mp3wrap $i.mp3 $i/*.mp3
done  

but i get the error

1.mp3: No such file or directory

It seems my script expands $i to "album 1.mp3" which is passed as two separate arguments. After some searching and reading I've ended up with this:

for i in album*
do
  mp3wrap "${i}.mp3" "${i}/*.mp3"
done  

Now I don't get an error message, instead I get the "usage" page from the mp3wrap program. So clearly it's not receiving the arguments in the same way as when I run the command from the command line, and I don't know how to check what the actual command line looks like when executed by the bash script.

What am I doing wrong? Any help appreciated!

4

You are close to it. Here is the correct code:

for i in album*
do
    mp3wrap "${i}.mp3" "${i}"/*.mp3
done

Variables and arguments should almost always be quoted to avoid your first issue. On the contrary, shell globing characters like * and ? should not, else they lose their special meaning.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.