get chosen value from bash menu

Hi again :slight_smile:

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 option" 20 78 16 \
"<-- Back" "Return to the main menu." \
"Add User" "Add a user to the system." \
"Modify User" "Modify an existing user." \
"List Users" "List all users on the system." \
"Add Group" "Add a user group to the system." \
"Modify Group" "Modify a group and its list of members." \
"List Groups" "List all groups on the system."
 
#want to grab the result here and place in a varible called 'choice' and just echo it out if possible

#This shows exit status
exitstatus=$?
if [ $exitstatus = 0 ]; then
echo "User selected Ok"
echo "User eneterd $choice"
else
echo "User selected Cancel."
fi

Any ideas would be great..thanks again

---------- Post updated at 08:29 AM ---------- Previous update was at 06:12 AM ----------

Been messing more and able to get it going a bit
and mofifies it to something like this

The only thing is, I get my results OK...but the if, then choice does not do as expected

Below is a example

#! /bin/bash
#
WM=$(whiptail --backtitle "Hi" --title "These are the names" --radiolist "Please select what is is to be turned on or off" 30 60 20 "ON" "Power on " ON "OFF" "Power OFF " OFF 3>&1 1>&2 2>&3)
echo "User selected Ok and entered $WM"

S1='ON'
S2='OFF'
 
if [ $S1==$WM ];
then
echo "You pressed ON('$WM')"
else
 
echo "You Pressed OFF('$WM')"
fi

What ever I press, I get the You pressed ON option

Try running your script like this to see what's going on:

sh -x  your-script
1 Like

The OP's script is a bash script - most likely 'sh' is Bourne.

1 Like

Try putting space around the == operator, i.e.

if [ $S1 == $WM ];
        ^--^-- these spaces!
1 Like

Hi ALl

That was it :slight_smile:
Perfect..cheers again

Note that when using bash or ksh, the select command that in available in both of those shells may provide an easier interface.

1 Like

I would disagree. select looping construct can only do so much. With whiptail, you have much much more control easily.

1 Like

I agree that select is limited, but it is available anywhere bash or ksh is available. The whiptail utility is only available on Linux systems. If you're trying to write code that is portable to more than just Linux systems, whiptail is not an option. For example, it isn't available on any of the Solaris and OS X systems I use.

1 Like

There is nothing in the script that is specifically bash other than == ; = works just as well and is portable.

1 Like

Thanks all

I was using Linux...new to that and scripting :slight_smile:

lots more to learn...but thanks for pointing me in the right direction