Re: shell script question
On Sunday 12 October 2003 10:35 pm, Jeff Elkins wrote:
>On Sunday 12 October 2003 6:50 pm, Colin Watson wrote:
>>On Sun, Oct 12, 2003 at 06:37:16PM -0400, Jeff Elkins wrote:
>>> I'm trying to write a script to change spaces in a filename to the
>>> underscore character:
>>>
>>> #!/bin/sh
>>> for i in *; do
>>> if test -f $i; then
>>> mv $i `echo $i | tr '" "' '_'`
>>> fi
>>> done
>>>
>>> When run, it gives me the error "too many args in line four."
>>
>>If you find yourself writing $variable without double quotes around it
>>in shell, you should think twice to make sure that you know exactly what
>>expansion rules are going to apply to it. Usually the answer is "more
>>than you wanted".
>>
>>Try this instead (untested):
>>
>> #! /bin/sh
>> for i in *; do
>> if test -f "$i"; then
>> mv "$i" "`echo "$i" | tr ' ' _`"
>> fi
>> done
>>
>>Also consider 'set -e' just below the #! line, in case one of the mv
>>commands goes bananas for some reason.
>
>That worked great! - God, I love this list :)
In the same vein, I'm working through a list of mp3s where some of them need
re-encoding. First, I convert them to wavs with this script fragment:
mpg123 -b 10000 -s "$1" | sox -t raw -r 44100 -s -w -c2 - "$2"
where I feed the script $1 (input) and $2 (output). In every case, $1 and $2
are the same, except for $2 I want the output filename to have a .wav
extension.
Could I alter the script to eliminate entering $2, since the basic filename is
the same?
Thanks again,
Jeff Elkins
Reply to: