Hello, I am trying to teach myself shell programming so I am scripting a really cheesy addressbook sort of thing. I am having problems with for loops and arrays however.
In particular, I am having problems with the function that lists all contacts. The script sources a file with the contacts in it which looks like:
Code:
contact[1]=Darren
Darren[1]="Darren Kirby"
Darren[2]="123 fake address way"
Darren[3]="123 456 7890"
Darren[4]="loser@life.com"
Darren[5]="placeholder for notes"
contact[2]=Joe
Joe[1]="Joe Blow"
Joe[2]="234 another fake rd"
Joe[3]="250 476 7773"
Joe[4]="loser@death.com"
Joe[5]="this is a note"
My function that adds the contacts adds them perfectly in this format, and a little loop adds 1 to each index of the "contact" array, but I can't get it to display them properly. Here's the relevant bit of the function:
Code:
list_contacts() {
if [ ! -e ~/.addbookdata ] # check for first time user
then
echo "There are no contacts listed"
touch ~/.addbookdata
echo "Contact data file has been created"
echo "You may now add contacts"
main_menu
else
source ~/.addbookdata
echo "All contacts:"
for index in 1 2 3 4 5
do
echo "${contact[index]}"
done
echo
fi
}
I am having problems with the echo statement, it works fine when I put in the actual name ie: "echo "$darren[index]" but what I need to have it do is cycle through all the contacts ie: contact[1], contact[2], etc...and cycle through the five contact indices for each contact which hold the actual address info.
What do I need to do here? Nested for loop? Any ideas? Even a bit of psuedo code might help me through this block...
Thanks