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@opensuse.org For additional commands, e-mail: opensuse+help@opensuse.org