Skip to Content

Revision of Shell Script [Phone book assignment] from Tue, 15/09/2009 - 4:11pm

The revisions let you track differences between multiple versions of a post.

  1. !/usr/bin/ksh

PS3="Enter your choice number "

clear

  1. phnbkfile file to save accounts info if it doesnot exist create it

then touch phnbkfile fi

  1. error function to help user

error() { echo "usage: signin " }

echo "\033[1;31;31m" echo "\033[50C Phone Book"

  1. Interactive

then select choice in create Display Exit do case $choice in create)

  1. Enter Name with validation:
  2. ----------------------------

echo "\033[5C Name:\c" read Name Name="$Name"

do echo "Enter valid name" echo "\033[5C Name:\c" read Name Name="$Name" done

  1. Enter Phone with validation:
  2. -----------------------------

echo "\033[5C Phone:\c" read Phone

do echo "Enter valid Phone number" echo "\033[5C Phone:\c" read Phone done

  1. Check if this account already exist:
  2. -----------------------------------

CHK=`grep -w "$Name" phnbkfile`

do echo "This Name already exists please enter another name" echo "\033[5C Name:\c" read Name Name="$Name" do echo "Enter valid name" echo "\033[5C Name:\c" read Name done echo "\033[5C Phone:\c" read Phone CHK=`grep -w "$Name" phnbkfile` done;

echo "$Name:$Phone" >> phnbkfile;;

Display) echo "\033[5C Name:\c" read Name

do echo "Enter valid name" echo "\033[5C Name:\c" read Name Name="$Name" done

CHK=`grep -w "$Name" phnbkfile`

then echo "There is no account naming $Name" else Name=`echo $CHK | cut -f1 -d:` Phone=`echo $CHK | cut -f2 -d:` echo "Name: $Name \n Phone: $Phone" fi;;

Exit) exit;; *) echo "Enter a valid choice" esac done

else

Name=$2 Phone=$3

case $1 in

  1. Create an account

-c) then

then echo "This unvalid name" fi

then echo "This Unvalid Phone number" fi

  1. Check if this account already exist:
  2. ...................................

CHK=`grep -w "$2" phnbkfile`

then echo "This Name already exists please enter another name" else echo "$2:$3" >> phnbkfile fi else error fi ;;

  1. Display info about an account:

-d) then

then echo "This is Unvalid name" fi

CHK=`grep -w "$2" phnbkfile`

then echo "There is no account naming $2" else Name=`echo $CHK | cut -f1 -d:` Phone=`echo $CHK | cut -f2 -d:` echo "Name: $Name \n Phone: $Phone" fi else error fi;;

*) error esac fi



Dr. Radut | forum