Presenting the user a menu in Bash ...


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Presenting the user a menu in Bash ...
# 1  
Old 10-10-2008
Presenting the user a menu in Bash ...

Hello,

I am trying to write a bash script function to present the user a menu of options. The following is what I have so far:

Code:
function MainMenu {
    while [[ "$USR_CHOICE" !== "4" || "$USR_CHOICE" !== "four" || "$USR_CHOICE" !== "Four" ]]
    do
        echo "--------------------------------------------------------------------------------"
        echo "-----------------------------------Main Menu------------------------------------"
        echo "--------------------------------------------------------------------------------"
        echo "Select from the following options:"
        echo "----------------------------------"
        echo ""
        echo "        1. Option One"
        echo "        2. Option Two"
        echo "        3. Option Three"
        echo "        4. Exit"
        echo ""
        read -r -p "Type in the desired option: " USR_CHOICE
        case $USR_CHOICE in
        1|"1"|one|One)
            ;;
        2|"2"|two|Two)
            ;;
        3|"3"|three|Three)
            ;;
        4|"4"|four|Four)
            ;;
        *)
            #Error occured in script
            ;;
        esac
    done
}

The problem is that when the script is run I get the following error message back:

Code:
./deploy.sh: line 14: conditional binary operator expected
./deploy.sh: line 14: syntax error near `!=='
./deploy.sh: line 14: ` while [[ "$USR_CHOICE" !== "4" || "$USR_CHOICE" !== "four" || "$USR_CHOICE" !== "Four" ]]'

So, the issue I am having is how can I run the loop of the manu menu as my function is intended to provide?

Thanks for the time.
# 2  
Old 10-10-2008
Change !== vs != and try again.
# 3  
Old 10-10-2008
That worked!

I feel kinda dumb. Oh well. Thanks for the assistance!

Chris K.
# 4  
Old 10-10-2008
No need to feel dumb - happens to everyone sometimes Smilie
# 5  
Old 10-10-2008
Quote:
Originally Posted by ckoeber
Hello,

I am trying to write a bash script function to present the user a menu of options. The following is what I have so far:

Code:
function MainMenu {
    while [[ "$USR_CHOICE" !== "4" || "$USR_CHOICE" !== "four" || "$USR_CHOICE" !== "Four" ]]


I recommend using standard syntax so that your script is portable (and fixing the !== operator):

Code:
MainMenu() {
  while [ "$USR_CHOICE" != "4" ] || [ "$USR_CHOICE" != "four" ] || [ "$USR_CHOICE" != "Four" ]

However, are you sure of the logic?

I would use while : and put the exit in the case statement.

Quote:
Code:
    do
        echo "--------------------------------------------------------------------------------"
        echo "-----------------------------------Main Menu------------------------------------"
        echo "--------------------------------------------------------------------------------"
        echo "Select from the following options:"
        echo "----------------------------------"
        echo ""
        echo "        1. Option One"
        echo "        2. Option Two"
        echo "        3. Option Three"
        echo "        4. Exit"
        echo ""
        read -r -p "Type in the desired option: " USR_CHOICE
        case $USR_CHOICE in
        1|"1"|one|One)


You are testing for the same thing twice. You don't need both 1 and "1".
Quote:
Code:
            ;;
        2|"2"|two|Two)
            ;;
        3|"3"|three|Three)
            ;;
        4|"4"|four|Four)
            ;;


Code:
4|[fF]our)
           break ;;

Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Bash Menu using zenity

Hi, I'm new to bash, and have an example menu script using Zenity. It works fine if the user enters A B or C, but if the user enters nothing, I can only figure out how to exit the script. How do I get the menu to reappear if no input is selected? The script is: title="Select example"... (2 Replies)
Discussion started by: allen11
2 Replies

2. Shell Programming and Scripting

Bash menu item counter

We have a simple menu with prompt of menu numbers to user. It is still under construction. Is there a way to "count" the menu choices so the prompt maximum count can be changed dynamically? See attached TODO note in code read_options(){ local choice # the... (7 Replies)
Discussion started by: annacreek
7 Replies

3. Shell Programming and Scripting

Display user selction from bash menu

I am trying to display the text the user selects from a bash menu. The below will display the menu and allow the user to enter a number, but will not display the choice selected. bash while true do printf "\n please make a selection from the MENU \n ... (4 Replies)
Discussion started by: cmccabe
4 Replies

4. Open Source

Bash menu not running

The perl command is not executing? I am trying to run the .pl in my cygwin home directory (C:\cygwin\home\cmccabe) using ${id}.txt.hg19_multianno.txt (located in the annovar directory) as the input file to be formatted and $FILENAME is the output file to be saved. The .pl is attached as... (8 Replies)
Discussion started by: cmccabe
8 Replies

5. Shell Programming and Scripting

Bash Script - Whiptail Menu Help!

Hello, Been trying to build a menu with whiptail lately. However, my code doesn't seems to be working even though when i compared to other similar code they looks the same. #!/bin/bash clear whiptail --msgbox "Entering networking sub-menu" 20 78 whiptail --title Networking --menu... (8 Replies)
Discussion started by: malfolozy
8 Replies

6. Red Hat

Bash: menu-complete and reverse

Hi, In the archives I found this: And this works fine. $if mode=vi "\C-0-": digit-argument TAB: menu-complete "\e But what I want is to reverse this. So I want that tab does reverse menu completion and shift tab does normal menu completion. Can anyone help me with this? Thanks (0 Replies)
Discussion started by: ozkanb
0 Replies

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

8. Shell Programming and Scripting

get chosen value from bash menu

Hi again :) This is just a sample whiptail menu. Works great, but have been trying to get the chosen value into a variable but failing pretty bad...its ther but unsure how to echo it out when needed #! /bin/bash #This is the menu whiptail --title "Menu example" --menu "Choose an... (9 Replies)
Discussion started by: olearydc
9 Replies

9. Shell Programming and Scripting

Help regarding a bash menu script

Greetings all, I'm having some trouble writing a menu drive bash script, actually coding the menu part was not difficult however its a problem with a menu option I'm having trouble with. My menu has 5 options, when the user selects the second option, they are then prompted to enter a number from... (5 Replies)
Discussion started by: Vitrophyre
5 Replies

10. Shell Programming and Scripting

Bash menu script

I have a main menu quit=n while do clear echo echo "1. General system information" echo "2. Hardware utilisation information" echo "3. File management" echo "4. User information" echo "5. Information on network connectivity" echo "6. Information on processes" echo "Q.Quit" ... (3 Replies)
Discussion started by: AngelFlesh
3 Replies
Login or Register to Ask a Question