Take the 2-minute tour ×
Unix & Linux Stack Exchange is a question and answer site for users of Linux, FreeBSD and other Un*x-like operating systems.. It's 100% free, no registration required.

I recently wrote the following bash function:

makeaudiobook () {
count=1
almbumartist=$2
for f in $1; do
        preprocess $f > $f-preprocessed
        text2wave $f-preprocessed -o $f.wav
        lame -b 16 --tt $f --ta $albumartist --tl $albumartist --tn $count $f.wav $f.mp3
        rm -rf $f.wav $f-preprocessed
        count=$(($count+1))
done
}

It does not work as expected. It does not iterate over all of the files that fit the glob expression I specify in $1. As a one-liner, this would work. An in fact, I made this into a function because I was tired of writing it out as a one-liner. But as a bash function, it doesn't work. So what do I need to do to make it work?

I want to be able to call it on things like the output of split, like,

me@localhost$~ makeaudiobook x?? haskell

...where x?? matches xaa, xab, xac, etc. Like I said, as a one-liner this is fine. So what do I need to do to pass this glob expression to $1?

share|improve this question
add comment

1 Answer

up vote 10 down vote accepted

x?? is expanded at the time of function call. So your function is already called with xaa xab xac... .

The simplest way would be to change the ordering of your parameters:

makeaudiobook () {
count=1
almbumartist=$1
shift
for f in "$@"; do
        preprocess "$f" > "$f"-preprocessed
        text2wave "$f"-preprocessed -o "$f".wav
        lame -b 16 --tt "$f" --ta "$albumartist" --tl "$albumartist" --tn "$count" "$f".wav "$f".mp3
        rm -rf "$f".wav "$f"-preprocessed
        count=$(($count+1))
done
}

(look how I put " around every variable - this is to prevent bad things from happening if you have whitespace in your file names, what often happens with audio files)

and call it with makeaudiobook haskell x??, i. e. with rotated arguments.

share|improve this answer
    
it works! it works! –  ixtmixilix Feb 28 '13 at 9:07
1  
Nit: in "$@" is redundant. for <...>; do by default loops over the parameter array. –  l0b0 Feb 28 '13 at 10:23
2  
@l0b0 Right (just forgot about that), but IMHO explicit is better than implicit. –  glglgl Feb 28 '13 at 10:51
add comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.