Re: [SLE] bash Script Using Filenames with Embedded Spaces
- From: Joachim Schrod <jschrod@xxxxxxx>
- Date: Tue, 07 Nov 2006 12:01:21 +0100
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
- References:
- [SLE] bash Script Using Filenames with Embedded Spaces
- From: Lucky Leavell
- Re: [SLE] bash Script Using Filenames with Embedded Spaces
- From: Randall R Schulz
- Re: [SLE] bash Script Using Filenames with Embedded Spaces
- From: Darryl Gregorash
- Re: [SLE] bash Script Using Filenames with Embedded Spaces
- From: Randall R Schulz
- Re: [SLE] bash Script Using Filenames with Embedded Spaces
- From: Lucky Leavell
- [SLE] bash Script Using Filenames with Embedded Spaces
- Prev by Date: [SLE] Crossover Office and K-menu entries
- Next by Date: Re: [SLE] NetBeans 5.5 Released
- Previous by thread: Re: [SLE] bash Script Using Filenames with Embedded Spaces
- Next by thread: Re: [SLE] bash Script Using Filenames with Embedded Spaces
- Index(es):
Relevant Pages
|