SlackwareThis Forum is for the discussion of Slackware Linux.
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.
hello everybody,
i take it there is no bash subforum so ill just right here.
i have installed the lame encoder, and i wanna write a bash script that reads the contents of a given folder, and runs a command for each file in it.. so basically batch process wav to mp3 conversion.
the files will obviously be *.wav files, the output should be the same file names with .mp3 instead of .wav, and the command is 'lame' along with some options....
i know little of bash in order to carry that out and im hoping that i will start understanding a few things once i see how this hopefully simple script works...
working on the aforementioned problem, the first thing to do is to actually me able to 'read' the filenames into lame
(lame works as: lame [OPTIONS] <input file> <output file>)
so i was thinking to use
Code:
ls | grep '\(.*\.\)wav.*'
to feed the output of ls (some .wav files) into grep that then looks for:
- any text (ie the filename) followed by
- a dot followed by
- the extension 'wav' followed by
- any other text that might be there after, or nothing..
the parenthesis: \( \) are there to save the matched filename followed by the dot, so as to be used with the extension mp3 later on.
im supposed to use \1 to address the filename (the matched expression in the parenthesis) but i haven't figured that out far yet. and at any rate i am not sure how i will concatenate this \1 with the word 'mp3'..
so i hope i got this right so far.. if you know of another way or can help in this way please do so
i was typing while you replied had i known of your script, i wouldn't even have bothered trying the much more complex grep /sed way... if there is any that is
i tested the script and it works fine... thank you very much!
i will try to make a script so as to allow arguments to be input when calling it.
just out of curiosity though i've reached this far:
lame [ OPTIONS ]< input file list >< output file list >
list is a temporary file created as ls > list
the thing is now lame exits saying 'excess arguments'... could it be that the whole list is dumped at once both from grep and from sed instead of one file at a time? - i think it is..
is there a way to serialize that ?
Hey! That's a great little addition. This question, has obviously been asked and answered many times, but that's the first time I've seen that included.
Could that also be..... $(basename "$FILE" .wav).mp3 ?
Hey! That's a great little addition. This question, has obviously been asked and answered many times, but that's the first time I've seen that included.
Could that also be..... $(basename "$FILE" .wav).mp3 ?
Yes, it could be. I guess I'm just a backtick kinda guy, though $( ) reads better and allows for nesting.
#!/bin/bash
LAMEOPTS=""
if [ "$#" -gt 0 ]; then
LAMEOPTS="$*"
else
LAMEOPTS="-m j -h -V 3 --vbr-new"
echo "no arguments. Assuming: $LAMEOPTS ."
fi
for FILE in $PWD/*.wav ; do
OUTNAME=`basename "$FILE" .wav`.mp3
lame $LAMEOPTS "$FILE" "$OUTNAME"
done
~
in order to account for options that the user may prefer to set.
i also placed the script in /usr/bin so everyone can access it
and i added the $PWD so as to be usable anywhere on the filesystem.
problem is that in a folder name has spaces PWD returns an incomplete path
so if , say, the path is /mnt/hd/my\ stuff
echo $PWD will return /mnt/hd/my
which is not a valid directory.... how can i overcome this?
Distribution: Slackware64 14.2 and current, SlackwareARM current
Posts: 1,644
Rep:
Quote:
Originally Posted by rickh
Hey! That's a great little addition. This question, has obviously been asked and answered many times, but that's the first time I've seen that included.
Could that also be..... $(basename "$FILE" .wav).mp3 ?
You can also use bash to do this, for example "${FILE//.wav/.mp3}" or "${FILE%%.wav}.mp3". Even more nice ways to do that
LinuxQuestions.org is looking for people interested in writing
Editorials, Articles, Reviews, and more. If you'd like to contribute
content, let us know.