Mailinglist Archive: opensuse (2772 mails)

< Previous Next >
Re: [opensuse] Another Bash scripting question
  • From: Sam Clemens <clemens.sam1@xxxxxxxxx>
  • Date: Sun, 18 May 2008 20:44:05 -0400
  • Message-id: <4830CD55.2030901@xxxxxxxxx>
Rodney Baker wrote:
Hi all.

I have a few directories containing wav files that have been ripped from CDs and I'm trying to write a script to invoke sox to convert them to mp3 format.

The filenames have whitespaces and hyphens in them e.g. 'artist - 01 - title with many words.wav'. I tried the following script but I'm obviously doing something wrong...





---------------------------------------------------------------------------------------------------------
#!/bin/bash

for file in `find * -print0 | xargs --null`

do {
INFILE=$file
OUTFILE=${file/%wav/mp3}
^^^^^^^^^^^^^^^^^
WHAT?????


Clue for the clueless...

If you had bothered to COMMENT YOUR CODE, you would
have discovered this error right off the bat.


And if you get in the habit of writing comments BEFORE
YOU WRITE THE CODE, you'll discover that your rate of
bug production drops drastically (both in bugs/hour
and also in bugs/lines of code).

echo "$OUTFILE"

# if [ ! -e "$file" ]
# then
# echo "$file does not exist."; echo

# else

# sox "$file" "$OUTFILE"

# fi

# if [ $? -eq 0 ]
# then
# rm -f "$file"
# fi
}
done

exit 0
-------------------------------------------------------------------------------------------------

In its current form each word of each filename is treated separately i.e. xargs --null appears to have no effect at all - it is interpreting whitespace as a delimiter.

If I modify the first line as follows:

for file in "`find * -print0 | xargs --null`"

or
for file in "$(find * -print0 | xargs --null)"

then I get the entire directory listing (i.e. all the filenames) concatenated into a single parameter with only the last file extension replaced.

What have I missed here? I'm assuming it is something to do with the pipe or redirection via xargs that is not working as expected.
BTW, I also tried this:

for file in $(xargs --null -- < `find * -print0`)

but then I get an "ambiguous redirect" error.

Using this:

for file in `xargs --null -- < `find * -print0``

gives the following errors:
command substitution: line 26: syntax error near unexpected token `newline'
command substitution: line 26: `xargs --null -- < '

but then produces the output I'm expecting (i.e. a list of the output file names (keeping in mind the actual processing part of the script is currently commented out) with the exception that "find" is the first entry in the list and "-print0" is the last entry in the list; somehow the variable $OUTFILE appears to be getting the values "find" and "-print0" at the beginning and end of the loop.

I seem to be just going around in circles here. Any suggestions will be greatly appreciated.

Thanks in advance,

--
To unsubscribe, e-mail: opensuse+unsubscribe@xxxxxxxxxxxx
For additional commands, e-mail: opensuse+help@xxxxxxxxxxxx

< Previous Next >
Follow Ups
References