Re: [opensuse] bash - why doesn't the tldp bash beginners guide for loop example work?



On Wed, 13 Feb 2008, David C. Rankin wrote:-

Listmates,

I have run into a bash problem with a simple for loop I don't
understand and would like input from the gurus to determine if the tldp
documentation is in error or if it is me. Safe money says it's me, but
here is the situation.

I'll give you three guesses which one...

I am using example 9.1.2.2. from the following link to construct
a simple for loop to extract a directory full of zip files:

http://tldp.org/LDP/Bash-Beginners-Guide/html/sect_09_01.html

The example give is:

[carol@octarine ~/html] cat html2php.sh
#!/bin/bash
# specific conversion script for my html files to php
LIST="$(ls *.html)"
for i in "$LIST"; do
NEWNAME=$(ls "$i" | sed -e 's/html/php/')
cat beginfile > "$NEWNAME"
cat "$i" | sed -e '1,25d' | tac | sed -e '1,21d'| tac >> "$NEWNAME"
cat endfile >> "$NEWNAME"
done

Oh, oh. There's an error in that. As you guessed, the quotes around
"$LIST" shouldn't be there. Using some HTML files in one of my
directories as an example:

davjam@adder:~/public_html> ls *.htm
index.htm pics1.htm spam3days.htm spams7day.htm spamstats.htm
pic.htm rsync.example.htm spams3day.htm spams_per_day.htm you.htm

That shows the files, just for reference.

davjam@adder:~/public_html> LIST="$(ls *.htm)"

That's the first line in that script.

davjam@adder:~/public_html> echo "${LIST}"
index.htm
pic.htm
pics1.htm
rsync.example.htm
spam3days.htm
spams3day.htm
spams7day.htm
spams_per_day.htm
spamstats.htm
you.htm

And there was the contents of $LIST.

davjam@adder:~/public_html> for i in "${LIST}"; do echo "\"${i}\""; done
"index.htm
pic.htm
pics1.htm
rsync.example.htm
spam3days.htm
spams3day.htm
spams7day.htm
spams_per_day.htm
spamstats.htm
you.htm"

Now, here I've passed "$LIST" to the for loop and the body just prints
the contents of $i and adds quotes around it so you can see just what is
passed as $i. As you can see, there is a double-quote at the beginning
of the first line, and the closing quotes at the end of the last line.
This says that everything in between those quotes was assigned to $i in
one go.

As you correctly guessed, the quotes around $LIST shouldn't have been
there, and removing them gets this output instead:

davjam@adder:~/public_html> for i in ${LIST}; do echo "\"${i}\""; done
"index.htm"
"pic.htm"
"pics1.htm"
"rsync.example.htm"
"spam3days.htm"
"spams3day.htm"
"spams7day.htm"
"spams_per_day.htm"
"spamstats.htm"
"you.htm"

<Snip>

I understand enough to realize the quotes affect bash expansion,
but I am curious as to whether the tldp example is in error. What says
the brain trust?

Yes, there's an error in the tldp example.


Regards,
David Bolt

--
Team Acorn: http://www.distributed.net/ OGR-P2 @ ~100Mnodes RC5-72 @ ~15Mkeys
SUSE 10.1 32bit | openSUSE 10.2 32bit | openSUSE 10.3 32bit | openSUSE 11.0a1
SUSE 10.1 64bit | openSUSE 10.2 64bit | openSUSE 10.3 64bit
RISC OS 3.6 | TOS 4.02 | openSUSE 10.3 PPC | RISC OS 3.11
--
To unsubscribe, e-mail: opensuse+unsubscribe@xxxxxxxxxxxx
For additional commands, e-mail: opensuse+help@xxxxxxxxxxxx



Relevant Pages

  • Re: Using foreach loop to create radiobutton menu
    ... Your foreach loop is not the problem, the problem is how you define the -command. ... The only possible thing the interpreter can do is substitute the current value of range, which is likely the last value once your loop exited. ... One choice is double quotes. ... The double quotes means that $range gets expanded while in the loop, long before the puts command actually runs. ...
    (comp.lang.tcl)
  • Re: Web browser question
    ... it seems that you are working to get stock quotes from ... the Yahoo finance. ... ' Loop until the page is fully loaded ... Do Until .ReadyState = 4 ...
    (microsoft.public.excel.programming)
  • Re: Fascinating problem with bash
    ... will result in OTHER_VAR being unset at the completion of the loop. ... subshell exits and any vars it sets will go away with it. ... The author uses double quotes for the subshell, ... first echo, to force variable substitution to happen when the loop is ...
    (Debian-User)
  • Re: For vs. For Each
    ... wonder given quotes like: ... "...Even the slightest inefficiency inside a loop is magnified many ... for repetitive property access inside your loops, using foreach instead ... of the garbage collector and JIT compilier. ...
    (microsoft.public.dotnet.languages.vb)
  • Links From another Column
    ... In Column I have numbered links to html files on my harddrive. ... cells are blank. ... next same value thats in N2 get the numbered link form column. ... could I loop through column ...
    (microsoft.public.excel.programming)