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



On Monday 06 November 2006 21:55, Darryl Gregorash wrote:
On 2006-11-07 00:06, Randall R Schulz wrote:
On Monday 06 November 2006 21:11, 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!)

<snip>

If the code fragment you gave above works, then what appears to be
spaces are some other character code, not an ASCII space (040 / 0x20).
There are other character codes that make no mark and are not
zero-width. Perhaps your file names use one of them.

That simply is not possible. The list is determined by "for i in <list>"
and that list is determined prior to the actual parsing of any commands
inside the for loop. If it is a literal space for the ls command, then
it must be a literal space for any other command. But the variation of
results reported here, and your comments in particular, have made me
wonder what effect the quotes will have placed in different places
inside the script.

First I changed my test script to execute a "ls -l $i" after the echo,
and came up with this sort of result:

Antonio Vivaldi-Gloria Dixit Dominus Magnificat Choir of Kings College
Cambridge and the Academy of Ancient Music.m3u
ls: Antonio: No such file or directory
ls: Vivaldi-Gloria: No such file or directory
ls: Dixit: No such file or directory
ls: Dominus: No such file or directory
(etc)

exactly as you stated.

The best I can think of is that the ls command takes a list, ie. the
actual ls commandline format is not "ls [options] [filespec]", rather it
is "ls [options] [list_of_filespecs]". Any command behaving in the same
way, such as cp, will fail in the same way. When $i in the ls command is
placed inside double quotes, I get the expected ls output:

~>cat thingy
cd /pub/music/mp3
for i in *.m3u; do echo $i; ls -l "$i"; done
cd ~
~>./thingy
Antonio Vivaldi-Gloria Dixit Dominus Magnificat Choir of Kings College
Cambridge and the Academy of Ancient Music.m3u
-rw-r--r-- 1 raven users 5364 2005-11-05 01:31 Antonio Vivaldi-Gloria
Dixit Dominus Magnificat Choir of Kings College Cambridge and the
Academy of Ancient Music.m3u
(etc)

If the double quotes are replaced with single quotes, each ls command
returns an error, "ls: $i: No such file or directory". I've forgotten
the subtle nuances of quotes in the bash commandline to remember why
this happens.


When the for loop is written the following way (and these can be single
or double quotes, it does not matter), then there is a single variable
in the list of the for loop, so the echo and ls commands are actually
run just once each:

for i in '*.m3u'; do echo $i; ls -l $i; done

and the output becomes:

~> ./thingy
Antonio Vivaldi-Gloria Dixit Dominus Magnificat Choir of Kings College
Cambridge and the Academy of Ancient Music.m3u Georg Friederich
Händel-Water Music Suite.m3u Johannes Brahms-Symphony no 1 in C minor Op
68 Thomas Sanderling Philharmonia Orchestra.m3u Johannes
Brahms-Symphony no 2 in D major Op 73 Thomas Sanderling Philharmonic
Orchestra.m3u Johannes Brahms-Symphony no 3 in F major Op 90 Tragic
Overture Op 81 Thomas Sanderling Philharmonia Orchestra.m3u Johannes
Brahms-Symphony no 4 in E minor Op 98 Thomas Sanderling Philharmonia
Orchestra.m3u Mikhail IppolitovIvanov-Liturgy of St John Chrysostom Op
37 Vespers Op 43.m3u St Petersburg Chamber Choir-Russian Easter.m3u The
Mamas The Papas-Beautiful Thing.m3u The Mamas The Papas-California
Dreamin.m3u The Mamas The Papas-If You Can Believe Your Eyes and
Ears.m3u The Tallis Scholars-Allegri Miserere Palestrina Missa Papae
Marcelli Mundy Vox Patris Caelestis.m3u Virtuosi di Praga-Music for the
Kings of Spain.m3u Westminster Abbey Choir and the Abbey
Consort-Miserere.m3u Wolfgang Amadeus Mozart-Requiem Slovak
Philharmonic Orchestra.m3u
-rw-r--r-- 1 raven users 5364 2005-11-05 01:31 Antonio Vivaldi-Gloria
Dixit Dominus Magnificat Choir of Kings College Cambridge and the
Academy of Ancient Music.m3u
-rw-r--r-- 1 raven users 624 2005-11-19 03:12 Georg Friederich
Händel-Water Music Suite.m3u
(etc)

Lesson: ALWAYS QUOTE VARIABLE REFERENCES WHOSE CONTENT YOU DO NOT FULLY
CONTROL.

So, that is absolutely correct, but with the qualification that what you
get does depend on where and how you do the quoting, and AFAICT there is
no hard and fast rule that gives a single correct way to use quotes.

Did someone say the bash commandline isn't interesting and fun? :-)

This is a very interesting thread. Parts are going into my "Good Stuff' sub
folder. When in doubt-quote;thoughtfully. A relative constant, how nice.

Thanks,
Jerome

--
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

  • Re: [SLE] bash Script Using Filenames with Embedded Spaces
    ... If it is a literal space for the ls command, ... wonder what effect the quotes will have placed in different places ... Antonio Vivaldi-Gloria Dixit Dominus Magnificat Choir of Kings College ...
    (SuSE)
  • Re: [SLE] bash Script Using Filenames with Embedded Spaces
    ... command, then it must be a literal space for any other command. ... Antonio Vivaldi-Gloria Dixit Dominus Magnificat Choir of Kings ... College Cambridge and the Academy of Ancient Music.m3u ... command is placed inside double quotes, I get the expected ls output: ...
    (SuSE)
  • Re: Bash commands: maybe they want to drive us crazy?
    ... Bill Unruh wrote: ... single quotes would cause the find command to expand the * ... Which means that a shell command and the shell itself are two very different ...
    (comp.os.linux.misc)
  • Re: Yet another "stop the bash quoting" post
    ... |> Output from running the file1.sh command is: ... |> the argument doesn't contain those single quotes; ... | By using set -x in the bash script and running /command/ without the ...
    (comp.os.linux.misc)
  • Re: Yet another "stop the bash quoting" post
    ... |> Output from running the file1.sh command is: ... |> the argument doesn't contain those single quotes; ... | By using set -x in the bash script and running /command/ without the ...
    (comp.unix.shell)