View Full Version : Help needed with scripting
drahcir
07-29-2006, 07:45 PM
Greetings,
My sister copied a bunch of old kids music cassettes onto her computer and split them up into wav files Then dumped them onto my file sever in my music folder.
Now what I want to do is convert them all to mp3 format. I have a little script that I can run that will work in one directory however I would like to be able to make it recursive so that I don't have to go thru every folder in my music directory and run the script(there are at least 50 directories and about 15 of them contain wav files and I'm not sure which is which)
The script that i wrote is very simple
# /bin/bash
for i in *.wav;
do echo $i;
outName=$(echo $i | sed 's/\.wav/.mp3/g');
lame -h "$i" "$outName";
rm "$i";
done
I spent an afternoon working on the recursiveness(if that's a word) and had to put it aside as I got a new job. Now I find I don't have the time to poke at it to get it to work. So if any one could help me out by giving me some pointers I would really appreciate it.
Richard
bhobjj
07-29-2006, 09:49 PM
I use this to work on all files in subdirectories:
[ $# -lt 1 ] && find * -type f
It will list all the files recursive to the directory you run it in.
drahcir
07-29-2006, 10:31 PM
Thanks
Now all I have to do is figure out how to feed the output into my script. I refined it to only show files of type wav
[ $# -lt 1 ] && find * -type f -name wav
Which works well I had been playing around with ls and grep
ls -R | grep wav
But that just gives me the file names
This gives me the path. If I get some time tomorrow I may give it another go.
Richard -- they key trick to learn in programming is how to break a large problem into smaller pieces that you can develope and test independently.
Consider your problem here. You want to search for all the files which have a certain characterisitic and as these are found, process them all in the same way. Suppose you try to break this problem apart into workable pieces.
a) Find all the files which have the desired characteristic. Your output should be a list of their names (fully qualified paths).
b) Given a list of files (fully qualified paths) process them each is some specified way.
It sounds as if you have done "a", yes? (or almost have "a" working). Fine, now try to do "b". Contrary to the expectations of inexperienced programmers you will NOT save time by trying to get the whole thing working at once.
drahcir
08-01-2006, 09:43 PM
Thanks for the suggestions,
I just started a new Job as a security(computer) administrator at an insurance company and they have me working 11 hr days. (the over time is cleaning up three years of terminations) So needless to say I haven't had the time to put the pieces together yet. When things slow down I may have a little time to study up on how to get the out put of my list command
find * -type f -name wav
into my convertion script
I'll post my script if and when I get it working
danieldk
08-02-2006, 06:29 AM
By heart (sorry, for typos):
#!/bin/sh
for musicfile in $(find . -name '*.wav' -type f) ; do
lame -h "$musicfile" "${musicfile/%.wav/.mp3}"
done
The first line loops to the array of results from find (which looks for file ending in .wav of the type 'file'). The third parameter to lame is a shell pattern matching operator it says "return $musicfile, with the '.wav' string at the end replaced by '.mp3'". Shell pattern matching is really useful (and fast, because it doesn't require spawning of a new process), but most people don't know it.
Mike makes a great point, an incremental approach is good for both novice and expert programmers. First think through what data you want to loop. Make the loop, and just put an "echo $variable" in the loop to see if the loop works ok. Same thing with operations in the loop. Just echo the results of stuff to the screen before feeding them to a command that touches files. If all textual output looks ok, you can feed it to, or use it as parameters to commands. It avoids a lot of bugs, and if you write larger programs you can optimize each layer that you add before adding new layers. That's why many programmers write function and method stubs before writing all functionality. The next Perl version will even have a "yadda" operator ('...') to do just that (it does nothing but throwing an exception).
tom_servo
08-10-2006, 07:37 AM
Thank you for the example there. I had no idea that bash had "${musicfile/%.wav/.mp3}" type operations built-in. When I wrote a script for a similar job a long time ago, I used awk (then later sed) to replace the .wav with .mp3. It works, but is not a clean and manageable (nor I am sure as efficient and fast) as your code.
Alain
vBulletin® v3.8.6, Copyright ©2000-2012, Jelsoft Enterprises Ltd.