Re: [SLE] bash Script Using Filenames with Embedded Spaces



Lucky Leavell wrote:

OK, here is my complete scipt which runs under bash on an Ubuntu 6.06 system where I have the mp32ogg utility to convert mp3 to ogg files which k3b can handle:

for i in *.mp3
do
echo $i
mp32ogg $i
done

whch works fine if the only craziness is embedded spaces in the file name; it failed when there were parentheses but, since that is rare in my situation, I can live with it. (Of course, if the single quoted $i would work there ... I'm off to try it!)

You need to use double quotes: In double quotes, shell variables are expanded, i.e., their value is inserted. In single quotes, variables are not expanded. The correct version of your script is:

for i in *.mp3
do echo "$i"
mp32ogg "$i"
done

Some might argue that the double quotes at echo are superfluous; but they are not: They are proper shell programming. If there is a file with two spaces in it, it is now output the proper way; otherwise the two spaces would have been turned into one space.

The lesson: If you have a shall variable and pass it as an argument to a command, ALWAYS include it in double quotes UNLESS if you want the value broken along its white space. Everything else is sloppy programming and might bite you some time in the future. You can do that in interactive usage, but refrain from it in shell scripts.

Oh yes, and within quotes, shell wildcards are not expanded. Therefore you must not put any quotes around *.mp3, as some posters did it.

Joachim
(who wrote his first shell script in the mid-80s :-)

-----

PS: And something fitting from my quote collection.
[You can find the reference on Google Groups. :-)]

From: Larry Wall

Drew Mills writes:
: A contest to see who could write the most useful script that could
: actually be used in the most languages *as is* [...]

I've written some scripts that work in 582 different languages,
all of them named sh.

--
=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-
Joachim Schrod Email: jschrod@xxxxxxx
Roedermark, Germany

--
Check the headers for your unsubscription address
For additional commands send e-mail to suse-linux-e-help@xxxxxxxx
Also check the archives at http://lists.suse.com
Please read the FAQs: suse-linux-e-faq@xxxxxxxx



Relevant Pages

  • Applescript and Autoformat (search and replace), Word 2004
    ... script that will take care of a recurring editing issue with nested ... quotes to double quotes, ... set oldauto2 to auto format as you type replace quotes ... set auto format as you type replace quotes to false ...
    (microsoft.public.mac.office.word)
  • Re: [opensuse] forcing order of file copy
    ... for file in *mp3; do echo $file; done ... create a small script that takes a single argument ... We are ignoring the messages from mkdir because most of the time it will spew a "already exists" error message which we don't care about. ... For additional commands, e-mail: opensuse+help@xxxxxxxxxxxx ...
    (SuSE)
  • Re: Database/Email Script
    ... Both double and single quotes work for quoting array indexes. ... the relevant code. ... What I would like first would be to get the script working. ... task as a learning experience.) ...
    (comp.lang.php)
  • Re: MP3s and Linux [ogg format]
    ... I just had a look at the script itself and that is how it works. ... there is little loss to get worried about (the mp3 to wav product may just ... be exactly what ogg captures anyway). ... > stores the output as a wav file. ...
    (Fedora)
  • Re: Interesting Discussion with my PHP Teacher
    ... ## For a small script like this one, ... > it wher the sql statement is constructed because there are too many quotes. ...
    (comp.lang.php)