DaemonForums  

Go Back   DaemonForums > Miscellaneous > Programming

Programming C, bash, Python, Perl, PHP, Java, you name it.

Reply
 
Thread Tools Display Modes
  #1   (View Single Post)  
Old 5th July 2011
fossala's Avatar
fossala fossala is offline
Real Name: David
Fdisk Soldier
 
Join Date: Feb 2011
Location: Cornwall, UK
Posts: 48
Thanked 0 Times in 0 Posts
Default Address book

I know there are plenty of addressbook apps that do this. It's more for learning rather than that I need it.
My code is not complete at all just started.
Code:
  1 #!/bin/sh
  2 #phonebook
  3 echo "Would you like to 'add' 'edit' or 'read' the database?"
  4 echo -n "Enter Choice: "
  5 read choice
  6         if [ $choice = add ]; then
  7                 echo -n "Please enter name for the database: "
  8                 read name
  9                 echo $name >> ~/.pbook/$name
 10                 echo "---------------------------"
 11                 echo -n "Enter Home Phone number [number/NO]: "
 12                 read -e number
 13                         if [ $number = NO ]; then
 14                                 echo ""
 15                         else
 16                                 `echo "Home number: $number" >> ~/.pbook/$name`
 17                         fi
 18                 echo -n "Enter email address [email/NO]: "
 19                 read -e eaddress
 20                         if [ $eaddress = NO ]; then
 21                                 echo ""
 22                         else
 23                                 `echo "Email address: $eaddress" >> ~/.pbook/$name`
 24                         fi
 25         elif [ $choice = read ]; then
 26                 echo -n "Please enter the clients name or list: "
 27                 read -e username
 28                         if [ $username = list ]; then
 29                                 ls ~/.pbook
 30                         else
 31                                 cat ~/.pbook/$username
 32                         fi
 33         else
 34                 echo "That is not a vailid choice"
 35         fi
What I want to is once I choose "list" under "read" it goes back to re ask me the name instead of exiting. I could do this by just adding the $username = part again but I was wondering If there was an easier/more clean way of doing this?
Reply With Quote
  #2   (View Single Post)  
Old 5th July 2011
vermaden's Avatar
vermaden vermaden is offline
Administrator
 
Join Date: Apr 2008
Location: pl_PL.lodz
Posts: 1,052
Thanked 118 Times in 93 Posts
Default

Generally I would rather create a while/readline loop, so the script will not end its work, like:

Code:
while :
do
  echo "what: " 
  read CHOICE
  case ${CHOICE} in
    (yes)
      # DO 1
      ;;
    (no)
      # DO 2
      ;;
    (exit|end)
      # END
      break
    (*)
      continue
      ;;
  esac
done
__________________
religions, worst damnation of mankind
"If 386BSD had been available when I started on Linux, Linux would probably never had happened." Linus Torvalds

Linux is not UNIX! Face it! It is not an insult. It is fact: GNU is a recursive acronym for “GNU's Not UNIX”.
vermaden's: links resources deviantart spreadbsd
Reply With Quote
  #3   (View Single Post)  
Old 5th July 2011
fossala's Avatar
fossala fossala is offline
Real Name: David
Fdisk Soldier
 
Join Date: Feb 2011
Location: Cornwall, UK
Posts: 48
Thanked 0 Times in 0 Posts
Default

Cheers this looks good and works. Could anyone point me in the direction of documentation on the matter. I can see that it works but I cannot see how it works and how to use it.
Reply With Quote
  #4   (View Single Post)  
Old 5th July 2011
rocket357's Avatar
rocket357 rocket357 is offline
Real Name: Jonathon
Wannabe OpenBSD porter
 
Join Date: Jun 2010
Location: 127.0.0.1
Posts: 322
Thanked 9 Times in 9 Posts
Default

Quote:
Originally Posted by fossala View Post
Cheers this looks good and works. Could anyone point me in the direction of documentation on the matter. I can see that it works but I cannot see how it works and how to use it.
man ksh or man sh
__________________
Linux Admin by Profession. OpenBSD user by choice.
Reply With Quote
  #5   (View Single Post)  
Old 5th July 2011
fossala's Avatar
fossala fossala is offline
Real Name: David
Fdisk Soldier
 
Join Date: Feb 2011
Location: Cornwall, UK
Posts: 48
Thanked 0 Times in 0 Posts
Default

I couldn't find anything that explained it in man sh or man mksh.
[EDIT] Managed to find documentation of how to use 'while'

Last edited by fossala; 5th July 2011 at 04:44 PM.
Reply With Quote
  #6   (View Single Post)  
Old 5th July 2011
rocket357's Avatar
rocket357 rocket357 is offline
Real Name: Jonathon
Wannabe OpenBSD porter
 
Join Date: Jun 2010
Location: 127.0.0.1
Posts: 322
Thanked 9 Times in 9 Posts
Default

man ksh, then type "/while list" and hit enter.
__________________
Linux Admin by Profession. OpenBSD user by choice.
Reply With Quote
  #7   (View Single Post)  
Old 5th July 2011
vermaden's Avatar
vermaden vermaden is offline
Administrator
 
Join Date: Apr 2008
Location: pl_PL.lodz
Posts: 1,052
Thanked 118 Times in 93 Posts
Default

@fossala

Check these mate:
http://bash.cyberciti.biz/guide/Menu_driven_scripts
http://developer.apple.com/documenta...ShellScripting
__________________
religions, worst damnation of mankind
"If 386BSD had been available when I started on Linux, Linux would probably never had happened." Linus Torvalds

Linux is not UNIX! Face it! It is not an insult. It is fact: GNU is a recursive acronym for “GNU's Not UNIX”.
vermaden's: links resources deviantart spreadbsd
Reply With Quote
Reply

Tags
shell

Thread Tools
Display Modes

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 On
HTML code is Off

Forum Jump

Similar Threads
Thread Thread Starter Forum Replies Last Post
C: address of an int in an array in a struct? xmorg Programming 3 18th April 2011 02:31 PM
could not look up internet address for .lan idefix FreeBSD General 2 31st January 2009 02:22 PM
Static IP address problem rex FreeBSD General 2 25th November 2008 08:53 PM
MAC address to IP rex FreeBSD General 9 11th November 2008 07:06 PM
Asking about IPv6 address berlowin Off-Topic 2 9th July 2008 02:39 AM


All times are GMT. The time now is 12:22 AM.


Powered by vBulletin® Version 3.8.4
Copyright ©2000 - 2014, Jelsoft Enterprises Ltd.
Content copyright © 2007-2010, the authors
Daemon image copyright ©1988, Marshall Kirk McKusick