help with scripting a simple menu


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting help with scripting a simple menu
# 1  
Old 06-06-2009
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 input (#'s 4-7). I appreciate your help!

Code:
 
#!/bin/bash
while:
do
clear
# Display menu
echo
echo  "*************************************************************"    
echo  "Please choose from the following options; type the" 
echo  "option number and hit the <Enter> key."
echo
echo  "   1) To list names of the files in the current DIR"
echo  "   2) Display today's date and time"
echo  "   3) Display a sorted list of people currently logged on"
echo  "   4) Display whether a file is a file or a DIR"
echo  "   5) Create a backup for a file"
echo  "   6) Find a user by First of Last name in /etc/passwd file"
echo  "   7) Find the manual pages for a specific command"
echo  "   8) Exit"
echo
echo  "*************************************************************"  
 
read option
 
case "$option" in 
    1)    echo "The files in the current DIR are: " 
          ls -l 
          echo "Hit <Enter> to continue." 
          read ignore 
          ;; 
 
    2)    echo "The current date is: " 
          date 
          unset date 
          echo "Hit <Enter> to continue." 
          read ignore 
          ;; 
 
    3)    echo "The following is a list of the academic scholars" 
          echo "currently logged in:" 
          who | cut -d " " -f1 | sort     
          echo "Hit <Enter> to continue." 
          read ignore 
          ;; 
 
    4)    echo "Enter file to determine whether a "simple" file or a" 
          echo "directory:" 
 
    5)    echo "Enter the file you would like to create a backup for:" 
 
 
    6)    echo "Enter user's first or last name:" 
 
 
    7)    echo "Enter command for which you would like the manual:" 
 
 
    8)    echo "Thank You for using Skynet" 
          echo "Remember: Skynet is the Future" 
          sleep 1.5 
          clear 
          exit 0 
          ;; 
 
    *)   echo "Invalid option; try running the program again." 
         exit 1 
         ;;    
 
esac
 
done.
 
exit 0

# 2  
Old 06-06-2009
Are you sure its not homework??SmilieSmilie
hmmm anyways
read <varname>
the <varname> will store whatever entered from Standard I/O
you can use $<varname> where ever you want
# 3  
Old 06-06-2009
Quote:
Originally Posted by vidyadhar85
Are you sure its not homework??SmilieSmilie
hmmm anyways
read <varname>
the <varname> will store whatever entered from Standard I/O
you can use $<varname> where ever you want
Not homework I swear! I'm 32 years old and only got my first computer ever last year. Before that the most I had ever done with one is clock in and out of my old warehouse job.
# 4  
Old 06-06-2009
Quote:
Originally Posted by Jsmith
... Also, I'm not clear on how to use the read command to utilize the user's input (#'s 4-7).
...
You show a prompt to the user if they choose 4 through 7 from the menu. You will need to capture the response of that prompt again, and run the relevant command - "cp" for backup, "grep" for /etc/passwd lookup, "man" for display of manual page etc. Also, you can perform a couple of file tests to determine if the input value in #4 is a regular file or a directory or something else.
In the command pipeline of #3, you may want to use "sort -u" to display unique users, since the same user can be displayed more than once in the output of "who" command.

I've tweaked your script a bit, incorporating these suggestions -

Code:
$                                                            
$ cat menu.sh                                                
#!/bin/bash                                                  
#set -vx                                                     

while :
do     
  clear
  # Display menu
  echo          
  echo  "*************************************************************"
  echo  "Please choose from the following options; type the"           
  echo  "option number and hit the <Enter> key."                       
  echo                                                                 
  echo  "   1) To list names of the files in the current DIR"          
  echo  "   2) Display today's date and time"                          
  echo  "   3) Display a sorted list of people currently logged on"    
  echo  "   4) Display whether a file is a file or a DIR"              
  echo  "   5) Create a backup for a file"                             
  echo  "   6) Find a user by First of Last name in /etc/passwd file"  
  echo  "   7) Find the manual pages for a specific command"           
  echo  "   8) Exit"                                                   
  echo                                                                 
  echo  "*************************************************************"
  read option                                                          
  case "$option" in                                                    
      1)    echo "The files in the current DIR are: "                  
            ls -l                                                      
            echo "Hit <Enter> to continue."                            
            read ignore                                                
            ;;                                                         
      2)    echo "The current date is: "                               
            date                                                       
            unset date                                                 
            echo "Hit <Enter> to continue."                            
            read ignore                                                
            ;;                                                         
      3)    echo "The following is a list of the academic scholars"    
            echo "currently logged in:"                                
            who | cut -d " " -f1 | sort -u                             
            echo "Hit <Enter> to continue."                            
            read ignore                                                
            ;;                                                         
      4)    echo "Enter file to determine whether a simple file or a directory:"
            read fdname                                                         
            if [ ! -e $fdname ]; then                                           
              echo "$fdname does not exist."                                    
            elif [ -d $fdname ]; then                                           
              echo "$fdname is a directory."                                    
            elif [ -f $fdname ]; then                                           
              echo "$fdname is a regular file."                                 
            else                                                                
              echo "$fdname is something else."                                 
            fi                                                                  
            echo "Hit <Enter> to continue."                                     
            read ignore                                                         
            ;;                                                                  
      5)    echo "Enter the file you would like to create a backup for:"        
            read fname                                                          
            if [ ! -e $fname ]; then                                            
              echo "$fname does not exist."                                     
            elif [ ! -f $fname ]; then                                          
              echo "$fname is not a regular file."                              
            else                                                                
              cp "$fname" "$fname.bkp"                                          
              echo "Created backup $fname.bkp for $fname"
            fi
            echo "Hit <Enter> to continue."
            read ignore
            ;;
      6)    echo "Enter user's first or last name:"
            read flname
            grep $flname /etc/passwd
            if [ $? -ne 0 ]; then
              echo "Sorry, $flname was not found in /etc/passwd"
            fi
            echo "Hit <Enter> to continue."
            read ignore
            ;;
      7)    echo "Enter command for which you would like the manual:"
            read cmd
            man $cmd
            echo "Hit <Enter> to continue."
            read ignore
            ;;
      8)    echo "Thank You for using Skynet"
            echo "Remember: Skynet is the Future"
            sleep 1.5
            break
            ;;
      *)   echo "Invalid option; valid ones are from 1 to 8"
           echo "Hit <Enter> to continue."
           read ignore
           ;;
  esac
done

$
$

Hope that helps,
tyler_durden
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. Homework & Coursework Questions

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... (13 Replies)
Discussion started by: Jagst3r21
13 Replies

3. Shell Programming and Scripting

Simple calculator with menu input - Need Help

I am trying to make a calculator. The user Enters number 1, chooses and operation, enters number 2, then chooses another operation or for the answer to be displayed. eg. 1 + 1 = or 1 + 1 + 2 + 1 = Both of these should be possible. #!/bin/bash read -p "what's the first number? " n1... (3 Replies)
Discussion started by: redshine6
3 Replies

4. UNIX for Dummies Questions & Answers

Simple bash script menu

Dear Sir, May I know how do I go about adding the following feature into the script below: When user enter values other than 1,2,3,4, a) Message “Wrong entry !!! Pls select 1,2,3 or 4” is displayed b) The screen is cleared again and the menu is displayed. #!/bin/bash clear var=1... (2 Replies)
Discussion started by: fusetrips
2 Replies

5. Shell Programming and Scripting

Two columns output in simple case menu?

EDIT : System used : Any Linux distribution. Hello everyone, I m having quite a headache trying to figure out why I m having a 2 columns output in the following code : #!/bin/ksh menu_rotation() { #Variables CHOIX1="Rotation 1" CHOIX2="Rotation 2" CHOIX3="Rotation 3" ... (11 Replies)
Discussion started by: Sekullos
11 Replies

6. Shell Programming and Scripting

Perl simple text menu with options

Hopefully I'm in the right place. Im new to the forums and linux! I'm looking to add a menu to my perl hangman game i have created. The menu will use user input for the desired option and then perform the operation indicated. I would like something along the lines of: Welcome to Hangman... (1 Reply)
Discussion started by: jahburmski
1 Replies

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

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

Simple Menu and coding

I am very new to Unix and don't know much about it. I've been trying to create a menu and what I don't understand is how to execute a command once a user makes a selection. I have the menu set up. In fact, the following is the code that I have thus far: #! /bin/csh # This is the UNIX menu... (0 Replies)
Discussion started by: sinjin
0 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