The UNIX and Linux Forums  
Hello and Welcome from United States to the UNIX and Linux Forums! Thank You for Visiting and Joining Our Global Community.

Go Back   The UNIX and Linux Forums > Top Forums > Shell Programming and Scripting
.
google unix.com



Shell Programming and Scripting Post questions about KSH, CSH, SH, BASH, PERL, PHP, SED, AWK and OTHER shell scripts and shell scripting languages here.

More UNIX and Linux Forum Topics You Might Find Helpful
Thread Thread Starter Forum Replies Last Post
Bash : how do i check the user input and make sure is only chracter or only number ? CheeSen Shell Programming and Scripting 7 09-07-2008 07:06 AM
Bash - Add User script niels.intl Shell Programming and Scripting 4 05-10-2008 10:24 PM
Problem setting up Bash user account sumanroyc UNIX for Advanced & Expert Users 2 10-27-2006 05:57 PM
Drop down menu in bash for timezone select simonb Shell Programming and Scripting 1 04-29-2006 01:02 PM
Help with Flat Files Please!! BASH (New User) cyberjax21 Shell Programming and Scripting 0 02-27-2006 09:19 PM

Closed Thread
English Japanese Spanish French German Portuguese Italian Dutch Swedish Russian Norwegian Hungarian Hebrew Danish Bulgarian Greek Powered by Powered by Google
 
LinkBack Thread Tools Search this Thread Rate Thread Display Modes
  #1 (permalink)  
Old 10-10-2008
ckoeber ckoeber is offline
Registered User
  
 

Join Date: Oct 2008
Posts: 4
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 (permalink)  
Old 10-10-2008
zaxxon's Avatar
zaxxon zaxxon is offline Forum Staff  
Moderator
  
 

Join Date: Sep 2007
Location: Germany
Posts: 2,277
Change !== vs != and try again.
  #3 (permalink)  
Old 10-10-2008
ckoeber ckoeber is offline
Registered User
  
 

Join Date: Oct 2008
Posts: 4
That worked!

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

Chris K.
  #4 (permalink)  
Old 10-10-2008
zaxxon's Avatar
zaxxon zaxxon is offline Forum Staff  
Moderator
  
 

Join Date: Sep 2007
Location: Germany
Posts: 2,277
No need to feel dumb - happens to everyone sometimes
  #5 (permalink)  
Old 10-10-2008
cfajohnson's Avatar
cfajohnson cfajohnson is offline Forum Advisor  
Shell programmer, author
  
 

Join Date: Mar 2007
Location: Toronto, Canada
Posts: 2,361
Quote:
Originally Posted by ckoeber View Post
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 ;;
Closed Thread

Bookmarks

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On




All times are GMT -4. The time now is 04:51 PM.


Powered by: vBulletin, Copyright ©2000 - 2006, Jelsoft Enterprises Limited. Language Translations Powered by .
vBCredits v1.4 Copyright ©2007 - 2008, PixelFX Studios
The UNIX and Linux Forums Content Copyright ©1993-2009. All Rights Reserved.Ad Management by RedTyger

Content Relevant URLs by vBSEO 3.2.0