Shell scripting/menu

 
Thread Tools Search this Thread
Homework and Emergencies Homework & Coursework Questions Shell scripting/menu
# 1  
Old 05-03-2013
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.

2. Relevant commands, code, scripts, algorithms:

Shell scripting

3. The attempts at a solution (include all code and scripts):

Code:
# 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.

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

Last edited by vbe; 05-04-2013 at 11:01 AM..
# 2  
Old 05-04-2013
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

Code:
while true
do
...
done

This User Gave Thanks to Corona688 For This Post:
# 3  
Old 05-04-2013
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 Smilie
# 4  
Old 05-04-2013
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.
# 5  
Old 05-05-2013
How does this look thus far:

Code:
# 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.

Last edited by Jagst3r21; 05-05-2013 at 12:19 AM..
# 6  
Old 05-05-2013
Quote:
How does this look thus far:
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:

Quote:
put everything in the loop that you want to be
repeated until the user selects choice 5.
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.
# 7  
Old 05-05-2013
I think I just have to move
Code:
done

above choice 5?

And the double quotes should just be regular ones like this " - unless they should be something else altogether?
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Menu and case statement scripting

hi all i am trying to get help with writing a script using case statement to display menu as 1) Authentication log 2) System log 3) Messages 4) Dmesg 5) Boot log Q) Exit When selecting the menu by 1 or 2 or 3 o 4 or 5, it should display the last 10 lines of the log files, if... (3 Replies)
Discussion started by: renegade11
3 Replies

2. Shell Programming and Scripting

Menu shell script help

Hi All, I have written a shell script that show menu driven option. My requirement is that in the menu driven option i want to select multiple choice. i.e if i want to select 1 or 1,2 or 1,2,3 or 2,3 etc .... Can some one help me in that My script. while true; do echo " " ... (8 Replies)
Discussion started by: Nawrajesh
8 Replies

3. Shell Programming and Scripting

Menu using shell script

Hi, I need to have a shell script for the below need. 1. Menu with one heading and 4 options. 2. the heading and 4 options are taken from a file. File entry ====== Heading1|option1|option2|option3|option4| Heading2|option1|option2|option3|option4| 3. the user entries must be captured in... (9 Replies)
Discussion started by: umastinu
9 Replies

4. Shell Programming and Scripting

Shell script menu

hi guys, how would you do the following? I have a menu with 5 options in my shell script: 1. Run function 1 against files 2. Run function 2 against files 3. Run function 3 against files 4. Run function 4 against files 5. Run function 5 against files I'd like to be able to run multiple... (10 Replies)
Discussion started by: rich@ardz
10 Replies

5. UNIX for Dummies Questions & Answers

Scripting menu problem

Hi there, I am new to Unix and at the moment I am trying to solve my assignment that is to create a script for the program to prompt user to type three codes, from user point of view it should be done by typing codes separating them by spaces. Then program displays a menu with these three... (5 Replies)
Discussion started by: Dannel
5 Replies

6. Shell Programming and Scripting

Call Shell scripting from Perl Scripting.

Hi How to call a shell scripting through a Perl scripting? Actually I need some value from Shell scripting and passes in the Perl scripting. So how can i do this? (2 Replies)
Discussion started by: anupdas
2 Replies

7. Shell Programming and Scripting

help with scripting a simple menu

Hi there. I'm trying to teach myself UNIX but the book I bought is a bit confusing. I'm trying out this exercise and I think I'm on the right track, but I'd appreciate any suggestions on how to improve what I have so far. Also, I'm not clear on how to use the read command to utilize the user's... (3 Replies)
Discussion started by: Jsmith
3 Replies

8. Shell Programming and Scripting

Scripting problem - when the file is not found i want it to return to the menu

when the file is not found i want it to return to the menu, however it carries out the next line when i hit a key I know its probably something simple can anyone help? here is my pause function: function pause(){ read -s -n 1 -p "Press any key to return to Menu . . ." echo } SCRIPT... (2 Replies)
Discussion started by: Alendrin
2 Replies

9. Shell Programming and Scripting

difference between AIX shell scripting and Unix shell scripting.

please give the difference between AIX shell scripting and Unix shell scripting. (2 Replies)
Discussion started by: haroonec
2 Replies

10. Shell Programming and Scripting

scripting for menu

Hi, I'm writting a script to filter a cvs log and get only the modified files to move them to a specific directory to compile. I try to filter a line and move from source to target, with no results. Could you help me? for example, in the cvs log file appears: cat log.txt U... (2 Replies)
Discussion started by: Manu
2 Replies
Login or Register to Ask a Question