Welcome to the most active Linux Forum on the web.
Go Back > Forums > Non-*NIX Forums > Programming
User Name
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.


  Search this Thread
Old 04-05-2003, 05:00 PM   #1
Senior Member
Registered: Nov 2002
Location: Edmonton AB, Canada
Distribution: Gentoo x86_64; Gentoo PPC; FreeBSD; OS X 10.9.4
Posts: 3,760
Blog Entries: 4

Rep: Reputation: 78
bash programming

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:
Darren[1]="Darren Kirby"
Darren[2]="123 fake address way"
Darren[3]="123 456 7890"
Darren[5]="placeholder for notes"

Joe[1]="Joe Blow"
Joe[2]="234 another fake rd"
Joe[3]="250 476 7773"
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:
list_contacts() {
	if [ ! -e ~/.addbookdata ] # check for first time user
		echo "There are no contacts listed"
		touch ~/.addbookdata
		echo "Contact data file has been created"
		echo "You may now add contacts"
		source ~/.addbookdata
		echo "All contacts:"
		for index in 1 2 3 4 5
			echo "${contact[index]}"
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...

Old 04-05-2003, 06:03 PM   #2
Senior Member
Registered: Nov 2002
Location: Edmonton AB, Canada
Distribution: Gentoo x86_64; Gentoo PPC; FreeBSD; OS X 10.9.4
Posts: 3,760
Blog Entries: 4

Original Poster
Rep: Reputation: 78
Ok, I think I can sort this out if I can figure out how to get variable variables to work.

ie: say I have something like echo "$var1[var2]", so I would need to get var1 and var2 to make up one variable, is this possible? I have tried command substitution and all sorts of quoting but no fruit.

Also, I am having problems getting vars to resolve properly.
More concrete example:
mail_me() {
	echo "Please choose a contact to mail:"
	read mailee
	maileeadd=`echo $mailee[4]`
	source ~/.addbookdata
	echo "${maileeadd}"
In this bit of code you would enter who you want to email (darren) then try to get the email address which is darren[4] in the array, but the echo statement resolves literally to "darren[4]" instead of the address as it should. Is this something to do with variable scope? Any help appreciated...
Old 04-05-2003, 09:30 PM   #3
Registered: May 2001
Posts: 29,390
Blog Entries: 55

Rep: Reputation: 3562Reputation: 3562Reputation: 3562Reputation: 3562Reputation: 3562Reputation: 3562Reputation: 3562Reputation: 3562Reputation: 3562Reputation: 3562Reputation: 3562
Here's what I could come up with. I'd start with alphabetical indexes, good for mgmnt and expansion, eventually (when you've got lotsa entries) you *will* want to be selective *which* arrays to load. Hope it's kinda self explainatory really, else execute as "sh -x <script>".

A=( contact666 )
D=( contact0 )
H=( contact1 )
contact1=( "Hammet, Kirk" "row 5, grave 3" "none" "" "" )
contact0=( "Darren Kirby" "123 fake address way" "123 456 7890" "" "placeholder for notes" )
contact666=( "Astaroth" "1 Kinda warm place" "none" "" "Definately on medication" )

mail_me() { echo "Please choose a contact to mail:"; read mailee
for contact in $(eval echo \${${mailee:0:1}[@]}); do eval echo \$${contact[0]} | grep -qe "${mailee}"
case $? in 0) p=$(eval echo \${$contact[0]}); m=$(eval echo \${$contact[3]});; esac; done
printf "%s${p:=${mailee}} has addr ${m:="which seems totally bogoid."}\n"

# Arrays start counting from zero up btw.


Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off

Similar Threads
Thread Thread Starter Forum Replies Last Post
Bash programming winterhunter Linux - Newbie 6 11-26-2007 07:14 AM
Bash Programming moodupani Programming 1 08-31-2005 06:50 PM
bash programming alaios Programming 2 07-23-2005 12:52 AM
bash programming drisay Programming 6 12-20-2004 09:09 AM
bash programming cpukiller Linux - Newbie 1 11-04-2003 06:45 PM > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 05:07 AM.

Main Menu
Write for LQ is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration