3

inside ~/mp3 I've some mp3 files.

My script:

#!/bin/bash
br=80
for a in $1*.{wav,mp3} ; 
do ffmpeg -i "$a" -ar 44100 -ab $br "$br""_tmp/${a%.*} ["$br"].mp3" ; 

with $1 add the path:

myscript.sh /home/$USER/mp3/

but I've the error:

/home/$USER/mp3/*.mp3: No such file or directory

so, the script does not run. Runs only when I execute the script inside ~mp3 dir.

3

Put at the top script :

shopt -s nullglob

that way, if there's no mp3 like in your case (I guess), there will be no error.

When there's no mp3 file matching the wildcard, the shell take *.mp3 literally.

2

This happens because there are no .mp3 files in the directory. If you executed this look in a directory without .mp3 and .wav files, you would get 2 errors, one for *.mp3 and a second for *.wav files.

A simple solution to make this work even when .mp3 or .wav are missing, is to add a test if the file exists:

for a in $1*.{wav,mp3}; do
    test -f "$a" || continue
    ffmpeg -i "$a" -ar 44100 -ab $br "$br""_tmp/${a%.*} ["$br"].mp3"
done

That said, @sputnick's solution with shopt -s nullglob is better than this.

You should also quote $1, and you can simplify the quotes in the ffmpeg line:

for a in "$1"*.{wav,mp3}; do
    test -f "$a" || continue
    ffmpeg -i "$a" -ar 44100 -ab $br "${br}_tmp/${a%.*} [$br].mp3"
done
  • janos thanks for your reply. My problem is execution of script: does not run. There're many files in "mp3" dir. – watchmansky Dec 25 '14 at 21:25
  • /home/wms/tmp/./myscript.sh /home/wms/tmp/mp3/ and does not run :-( – watchmansky Dec 25 '14 at 21:32
  • Jason: {a%.*} keep whole path, so ouput path will be: 32k_tmp/home/wms/tmp/mp3/audio1 [32k].mp3: No such file or directory. So I've to remove it. – watchmansky Dec 25 '14 at 21:42

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.