Skip to Content

Shell Script [Phone book assignment]

#!/usr/bin/ksh

PS3="Enter your choice number "

clear

#phnbkfile file to save accounts info if it doesnot exist create it
####################################################################

if [ ! -f phnbkfile ] 
 then
     touch phnbkfile
fi

#error function to help user 
########################### 

error()
       {
        echo "usage:  signin [-c] \"name\" PhoneNumber"
        echo "usage:  signin [-d] \"name\"" 
        echo "usage:  signin "
       }
      
echo "\033[1;31;31m"
echo "\033[50C Phone Book"

#Interactive
############

if [ $# -eq 0 ]
then
     select choice in create Display Exit
     do
       case $choice in 
       create)
# Enter Name with validation:
#----------------------------
             echo "\033[5C Name:\c"
             read Name
             Name="$Name"

             Check=`echo "$Name" | tr -d [0-9]`

             while [ ${#Name} -ne ${#Check} ]
              do 
                 echo "Enter valid name"
                 echo "\033[5C Name:\c"
                 read Name
                 Name="$Name"
                 Check=`echo "$Name" | tr -d [0-9]`
              done 
 
# Enter Phone with validation:
#-----------------------------
             echo "\033[5C Phone:\c"
             read Phone

              Check=`echo "$Phone" | tr -d [0-9]`

              while [ ${#Check} -ne 0 ]
              do
                 echo "Enter valid Phone number"
                 echo "\033[5C Phone:\c"
                 read Phone 
                 Check=`echo "$Phone" | tr -d [0-9]`
              done

#Check if this account already exist:
#-----------------------------------
               CHK=`grep -w "$Name" phnbkfile`
               
               while [ -n "$CHK" ]
                do
                  echo "This Name already exists please enter another name"
                  echo "\033[5C Name:\c"
                  read Name
                  Name="$Name"
                  Check=`echo "$Name" | tr -d [0-9]`
                  while [ ${#Name} -ne ${#Check} ]
                   do
                       echo "Enter valid name"
                       echo "\033[5C Name:\c"
                       read Name
                       Check=`echo "$Name" | tr -d [0-9]`
                   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

                Check=`echo "$Name" | tr -d [0-9]`

                while [ ${#Name} -ne ${#Check} ]
                  do
                     echo "Enter valid name"
                     echo "\033[5C Name:\c"
                     read Name
                     Name="$Name"
                     Check=`echo "$Name" | tr -d [0-9]`
                  done

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

                if [ ! -n "$CHK" ]
                 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

#Create an account
##################      
                   -c) 
                      if [ $# -eq 3 ]
                      then  
                            Check=`echo "$2" | tr -d [0-9]`

                            if [ ${#Name} -ne ${#Check} ]
                            then 
                                 echo "This unvalid name"
                            fi 
 

                            Check=`echo "$3" | tr -d [0-9]`

                            if [ ${#Check} -ne 0 ]
                            then 
                                 echo "This Unvalid Phone number"
                            fi 

#Check if this account already exist:
#...................................
                            CHK=`grep -w "$2" phnbkfile`
               
                            if [ -n "$CHK" ]
                            then  
                                   echo "This Name already exists please enter another name"
                            else
                                   echo "$2:$3" >> phnbkfile 
                            fi 
                       else
                              error
                       fi ;; 

#Display info about an account:
###############################

                  -d)
                     if [ $# -eq 2 ]
                     then
                          Check=`echo "$2" | tr -d [0-9]`

                           if [ ${#Name} -ne ${#Check} ]
                           then 
                                echo "This is Unvalid name"
                           fi 
                        
                           CHK=`grep -w "$2" phnbkfile`

                           if [ ! -n "$CHK" ]
                           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