I created a shell script to convert all wave files to mp3 files. I use Ubuntu 18.04, FFmpeg was installed using apt, and I run my script in bash.
My script:
#!/bin/bash
ls *.wav | while read file
do
echo $file
ffmpeg -i "$file" -codec:a libmp3lame -b:a 192k "${file%.*}.mp3"
done
The target files are followings: (include space characters. The real filenames are longer, but I simplified the filenames. Also in this case, the problem occurs)
% ls *.wav
'01 A.wav' '02 A.wav' '03 A.wav'
The problem is that sometimes $file in the loop is blank or a part of filename strangely ('echo $file' shows that), and ffmpeg says '[broken filename]: No such file or directory'. I confirmed the following things.
- When I comment out the ffmpeg line, 'echo' shows what I expected. ($file not broken)
- When I replace ffmpeg to a similar command like 'lame', it works. ($file not broken)
- When I replace 'ls *.wav | while read file' to 'for file in *.wav', it works. ($file not broken)
So, only when I use combination of 'ls' and 'ffmpeg', $file is broken. What's going on? Or do I misunderstand something?