LinuxQuestions.org
Did you know LQ has a Linux Hardware Compatibility List?
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
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.

Notices

Reply
 
Search this Thread
Old 04-05-2003, 05:00 PM   #1
bulliver
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: 77
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:
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
 
Old 04-05-2003, 06:03 PM   #2
bulliver
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: 77
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:
Code:
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
unSpawn
Moderator
 
Registered: May 2001
Posts: 27,457
Blog Entries: 54

Rep: Reputation: 2897Reputation: 2897Reputation: 2897Reputation: 2897Reputation: 2897Reputation: 2897Reputation: 2897Reputation: 2897Reputation: 2897Reputation: 2897Reputation: 2897
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>".

#!/bin/bash
A=( contact666 )
D=( contact0 )
H=( contact1 )
contact1=( "Hammet, Kirk" "row 5, grave 3" "none" "corpse@graveyard.org" "" )
contact0=( "Darren Kirby" "123 fake address way" "123 456 7890" "loser@life.com" "placeholder for notes" )
contact666=( "Astaroth" "1 Kinda warm place" "none" "billg@hotmail.com" "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"
}

mail_me
# Arrays start counting from zero up btw.
 
  


Reply


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


All times are GMT -5. The time now is 10:54 PM.

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