get chosen value from bash menu


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting get chosen value from bash menu
# 1  
Old 08-18-2012
get chosen value from bash menu

Hi again Smilie

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

Code:
#! /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

Code:
#! /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
# 2  
Old 08-18-2012
Try running your script like this to see what's going on:

Code:
sh -x  your-script

This User Gave Thanks to in2nix4life For This Post:
# 3  
Old 08-18-2012
Quote:
Originally Posted by in2nix4life
Try running your script like this to see what's going on:

Code:
sh -x  your-script

The OP's script is a bash script - most likely 'sh' is Bourne.
This User Gave Thanks to vgersh99 For This Post:
# 4  
Old 08-18-2012
Try putting space around the == operator, i.e.
Code:
if [ $S1 == $WM ];
        ^--^-- these spaces!

This User Gave Thanks to RudiC For This Post:
# 5  
Old 08-18-2012
Quote:
Originally Posted by RudiC
Try putting space around the == operator, i.e.
Code:
if [ $S1 == $WM ];
        ^--^-- these spaces!

Hi ALl

That was it Smilie
Perfect..cheers again
# 6  
Old 08-18-2012
Note that when using bash or ksh, theselectcommand that in available in both of those shells may provide an easier interface.
This User Gave Thanks to Don Cragun For This Post:
# 7  
Old 08-18-2012
Quote:
Originally Posted by Don Cragun
Note that when using bash or ksh, theselectcommand that in available in both of those shells may provide an easier interface.
I would disagree. select looping construct can only do so much. With whiptail, you have much much more control easily.
This User Gave Thanks to elixir_sinari For This Post:
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. 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

4. Shell Programming and Scripting

Bash menu opens and closes

Ever since I added these two code blocks to my bash menu it just opens and closes right away. I use a batch file that worked fine until these codes were added and I am not sure what is wrong. Basically, what I am trying to do in the additional section is if the answer is "Y" then it goes back... (13 Replies)
Discussion started by: cmccabe
13 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

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

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

10. Shell Programming and Scripting

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: function MainMenu { while ] do echo "--------------------------------------------------------------------------------" echo... (4 Replies)
Discussion started by: ckoeber
4 Replies
Login or Register to Ask a Question