Shell scripting/menu

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:

Write a shell menu program that will:
a. copy a file to a designated directory
b. tell you if a specified user is logged in.
c. display the details of a specified file
d. list the contents of a specified directory

Check for input errors, issues an error message and continue until the exit option is chosen.

  1. Relevant commands, code, scripts, algorithms:

Shell scripting

  1. The attempts at a solution (include all code and scripts):
# Use of a case statement to offer a 5 item menu
echo �  Menu\n
1. Copy a file to a designated directory\n
2. Check whether a specified user is logged in\n
3. Display the details of a specified file\n
4. List the contents of a specified directory\n
5. Quit to Unix\nEnter your option #: \c�
read choice
case �$choice� in
1)	cp file $HOME
2)	who | grep 'name'
3)      ls -l file
4)      ls directory
5)      exit ;;
*)	echo �Invalid option�   # ;; not needed for last option
esac

A couple things:

1) Is this a step in the right direction? And could someone please test it just to see if it functions?

2) How would I make the program more portable so that the user can enter the name of a file for option one or enter the name of the person for option 2. What I mean is, how do you make it so you have variables for the options instead of hardcoded ones?

I need to also turn in a printout of the program listing or something too, whatever that means(?)

Thanks.

  1. Complete Name of School (University), City (State), Country, Name of Professor, and Course Number (Link to Course):

Brookdale Community College - Lincroft, New Jersey - United States - Dr. Rick Bournique- COMP 145

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).

Your case syntax is slightly wrong: The ;; must come at the end of every option, not just the last two.

Your read syntax is correct.

All that remains is putting it in a loop like

while true
do
...
done
1 Like

Thanks, I understand about the case and I will fix that, but what about making the program more portable?

How would I make it so the user has to input the option for things so its not hard coded?

And not sure what you mean by putting it in a loop? What in a loop?

Thanks I am just a noob so your help is appreciated :slight_smile:

You printed a prompting message specifying the menu choices available and then read a response from the user letting them select their choice. If the user chose option 1, could you print another prompt asking them to choose the file to be copied and read another response from them to select the file to be copied? If the user chose option 2, could you print another prompt asking them to identify the name of user to search for?

As for you question about what to put in the loop Corona688 mentioned, put everything in the loop that you want to be repeated until the user selects choice 5.

How does this look thus far:

# Use of a case statement to offer a 5 item men
echo �  Menu\n
1. Copy a file to a designated directory\n
2. Check whether a specified user is logged in\n
3. Display the details of a specified file\n
4. List the contents of a specified directory\n
5. Quit to Unix\nEnter your option #: \c�

read choice

case �$choice� in
while true
do
1)	echo � Chose a file to be copied to your /etc directory
        read filename
        cp $filename /etc
2)	echo � Enter the name of a user to check if they are logged in
        read name
        who | grep '$name';;
3)      echo � Enter the name of a file to check it's details
        read name
        ls -l $name;;
4)      echo � Enter the name of a directory to list its contents
        read name
        ls $name;;
5)      exit ;;
done
*)	echo �Invalid option�   # ;; not needed for last option
esac

NOTE: It doesn't seem to work, says not found for each menu item. Just posting for some guidance to correct the issue.

It's progressing, but still has some definite problems.

1) Are you using some kind of non-text editor? Your "double quote" marks come out strange in the code you did such a great job posting. It's a little confusing with those odd "double quote" characters, but there may be problem with not enough or too many of those things.

2) Think about what Don Cragun said before, and it will help explain one reason why it is not currently working:

Do you know what a loop is? If not, go back and review the post by Corona688, and move your code around to put "everything you want repeated until choice 5 selected" inside the loop.

I think I just have to move

done

above choice 5?

And the double quotes should just be regular ones like this " - unless they should be something else altogether?

What type of system are you using? (The treatment of backslash characters in echo arguments varies from system to system. And the way you're using echo seems strange for any operating system.) Have you considered using printf instead of echo?

Can you explain the difference between strings delimited by double quotes ( "..." ) and by single quotes ( '...' )? Are all of you quotes matched? And, yes, just use plain double quotes ( " ); not opening double quotes and closing double quotes ( and )!

What shell are you using? Look at your shell's man page for the format of a case statement and the format of a while statement? I don't know of any shell that accepts a compound statement of the form:

case "$choice" in
while true
do
 ... ... ...
done
*)	echo "Invalid option"   # ;; not needed for last option
esac

You don't want to put some cases in a loop; you want to put the case statement in a loop. You also need to decide if you want the menu to only appear once, or to appear every time through the loop.

Basically yes. Here's one good approach. Get something simple working. Make a simple loop, like Corona688 showed before, and put one thing in it. For example, ask the user for their choice, print their choice, and if the choice is 5, exit. Once that is working, put in something else. At each step, keep the code working. It's the best way to develop. That way you deal with one problem at a time.

Yes, just use simple "double quotes". Are you using Word or something like that? You have to use a text editor for it to work.

I stripped it down to this:

# Use of a case statement to offer a 5 item menu
echo "  Menu\n
1. Copy a file to a designated directory\n
5. Quit to Unix\nEnter your option #: \c"
 
read choice
 
case "$choice" in
while true
do
1)      echo " Chose a file to be copied to your /etc directory;;
       read filename;;
       cp $filename /etc;;
done
5)      exit ;;
*)      echo "Invalid option"   # ;; not needed for last option
esac

but I am getting ./example: 8: Syntax error: word unexpected (expecting ")")

any ideas?

I suggested some ideas in message #8 in this thread. It doesn't look like you considered anything I posted there, and the problems you're showing here seem to be the same as the problems you had then.

Is this really an exact copy of the script you ran when you got the error message above? I would have expected it to complain about line 9 instead of line 8 with the code you listed above.

Go back to post number two, and look at the while loop. Everything has to fit inside the while loop. Put one echo and one read inside the while loop for starters. Once that is working, put your case statement inside the while loop too, to deal with the user response.

Jagst3r21
Your approach presents your menu to the user only once and if he has not selected to exit then it goes on to loop infinitely.
The proper approach should be to loop the menu too so that once you have served the users request for the first time you again go back to the user to get his input and ask him whether he wants to quit if yes then get out of the loop
And also you dont need to have ;; in every line within case statement .Search for some case examples I am sure you will come to know what is going wrong in your script

This is what i found

# Use of a case statement to offer a 5 item men
echo "Menu
1. Copy a file to a designated directory
2. Check whether a specified user is logged in
3. Display the details of a specified file
4. List the contents of a specified directory
5. Quit to Unix "

while :
do
echo "Enter your option "
read choice
case $choice in
1)	echo "Chose a file to be copied to your /etc directory:
        read filename
        cp $filename /etc;;
2)	echo "Enter the name of a user to check if they are logged in"
        read name
        who | grep $name;;
3)      echo "Enter the name of a file to check it's details"
        read name
        ls -l $name;;
4)      echo "Enter the name of a directory to list its contents"
        read name
        ls $name;;
5)      exit ;;
*)	echo "Invalid option "   # ;; not needed for last option
esac
done