On the topic of BASH loops

Paul King pking123-rieW9WUcm8FFJ04o6PK0Fg at public.gmane.org
Thu Aug 19 15:14:59 UTC 2004


The recent discussion got me thinking about BASH loops. I use them ad nauseam 
to download radio programs of certain dates and times from radio station 
archives (such as Pacifica). 

The biggest issue for me was that if I had multiple dates, I could not place 
them in an array. For example:

dates=("Mon Aug 16" "Tue Aug 17" "Wed Aug 18")

for i in ${dates[@]} ; do 
    echo ${i}
done

gives me the same output as if I had declared my array as:

dates='Mon Aug 16 Tue Aug 17 Wed Aug 18'

for i in $dates ; do 
    echo ${i}
done

OUTPUT:
Mon
Aug
17
Tue
Aug
17
Wed
Aug
18

YECCH!

The only way I could declare a BASH array in any useful sense is to do it 
explicitly (as taken from a script that just ran successfully):

date[1]='Mon 2004 08 16'
date[2]='Tue 2004 08 17'
date[3]='Wed 2004 08 18'

for i in 1 2 3 ; do
   echo ${date[$i]}
done

OUTPUT:
Mon 2004 08 16
Tue 2004 08 17
Wed 2004 08 18

Much better!

Notice I could have used any three index values that darned well pleased me. If 
anyone can come up with a better way in BASH to declare an array of strings 
which have whitespace in them, please suggest something. 

Paul
=========================================================
Paul King              http://www3.sympatico.ca/pking123/


--
The Toronto Linux Users Group.      Meetings: http://tlug.ss.org
TLUG requests: Linux topics, No HTML, wrap text below 80 columns
How to UNSUBSCRIBE: http://tlug.ss.org/subscribe.shtml





More information about the Legacy mailing list