[Techtalk] Question: sed s option in a shell script

Sabine Konhaeuser sjmk at gmx.net
Tue Mar 15 11:22:17 EST 2005


On Sunday 13 March 2005 23:38, Devdas Bhagat wrote:
> On 13/03/05 20:41 -0800, Sabine Konhaeuser wrote:
> > Hi Everyone,
> >
> > I'm in the process of digitizing my record collection. I will
> > save the recorded songs as flac and mp3 files. The flac command
> > flac -8 *.wav loops through the entire directory and creates the
> > flac files. So far so good. Lame however doesn't seem to do this.
> > So I wrote a small script that loops through the directory,
> > fetching every wav file and then with sed replace the ending wav
> > with mp3. This is the script:
> >
> > **** script starts here ****
> >
> > OLDSUFFIX=wav
> > NEWSUFFIX=mp3
> > for FILE in *."$OLDSUFFIX"
> > do
> >      NEWNAME=´"$FILE" | sed -e "s/$OLDSUFFIX/$NEWSUFFIX/"´
> >      lame --preset standard "$FILE" "$NEWNAME"
> > done
> >
> > **** script ends here ****
>
> #!/bin/bash
>
> #This should work wiht /bin/sh but I haven't tested it with that
> OLDSUFFIX=wav
> NEWSUFFIX=mp3
> for FILE in *.${OLDSUFFIX}
> do
>
> # Run the command in backticks to get the desired output.
> # sed -e takes double quotes intead of single quotes because we
> want the # shell to interpret the variables.
> # The $ at the end  of the regular expression forces a change only
> a the # end. Eg: A file named waves.wav
>
> 	NEWNAME=`echo ${FILE}|sed -e "s/${OLDSUFFIX}$/${NEWSUFFIX}/"`
> 	lame --preset standard ${FILE} ${NEWNAME}
> done
>
> <snip>
>
> > The script itself does work, it loops through all the wav files
> > and creates the mp3 files. But the file name is not file_name.mp3
> > but file_name.wav.mp3. Not that this is a big deal, but the wav
> > in the name bothers me and I'd like to have it removed
> > automatically. Right now I rename the files by hand.
>
> There is also rename(1) for bulk renaming.
>
> Devdas Bhagat

So this works:
<snip>

#! /bin/sh
OLDSUFFIX=wav
NEWSUFFIX=mp3
for FILE in *.${OLDSUFFIX}
do
    NEWNAME=`echo ${FILE}|sed -e "s/${OLDSUFFIX}$/${NEWSUFFIX}/"`
    lame --preset standard "$FILE" "$NEWNAME"
done

</snip>

The missing echo and the wrong quotes probably threw sed off. 
The part 'lame --preset standard ${FILE} ${NEWNAME}' causes a problem 
with lame (lame: command not found).

What is the difference between "$FILE" and {$FILE}? I found both used 
when I was searching for sed. But no explanation why one version was 
chosen over the other.

Thanks everyone for those great tips. There is always something new to 
learn. Although I'm using Linux as my main desktop for a couple of 
years now (since SuSE 7.2), I never really delved into shell 
scripting.

Cheers,
-- 
Sabine


More information about the Techtalk mailing list