Problem with executing a possible if or case statement script

Use and complete the template provided. The entire template must be completed. If you don't, your post may be deleted!

  1. The problem statement, all variables and given/known data:
    Create a phonebook program.
    It should use functions to perform the required tasks. It should be menu-based, allowing you the options of:

    • Search address book
    • Add entries

You will also need a Display function to present the result.
Suggestions:
The phonebook is basically an ASCII file with Phone Number, Name, Email Address and zipcode.
Try to use ":" as field seperator

  1. Relevant commands, code, scripts, algorithms:
    I would think to use either case and if statements together, trying to nest them, or possibly using a loop of some kind, but I haven't gotten the whole loop thing down.
    I know that i havent incorporated the phone list into it yet, but i just want my scripting to work. Not sure if I'm reading the problem correctly either.

  2. The attempts at a solution (include all code and scripts):
    GNU nano 1.2.4 File:phonebook

# ==================================================================================================
# Title: Phonebook directory
# Owner: Rob 
# Purpose: To allow the user to look up directories in the file
# ==================================================================================================

printf "Who would you like to search for in our database? \n"

printf "Enter your option for a record: \n a: add \n s: search \n e: exit \n"; read option
 case "$option" in
   "a") echo "A new record will be added.";;
   "s") echo "You have chosen to search the directory";;
   "e") echo "You have chosen to exit the database. Goodbye..";;
   *) echo "That is an invalid option.";;
if [ "$option" = "s" ]
then
   echo "`more phonelist`"
else
   if [ "$choice" = "a" ]
then
   echo "input the name of the person you would like to add"
else
   if [ "$chioce" = "e" ]
then
   echo "you have chosen to leave the database. Goodbye..."
else
   echo "That is an invalid option"
fi
fi
fi
 esac

Here is the phonebook directory

GNU nano 1.2.4 File: phonelist

Directory of local Clients and their information

907-345-0962 : Essie Vaill       : essie@vaill.com       : 99515
602-252-4237 : Cruz Roudabush    : cruz@roudabush.com    : 85004
212-889-5775 : Billie Tinnes     : billie@tinnes.com     : 10001
732-442-0638 : Zackary Mockus    : zackary@mockus.com    : 88761
808-836-8960 : Rosemarie Fifield : rosemarie@fifield.com : 96819
815-467-0487 : Bernard Laboy     : bernard@laboy.com     : 60410
602-953-2753 : Sue Haakinson     : sue@haakinson.com     : 85051
610-395-8745 : Valerie Pou       : valerie@pou.com       : 18087
626-960-6738 : Lashawn Hasty     : lashawn@hasty.com     : 91790
407-857-0431 : Marianne Earman   : marianne@earman.com   : 32809
901-327-5336 : Justina Dragaj    : justina@dragaj.com    : 38112
503-371-8219 : Mandy Mcdonnell   : mandy@mcdonnell.com   : 97302
330-758-0314 : Conrad Lanfear    : conrad@lanfear.com    : 44512
714-772-5050 : Cyril Behen       : cyril@behen.com       : 49202
210-229-3017 : Shelley Groden    : shelley@groden.com    : 78205

The names and likenesses are all made up, but for the sake of the assignment they are there.

  1. Complete Name of School (University), City (State), Country, Name of Professor, and Course Number (Link to Course):
    Baker College, Jackson, MI, USA, S.Sadiq LUX 211

Note: Without school/professor/course information, you will be banned if you post here! You must complete the entire template (not just parts of it).

Toward getting your current script working:

1) The ecase statement is in the wrong place. Move it up.

2) You don't need to (shouldn't) put more phonelist in an echo statement. The results of this are certainly unpredictable at best. The output from the more command is captured by the shell and used as the parameter for the echo command. From this perspective, the user might be given a prompt ( --more-- ) before they see any output, or they may never be prompted and the text written by the echo command would scroll by unchecked; either of these cases would be confusing. Further, if your phone book is large, the amount of text placed on the echo command might exceed system limits and cause an error. Simply coding the command and its parameters as a statement in the script is all you need here.

In general, I don't think that it is good form to ever execute a command that naturally expects user input using back ticks or when using the preferred $(command) syntax. These imply that the output from the command is to be saved or passed to another command/function and that human intervention might not be appropriate.

3) You are not reading choice from the user, yet you test for it.

I'd also recommend using double bracketed expressions ([[ expression ]]) as they are much more efficient. All of the modern Bourne derived shells support this syntax. When using this notation, you do need to use == to test for equality.

If you are using Kshell, then you can have the shell test for syntax errors which might help with your debugging. If your script is in the file pb.ksh then this command can be used to test for syntax issues:

ksh -n pb.ksh 

When I ran your original script through Kshell this way the output was this:

$ ksh -n  pb.ksh
t16.ksh: syntax error at line 16: `[' unexpected

Which immediately pointed out that the esac was misplaced.

Im not a bash user per se, but I believe that bash has a similar option/feature to syntax check if you prefer to use bash.

Hope this helps get you going again.