LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Linux - General (https://www.linuxquestions.org/questions/linux-general-1/)
-   -   >while read< loop terminates/quits after a single cycle (https://www.linuxquestions.org/questions/linux-general-1/while-read-loop-terminates-quits-after-a-single-cycle-891693/)

paziulek 07-14-2011 07:56 AM

>while read< loop terminates/quits after a single cycle
 
Hello Everybody,

I tried to find an answer to whatever is happening with my sh, tried a few approaches, but none is working the way I planned... the loop quits with exit 0, runs once, if I comment out `ffmpeg` line, it runs N times as it should - ffmpeg does not error out other than 0


Code:

find . | grep \.mp3 | while read filename
do
  newfilename=`echo "$filename" | tr "\.mp3" "\.ogg"`
  ffmpeg -i "$filename" -acodec libvorbis -ac 2 -aq 4 -y "$newfilename"
  echo inside #4debug
done
echo outside

this sh runs once, echoes "inside" 'n "outside" once, exits with '0'... unless ffmpeg commented out... any ideas?

I also tried to feed the loop with: done < /tmp/fname.list

Thanks!

edit:
BTW, this works well - but I am very interested in the >while read< loop giving up.
Code:

for i in *.mp3; do ffmpeg -i "$i" -acodec libvorbis -ac 2 -aq 4 -y "$(basename "$i" .mp3).ogg"; done

MTK358 07-14-2011 08:33 AM

I don't know about your main problem, but I see a lot of things in your script which could be done better.

Quote:

Code:

find . | grep \.mp3 | while read filename

You're matching ".mp3" in any place in the file, not just at the end. Also, you don't even need grep:

Code:

find . -name '*.mp3' | while read filename
Quote:

Code:

newfilename=`echo "$filename" | tr "\.mp3" "\.ogg"`

First, $(command) is much better than backticks. It can't be confused with single quotes, and it nests easily. The "tr" replaces all occurences of ".mp3", even if it's not at the end. And finally, you don't even need "tr":

Code:

newfilename="${filename%.mp3}"
http://www.gnu.org/software/bash/man...Expansion.html

Guttorm 07-14-2011 08:37 AM

http://www.linuxquestions.org/questi...script-800450/

paziulek 07-14-2011 08:52 AM

Thank you MTK358 & Guttorm,

MTK358 - I completely agree, \.mp3 is not an ultimate, nor even a good approach, probably at the end of the list of possible approaches. Thank you!

Guttorm - the link you provided is the answer to a the loop quitting with ffmpeg - adding '< /dev/null' fixed its behavior:

Quote:

ffmpeg does some strange things to stdin. Try piping /dev/null into it, so it doesn't mess up the while loop.
a big YES for both!

Thank you and have a GOOD day and/or night!

paziu


All times are GMT -5. The time now is 04:09 AM.