[Gllug] batch convert

Tethys tet at accucard.com
Wed Jan 29 09:48:59 UTC 2003


Jonathan Harker writes:

>Cool, but if I needed to do something more tricky, how do I use the
>
>for f in $files; do ... done
>
>trick with files with spaces?

It depends. If you can match the files with a simple glob expression,
then do so directly in the for loop, and just quote the loop variable
when you dereference it (as I've mentioned many times before, you should
always be doing that anyway!). I'm assuming you're resampling to reduce
the size of your MP3s to fit on a portable player or something[1]. Try:

	for file in *.mp3
	do
		newfile=`echo "$file" | sed 's/.mp3$/_small.mp3/'`
		mpg321 --stdout "$file" | bladeenc -mono -32 STDIN "$newfile"
	done

If you need to prepare the list of files in advance, you need to employ
a bit more cunning. The easiest way (in ksh and bash, at least -- not
supported in bourne shell) is to put the filenames into an array. You
can then query the array length, and process each array element in
turn:

	count=0
	arraylen=${#files[@]}
	while [ $count -lt $arraylen ]
	do
	    process_file ${files[$count]}
		count=$((count + 1))
	done

Note, however, that if you want your script to be portable, you have
to take care. Arrays are limited to 1024 elements in ksh, for example
(in bash, they're limited only by available memory, but bash isn't
always available). If you're processing huge numbers of files, then
you can write the filenames to a temporary file, and process it one
line at a time. It's slow and klunky, but it works and I've been in
situations where it's been the only option:

	filenames=/my/file
	tmpfile="$filenames.$$"

	while [ -s "$filenames" ]
	do
		file=`head -1 "$filenames"`
		process_file "$file"
		tail +2 "$filenames" > "$tmpfile"
		mv "$tmpfile" "$filenames"
	done

Tet

-- 
Gllug mailing list  -  Gllug at linux.co.uk
http://list.ftech.net/mailman/listinfo/gllug




More information about the GLLUG mailing list