|
Home > Archive > Unix Shell > May 2004 > **variables, lists, and for loops **
You are viewing an archived Text-only version of the thread.
To view this thread in it's original format and/or if you want to reply to
this thread please [click here]
| Author |
**variables, lists, and for loops **
|
|
| validus@c-gate.net 2004-05-20, 2:39 am |
| OK, I'm sure there is something fairly straightforward I am
overlooking here in my sleep deprived state but I can't figure
this out.
Say I have a list of words in a variable that I want to iterate
through but some of the words have embedded spaces.
I want to iterate through this list but the first for loop doesn't
group the words based on the quotations. The second one
does. The list contents appear to be the same for each but we get
different output.
========================================
=====================
#!/bin/bash
list="\"Coca Cola\" \"Diet Pepsi\" \"Coffee\" "
echo "List = $list"
for drink in $list
do
echo "Drink = $drink"
done
for drink in "Coca Cola" "Diet Pepsi" "Coffee"
do
echo "Drink = $drink"
done
========================================
=====================
I'm using GNU bash, version 2.05b.0. Any ideas?
cheers,
validus
| |
| Stefan Lagotzki 2004-05-20, 5:38 am |
| Validus <validus@c-gate.net>:
> Say I have a list of words in a variable that I want to iterate
> through but some of the words have embedded spaces.
[..]
> ========================================
=====================
> I'm using GNU bash, version 2.05b.0. Any ideas?
>
If you use the bash, you may create this array and test it:
stefan2@platon:~/test> mylist=("Coke" "Pepsi" "Water" "Juice")
stefan2@platon:~/test> echo ${mylist[0]}
Coke
stefan2@platon:~/test> echo ${mylist[3]}
Juice
stefan2@platon:~/test> for F in ${mylist[@]}
> do
> echo $F
> done
Coke
Pepsi
Water
Juice
See also: man bash --> Arrays
---------------------------------------------------------------
#!/bin/bash
mylist=("Coke" "Pepsi" "Water" "Juice")
for F in ${mylist[@]}
do
echo $F
done
| |
| Chris F.A. Johnson 2004-05-20, 5:38 am |
| On 2004-05-20, validus@c-gate.net wrote:
> OK, I'm sure there is something fairly straightforward I am
> overlooking here in my sleep deprived state but I can't figure
> this out.
>
> Say I have a list of words in a variable that I want to iterate
> through but some of the words have embedded spaces.
> I want to iterate through this list but the first for loop doesn't
> group the words based on the quotations. The second one
> does. The list contents appear to be the same for each but we get
> different output.
> ========================================
=====================
> #!/bin/bash
> list="\"Coca Cola\" \"Diet Pepsi\" \"Coffee\" "
> echo "List = $list"
>
> for drink in $list
> do
> echo "Drink = $drink"
> done
>
> for drink in "Coca Cola" "Diet Pepsi" "Coffee"
> do
> echo "Drink = $drink"
> done
> ========================================
=====================
> I'm using GNU bash, version 2.05b.0. Any ideas?
With any Bourne-type shell (including bash), you can do it by
manipulating the internal field separator ($IFS):
## create the list with a newline separating the items
list='Coca Cola
Diet Pepsi
Cofee'
## Set IFS to a newline
IFS='
'
for item in $list
do
echo "Drink = $item"
done
In bash, you can also use an array:
list=( "Coca Cola" "Diet Pepsi" "Coffee" )
for item in "${list[@]}"
do
echo "Drink = $item"
done
--
Chris F.A. Johnson http://cfaj.freeshell.org/shell
========================================
===========================
My code (if any) in this post is copyright 2004, Chris F.A. Johnson
and may be copied under the terms of the GNU General Public License
| |
| validus@c-gate.net 2004-05-20, 5:37 pm |
| Thanks for the help guys. Chris, changing the IFS variable did exactly what I
wanted.
cheers,
validus
|
|
|
|
|