# Menu Script problem

**URL:** <https://community.unix.com/t/menu-script-problem/278595>\
**Category:** UNIX for Dummies Questions & Answers\
**Created:** [December 5, 2010, 8:03pm UTC](https://community.unix.com/t/menu-script-problem/278595 "2010-12-05T20:03:51Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![christ.666](https://community.unix.com/letter_avatar/christ.666/32/6_449c533f586939b6e5f246632a46421f.png) [@christ.666](https://community.unix.com/u/christ.666)\
**Post date:** [December 5, 2010, 8:03pm UTC](https://community.unix.com/t/menu-script-problem/278595/1 "2010-12-05T20:03:51Z")

</div>

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.

```nohighlight
#!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
 

```

---

<div class="post-metadata">

**Author:** ![Chubler\_XL](https://community.unix.com/user_avatar/community.unix.com/chubler_xl/32/2077_2.png) [@Chubler\_XL](https://community.unix.com/u/Chubler_XL)\
**Post date:** [December 5, 2010, 8:32pm UTC](https://community.unix.com/t/menu-script-problem/278595/2 "2010-12-05T20:32:24Z")

</div>

Please use

````auto
```text
 and 
```
 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 \*

```nohighlight
#
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

```

---

<div class="post-metadata">

**Author:** ![christ.666](https://community.unix.com/letter_avatar/christ.666/32/6_449c533f586939b6e5f246632a46421f.png) [@christ.666](https://community.unix.com/u/christ.666)\
**Post date:** [December 5, 2010, 11:13pm UTC](https://community.unix.com/t/menu-script-problem/278595/3 "2010-12-05T23:13:24Z")

</div>

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!!!:eek:

---

<div class="post-metadata">

**Author:** ![codecaine](https://community.unix.com/letter_avatar/codecaine/32/6_449c533f586939b6e5f246632a46421f.png) [@codecaine](https://community.unix.com/u/codecaine)\
**Post date:** [December 6, 2010, 9:17pm UTC](https://community.unix.com/t/menu-script-problem/278595/4 "2010-12-06T21:17:11Z")

</div>

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.

```nohighlight
#!/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

```
