Menu Script problem


 
Thread Tools Search this Thread
Top Forums UNIX for Dummies Questions & Answers Menu Script problem
# 1  
Old 12-05-2010
Question Menu Script problem

Im new to unix/linux and am having trouble with this one.
My problem is when i enter 9 to exit it doesnt do so.
also option 6 doesent display the time. Its getting to be frustrating.
I know there are probably alot of bug in this bu these basic ones are really odd.
Code:
#!bin/bash 

#
until [ "$userIn" = "10"  ]   #User controllerd until <9> is entered
or main menu
  do
       clear
       echo "Welcome to Christopher Krause's Main Menu"
       echo
       echo "1: Users Curretnly Logged On "
       echo "2: Display Calender for chosen Month and Year"
       echo "3: Diplay Current Directory Path"
       echo "4: Change Directory"
       echo "5: List of Files in Current Directory "
       echo "6: Display Current Time & Date & Calendar "
       echo "7: Start the vi editor "
       echo "8: Email a file to a user "
       echo "9: Quit "
       echo  
       echo -n "Please enter your option and hit <Enter>: "  #User prompt
       read userIn                #Storing available directory
       case "$userIn" in          #Users input 
          1)   clear
               echo
               echo "Users Curretnly Logged In:"
               who | cut -d\  -f1
#Program description
               echo "Press <Enter> to return to menu"
               read null
               ;;
          2)   clear
               echo -n "Plese enter the month in the form MM:"
#
               read month
               if [ $month -lt 1 -o $month -gt 12 ]
                  then
                     echo "$month is not a valid month!!"
                  else
                    echo -n "Please enter a yeat in the form of YYYY: "
                    read year
                    if [ $year -lt 0 -o $year -gt 3000 ]
                       then 
                          echo "$year is not a valid year!!"
                       else
                          cal $month $year
                  fi
                fi
               echo 
               echo "press <Enter> to return to main menu."
               read null
               ;;
          3)   clear
               echo "Your directory path is: "
               echo
               pwd    #pwd command is intiated and displayed
               echo "press <Enter> to return to main menu."
               read null
               ;;
          4)   clear
               echo -n " Enter new directroy path."
               read userdir
               if [ $userdir ]
                  then 
                   cd $userdir
                  elif [ -z $userdir ]
                   then 
                     cd ~
                  elif [ $userdir = "~" ]
                   then 
                     cd ~
                  elif [ $userdir = "/$HOME"
                   then 
                     cd ~
                  else
                   echo "$userdir is not a valid directory!!"
                   echo
                   echo "press <Enter> to go to main menu."
                   read null
               fi
               ;;
          5)   clear 
               ls -l | more 
               echo 
               echo "press <Enter> to return to main menu."
               echo
               ;;
          6)   clear
               echo -n "The current date and time is: "
               echo
               date
               echo
               echo "press <Enter> to return to main menu."
               ;;
          7)   clear
               echo "Please enter the file you want to edit."
               echo "{Leave blank for new file}"
               echo
               echo -n "File: "      
               read vifile   #Valid file is stored
               filetype='file "$vifile" | cut -d\ -f2'
               if [ $filetype = "ASCII" ]
                  then 
                     vi $vifile
                  elif [ $filetyoe = "cannot" ]
                     then
                     vi $vifile
                  elif [ -z $vifile ]
                     then
                        vi
                  else "$vifile is not a valid text file."
               fi
               echo
               echo "press <Enter> to return to main menu."
               read null
               ;;
          8)   clear
               validuser=1
               until [ $validuser -eq 0 ]
               do 
                 echo -n "Please enter the recipients name and press <Enter>."
               read emailname
                 cat /etc/passwd | cut -d: -f1 | grep $emailname 1> /etc
                  if [ $7 -eq 0 ]
                   then
                      validuser=0
                   else echo "$emailname is not a valid user on 'hostname'"
                 fi
               done
               echo -n Please enter the subject of email and press <Enter>."
               read emailsubject
               done
               validfile=1
               until [ $validfile -eq 0 ]
               do 
                  echo -n "Please enter the file to be attached and press <Enter>."
                  read emailfile
                  validfile='file "$emailfile" | cut -d\  -f2'
                  if [$validfile = "ASCII" ]
                      then
                         validfile=0
                      else 
                         echo "$emailfile is not a text file!!!"
                  fi
                done
                'mail -s "$emailsubject" "$emailuser" < "$emailfile"'
               ;
          9)   echo
               echo "You have chosen to quit. Goodbye!!!"
               exit 1
               ;;
          *)   echo 
               echo ""$userIn" is not a valid option. Please try again."
               echo
               echo "
               ;; 
         esac
done


Last edited by Scott; 12-06-2010 at 02:01 AM.. Reason: Added code tags
# 2  
Old 12-05-2010
Please use [code] and [/code] tags

Issues found (shown in red in code below)

- Comment wrapped to next line without continue
- Missing a couple of read null after messages (case 5 & 6)
- else statement in case 8
- Missing a quote in case 8
- Missing ; in ;; of case 8
- Missing quote and end of echo in case *

Code:
#
until [ "$userIn" = "10" ] #User controllerd until <9> is entered or main menu
do
    clear
        echo "Welcome to Christopher Krause's Main Menu"
        echo
        echo "1: Users Curretnly Logged On "
        echo "2: Display Calender for chosen Month and Year"
        echo "3: Diplay Current Directory Path"
        echo "4: Change Directory"
        echo "5: List of Files in Current Directory "
        echo "6: Display Current Time & Date & Calendar "
        echo "7: Start the vi editor "
        echo "8: Email a file to a user "
        echo "9: Quit "
        echo 
        echo -n "Please enter your option and hit <Enter>: " #User prompt
        read userIn #Storing available directory
        case "$userIn" in #Users input 
            1) clear
               echo
               echo "Users Curretnly Logged In:"
               who | cut -d\ -f1
               #Program description
               echo "Press <Enter> to return to menu"
               read null
            ;;
            2) clear
               echo -n "Plese enter the month in the form MM:"
               #
               read month
               if [ $month -lt 1 -o $month -gt 12 ]
               then
               echo "$month is not a valid month!!"
               else
               echo -n "Please enter a yeat in the form of YYYY: "
               read year
               if [ $year -lt 0 -o $year -gt 3000 ]
               then 
               echo "$year is not a valid year!!"
               else
               cal $month $year
               fi
               fi
               echo 
               echo "press <Enter> to return to main menu."
               read null
            ;;
            4) clear
               echo -n " Enter new directroy path."
               read userdir
               if [ $userdir ]
               then 
                   cd $userdir
               elif [ -z $userdir ]
               then 
                   cd ~
               elif [ $userdir = "~" ]
               then 
                   cd ~
               elif [ $userdir = "/$HOME"
               then 
                   cd ~
               else
                   echo "$userdir is not a valid directory!!"
                   echo
                   echo "press <Enter> to go to main menu."
                   read null
               fi
            ;;
            5) clear 
               ls -l | more 
               echo 
               echo "press <Enter> to return to main menu."
               read null
            ;;
            6) clear
               echo -n "The current date and time is: "
               echo
               date
               echo
               echo "press <Enter> to return to main menu."
               read null
            ;;
            7) clear
               echo "Please enter the file you want to edit."
               echo "{Leave blank for new file}"
               echo
               echo -n "File: " 
               read vifile #Valid file is stored
               filetype='file "$vifile" | cut -d\ -f2'
               if [ $filetype = "ASCII" ]
               then 
                   vi $vifile
               elif [ $filetyoe = "cannot" ]
               then
                   vi $vifile
               elif [ -z $vifile ]
               then
                   vi
               else "$vifile is not a valid text file."
               fi
               echo
               echo "press <Enter> to return to main menu."
               read null
            ;;
            8) clear
               validuser=1
               until [ $validuser -eq 0 ]
               do 
                   echo -n "Please enter the recipients name and press <Enter>."
                   read emailname
                   cat /etc/passwd | cut -d: -f1 | grep $emailname 1> /etc
                   if [ $7 -eq 0 ]
                   then
                       validuser=0
                   else 
                          echo "$emailname is not a valid user on 'hostname'"
                   fi
               done
               echo -n "Please enter the subject of email and press <Enter>."
               read emailsubject
               validfile=1
               until [ $validfile -eq 0 ]
               do 
                   echo -n "Please enter the file to be attached and press <Enter>."
                   read emailfile
                   validfile='file "$emailfile" | cut -d\ -f2'
                   if [$validfile = "ASCII" ]
                   then
                       validfile=0
                   else 
                       echo "$emailfile is not a text file!!!"
                   fi
               done
               'mail -s "$emailsubject" "$emailuser" < "$emailfile"'
            ;;
            9) echo
               echo "You have chosen to quit. Goodbye!!!"
               exit 1
            ;;
            *) echo 
               echo ""$userIn" is not a valid option. Please try again."
               echo
               echo ""
            ;; 
        esac
done

# 3  
Old 12-06-2010
Re: Menu Script

thanks;
I still have a lot of bugs to workout.
That atleast helped with some minor syntax.
Scripting has been real fun, trying to troubleshoot and what not.
thanks again!!!Smilie
# 4  
Old 12-06-2010
There is also a select command for BASH to build menus. Here is a example

The select command allows you to create bash script with menus. The select command uses the PS3 value for the prompts. Each menu items is separated by spaces in a variables as you can see in my OPTIONS variable. The beginning of the command is select a variable name for checking the options and a list of menu options. For example: select any_variable_name in menu_options. If you do not add break commands within the select command it will be a infinite it loop and you have to terminate using ctrl+c. Below I have posted a example of using the select command.
Code:
#!/bin/bash

# Purpose: This program shows you a example of using the select command to create menus
# Author: codecaine aka Jerome Scott II 
# Date: 8/10/2010

PS3='Select a choice: '

OPTIONS='www.freelancecode.net www.pro9ramming.com Exit'

select var in $OPTIONS
do
    if [ $var = 'www.freelancecode.net' ]; then
        echo You choosed www.freelancecode.net
        break
    elif [ $var = 'www.pro9ramming.com' ]; then
        echo You choosed www.pro9ramming.com
        break
    elif [ $var = 'Exit' ]; then
        break
    fi
done

 
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. UNIX for Beginners Questions & Answers

Gnome 3.28.3 menu item dissapears under the system menu

I installed CentOS 8 with Gnome 3.28.2 and I noticed that the "switch user" menu item disappeared from under the system menu of Gnome classic (Both X11 & Wayland). I checked google and this problem seems to have a history going back several releases of Gnome. Unfortunately, I never found a... (1 Reply)
Discussion started by: bodisha
1 Replies

2. Linux

Problem with menu

Everything in the following script works fine, except the jobs command. It works when the script is not running, but does not work in the script itself and I cannot figure out why. Any help would be greatly appreciated. using bash shell clear while echo " A: Jobs" echo " B: PS" echo " C:... (2 Replies)
Discussion started by: silencep77
2 Replies

3. Shell Programming and Scripting

Execution Problem with dispalying file content using menu driven script

HI All.. below is my menu options script. in option 2,3 and 4 im giving input and they are saving into their respective text file. problem is when im trying to "cat" those files in options 7,8 and 9 im not getting the output. no respective file contents are displaying on screen. but if i... (1 Reply)
Discussion started by: saichand1985
1 Replies

4. Shell Programming and Scripting

Shell script menu problem

I have tried searching the forum but i haven't found a solution for this. I have a shell script that presents the users with menus. The menus branch out to sub menus. It is all hunky dory as long as i traverse forward. But if i am in a sub menu and return to the previous menu and choose any... (11 Replies)
Discussion started by: goddevil
11 Replies

5. Shell Programming and Scripting

Menu in Menu script issue

Problem: I am trying to create a menu in a menu script and I am running into an issue with the calculator portion of the script. I am first presented with the ==Options Menu== which all 5 options working correctly. Now comes the fun part. I select option 1 which takes me to my ==Calculator... (1 Reply)
Discussion started by: iDdraig
1 Replies

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

7. Shell Programming and Scripting

Script 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... (2 Replies)
Discussion started by: Dannel
2 Replies

8. Shell Programming and Scripting

Ive got a problem with menu sub commands

Ive got a problem fron a menu script where: If i choose option 1 it will run a command for instance option 1 will run another script that will grep for a certain process name also using the If command. IE if chicken process is present echo cluck cluck, but if no chicken then echo no eggs... (4 Replies)
Discussion started by: wmccull
4 Replies

9. Ubuntu

Start Menu Problem

I posted this at another forum 25 minutes ago and it has only been looked at once. I have much more faith in this forum, as I've had some good help in the shell scripting forum. So I'll just repost this here. I don't know if this is an (K)Ubuntu specific problem, but that is what I use. Problem... (0 Replies)
Discussion started by: notsomeone
0 Replies

10. 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
Login or Register to Ask a Question