Linux - NewbieThis 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
Welcome to LinuxQuestions.org, a friendly and active Linux Community.
You are currently viewing LQ as a guest. By joining our community you will have the ability to post topics, receive our newsletter, use the advanced search, subscribe to threads and access many other special features. Registration is quick, simple and absolutely free. Join our community today!
Note that registered members see fewer ads, and ContentLink is completely disabled once you log in.
If you have any problems with the registration process or your account login, please contact us. If you need to reset your password, click here.
Having a problem logging in? Please visit this page to clear all LQ-related cookies.
Get a virtual cloud desktop with the Linux distro that you want in less than five minutes with Shells! With over 10 pre-installed distros to choose from, the worry-free installation life is here! Whether you are a digital nomad or just looking for flexibility, Shells can put your Linux machine on the device that you want to use.
Exclusive for LQ members, get up to 45% off per month. Click here for more info.
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, 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:
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
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.
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:
LinuxQuestions.org is looking for people interested in writing
Editorials, Articles, Reviews, and more. If you'd like to contribute
content, let us know.