LinuxQuestions.org
Latest LQ Deal: Latest LQ Deals
Go Back   LinuxQuestions.org > Forums > Linux Forums > Linux - General
User Name
Password
Linux - General This Linux forum is for general Linux questions and discussion.
If it is Linux Related and doesn't seem to fit in any other forum then this is the place.

Notices


Reply
  Search this Thread
Old 07-14-2011, 07:56 AM   #1
paziulek
Member
 
Registered: Mar 2009
Location: N.C.
Distribution: "gentoo|debian|arch"
Posts: 106

Rep: Reputation: 11
Question >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

Last edited by paziulek; 07-14-2011 at 08:15 AM.
 
Old 07-14-2011, 08:33 AM   #2
MTK358
LQ 5k Club
 
Registered: Sep 2009
Posts: 6,443
Blog Entries: 3

Rep: Reputation: 723Reputation: 723Reputation: 723Reputation: 723Reputation: 723Reputation: 723Reputation: 723
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
 
1 members found this post helpful.
Old 07-14-2011, 08:37 AM   #3
Guttorm
Senior Member
 
Registered: Dec 2003
Location: Trondheim, Norway
Distribution: Debian and Ubuntu
Posts: 1,453

Rep: Reputation: 446Reputation: 446Reputation: 446Reputation: 446Reputation: 446
http://www.linuxquestions.org/questi...script-800450/
 
1 members found this post helpful.
Old 07-14-2011, 08:52 AM   #4
paziulek
Member
 
Registered: Mar 2009
Location: N.C.
Distribution: "gentoo|debian|arch"
Posts: 106

Original Poster
Rep: Reputation: 11
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

Last edited by paziulek; 07-14-2011 at 08:54 AM.
 
  


Reply


Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
for loop or while loop to read the fields of a file.. visitnag Linux - Newbie 10 09-02-2010 08:47 PM
while READ loop problem caps_phisto Linux - General 3 02-05-2007 09:58 AM
How do I keep an executable from terminates after the initiating shell terminates mr.v. Linux - Newbie 8 01-20-2007 02:47 AM
display full line instead of single word in for loop ken734 Linux - Newbie 3 08-15-2003 02:35 PM

LinuxQuestions.org > Forums > Linux Forums > Linux - General

All times are GMT -5. The time now is 08:06 PM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration