LinuxQuestions.org
Welcome to the most active Linux Forum on the web.
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Linux Forums > Linux - Newbie
User Name
Password
Linux - Newbie This Linux forum is for members that are new to Linux.
Just starting out and have a question? If it is not in the man pages or the how-to's this is the place!

Notices


Reply
  Search this Thread
Old 10-10-2005, 01:35 PM   #1
JimBass
Senior Member
 
Registered: Oct 2003
Location: New York City
Distribution: Debian Sid 2.6.32
Posts: 2,100

Rep: Reputation: 49
Shell scripting question about cut


Hello all!

I am piecing together a little script to convert all .wav files in all directories below the current one, and convert them into mp3 files. As my shell scripting sucks, I need a little help with the cut command. I have pieced together this script from some found here on linuxquestions:
Code:
#!/bin/bash

 find . -name *.wav > wavlist
 for i in $(cat wavlist)
 do
 ORIG=$j
 DEST=$(ls $j | cut -d "." -f 1).mp3
 lame -h -b 192 $ORIG $DEST
 echo "done converting $j, removing.."
 rm $j
 rm wavlist
 done
The problem is that the cut command removes everything past the period (.), but my list of files from line 2 has things in the form:
Code:
./directory/another/another/file.wav
I want it ot cut at the period before wav, but the script correctly cuts at the period that starts the line, and that doesn't pass the correct info to the lame encoder. I see that the -d "." option in cut uses a period as a delimiter, but how can I get it to perform the cut at the second period rather than the first?

Thanks for the help!

Peace,
JimBass
 
Old 10-10-2005, 01:57 PM   #2
Vookimedlo
Member
 
Registered: Jul 2004
Location: Czech Republic - Roudnice nad Labem
Distribution: Debian
Posts: 253

Rep: Reputation: 34
try basename (man basename)

Code:
DEST=$(basename $j .wav).mp3
 
Old 10-10-2005, 02:21 PM   #3
JimBass
Senior Member
 
Registered: Oct 2003
Location: New York City
Distribution: Debian Sid 2.6.32
Posts: 2,100

Original Poster
Rep: Reputation: 49
Thanks, but basename doesn't do what I wanted. It dtrips the extension nicely, but it also strips the leading directories, which I need. Printing the output of the script with your basename line gives this:
Code:
jim@musicbox:~/Tori_Amos$ ./script
 .wav.mp3
 .wav.mp3
 .wav.mp3
 .wav.mp3
 .wav.mp3
 .wav.mp3
 .wav.mp3
 .wav.mp3
 .wav.mp3
 .wav.mp3
 .wav.mp3
 .wav.mp3
 .wav.mp3
 .wav.mp3
 .wav.mp3
 .wav.mp3
 .wav.mp3
 .wav.mp3
Thanks though, if it just stripped but left the directory info, it would be ideal.

I see that you can specifiy a number of chars for cut to cut into, is there a switch to make it cut 3 chars from the end of a line?

Peace,
JimBass

Last edited by JimBass; 10-10-2005 at 02:34 PM.
 
Old 10-10-2005, 02:36 PM   #4
anomie
Senior Member
 
Registered: Nov 2004
Location: Texas
Distribution: RHEL, Scientific Linux, Debian, Fedora
Posts: 3,935
Blog Entries: 5

Rep: Reputation: Disabled
Try
Code:
DEST=.$( ls $i | cut -d '.' -f '2' ).mp3
P.S. Your iterator is $i. Why are you referring to $j?
 
Old 10-10-2005, 02:40 PM   #5
anomie
Senior Member
 
Registered: Nov 2004
Location: Texas
Distribution: RHEL, Scientific Linux, Debian, Fedora
Posts: 3,935
Blog Entries: 5

Rep: Reputation: Disabled
One more thing: It would be wise to add an exit status check after the lame program runs so that you're not deleting files that didn't get converted.

Code:
lame -h -b 192 $ORIG $DEST
if [[ $? -ne 0 ]] 
  then echo "Did not convert. Bailing out now..."
  exit 1
fi
Or some such.
 
Old 10-10-2005, 03:05 PM   #6
JimBass
Senior Member
 
Registered: Oct 2003
Location: New York City
Distribution: Debian Sid 2.6.32
Posts: 2,100

Original Poster
Rep: Reputation: 49
The iterator had been i, then I mostly converted it to j, and obviously missed an important one! Thanks for the help! It works well now. I added your saftey bailout, as that fdoes seem like a fine idea. Now the only issue is setting some type of control so that it only tries to remove wavlist.txt once, rather than each time it goes through te $j loop.

Code:
#!/bin/bash

 find . -name *.wav > wavlist.txt
 for j in $(cat wavlist.txt)
 do
 ORIG=$j
 DEST=.$(ls $j | cut -d "." -f 2).mp3

 lame -h -b 192 $ORIG $DEST
        if [[ $? -ne 0 ]]
        then echo "Did not convert. Bailing out now..."
        exit 1
        fi
 echo "done converting $j, removing.."
 rm $j
 rm wavlist.txt
 done
Thanks soo much for everyone's help!

Peace,
JimBass
 
Old 10-10-2005, 03:22 PM   #7
anomie
Senior Member
 
Registered: Nov 2004
Location: Texas
Distribution: RHEL, Scientific Linux, Debian, Fedora
Posts: 3,935
Blog Entries: 5

Rep: Reputation: Disabled
Quote:
Now the only issue is setting some type of control so that it only tries to remove wavlist.txt once, rather than each time it goes through te $j loop.
Put the
Code:
rm wavlist.txt
on the line after
Code:
done
(i.e. after the loop is finished).
 
Old 10-10-2005, 03:26 PM   #8
JimBass
Senior Member
 
Registered: Oct 2003
Location: New York City
Distribution: Debian Sid 2.6.32
Posts: 2,100

Original Poster
Rep: Reputation: 49
I had thought done was for the entire script, not just the loop. Thanks again man! I really need to get my scripting together. Thank you for the insight, it helps me to understand what is happening and why.

Peace,
JimBass
 
Old 10-20-2005, 11:24 PM   #9
jschiwal
LQ Guru
 
Registered: Aug 2001
Location: Fargo, ND
Distribution: SuSE AMD64
Posts: 15,733

Rep: Reputation: 682Reputation: 682Reputation: 682Reputation: 682Reputation: 682Reputation: 682
Consider using variable substitution.

Something like:

lame -h -b 192 "$j" "${j%.wav}.mp3"

If you might have a mixture of .wav and .WAV files,
lame -h -b 192 "$j" ".${j%.[wW][aA][vV]}.mp3"

This method is closer to your cut command:
lame -h -b 192 "$j" ".${j%.*}.mp3"

I put quotes around the variables in the case when a variable has either whitespace or a special meta character. You may also want to change the value of the IFS variable to the \n character.

Otherwise, a space one of the lines would result in $j being assigned to only the first part of the line instead of the entire line.

Another small item, just because one conversion failed, I don't think that would be a valid reason to abort the entire loop. Perhaps this would work:

lame -h -b 192 "$j" ".${j%.*}.mp3" || rm .${j%.*}.mp3 ; echo "could not convert $j"

Last edited by jschiwal; 10-20-2005 at 11:26 PM.
 
  


Reply



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 Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
Shell scripting question. dragin33 Linux - General 2 08-11-2004 05:17 PM
Shell Scripting Question Onyx^ Linux - General 5 04-27-2004 10:37 AM
Shell Scripting Question b_vasu Linux - Newbie 1 11-21-2003 02:10 PM
Shell Scripting Question jester_69 Programming 13 11-05-2003 06:55 PM
Shell Scripting Question chrisk5527 Linux - General 12 07-09-2003 03:36 PM

LinuxQuestions.org > Forums > Linux Forums > Linux - Newbie

All times are GMT -5. The time now is 09:36 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