Bash Script - Whiptail Menu Help!


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Bash Script - Whiptail Menu Help!
# 1  
Old 07-12-2014
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.


Code:
#!/bin/bash
clear

whiptail --msgbox "Entering networking sub-menu" 20 78

whiptail --title Networking --menu "Make your choice" 16 78 5 \
"1)" "Display info"
while read CHOICE
do
case $CHOICE in
1) ./displayinfo.sh
esac
done


when i choose on the choice, it only appear this.


can anyone help me spot the mistake?
Bash Script - Whiptail Menu Help!-1jpg
Bash Script - Whiptail Menu Help!-2jpg

Last edited by Scrutinizer; 07-12-2014 at 12:16 PM.. Reason: CODE tags
# 2  
Old 07-12-2014
Try this in your case statement:-
1) ./displayinfo.sh ;;
# 3  
Old 07-12-2014
Tried. it still doesnt work Smilie
# 4  
Old 07-12-2014
You need to find out how the second whiptail writes its output. Somehow the output of the second whiptail command needs to be fed into the while-read loop. There is not input into that loop here. Perhaps through a pipe symbol (if that command outputs on stdout)?

--
A short look at the man page of whiptail and the menu option, says it's outputting on stderr. But it also needs stdout to go to the terminal, so a pipe cannot be used)

===EDIT===
(corrected previous sentence (thanks Don). I originally said "background" there, which is wrong I meant "subshell". But also that is not really relevant. What matters is that pipe cannot be used like that, because the command needs stdout to go directly to the terminal..)

So try something like:

Code:
.....
whiptail --title Networking --menu "Make your choice" 16 78 5 \
  "1)" "Display info" 2>somefile
CHOICE=$(cat somefile)
case $CHOICE in
  "1)") ./displayinfo.sh
esac


Last edited by Scrutinizer; 07-13-2014 at 04:39 AM.. Reason: Corrected background to subshell. Thanks Don...
# 5  
Old 07-12-2014
Got it!

After change it to the code that you posted, it works!

However, i still not quite sure about this line

2>somefile

Is it taking the variable 2 answer and copy to a file named somefile?

Regards,

Malfolozy

Thanks for the help! Appreciate it!

Have a nice day! Smile More! Smilie
# 6  
Old 07-12-2014
I have no experience with whiptail other than reading the man page, but I think Scrutinizer is close on the solution to your problem. I am, however, a little confused by his implication that commands in a pipeline are run in the background. Pipelines are run in the foreground unless the pipeline is terminated with an & (which runs all commands in a pipeline in the background).

But, it appears from the man page (although not explicitly stated) that standard output from whiptail is supposed to be the terminal. So, the output of whiptail can't be connected to a pipe. The man page also says that the exit status needs to be checked to determine whether a yes/OK, no/cancel, or error or interrupt was detected. My guess (with no testing since my system doesn't have the whiptail command) would be that you want something like:
Code:
#!/bin/bash
clear
whiptail --msgbox "Entering networking sub-menu" 20 78
trap 'rm -f choice$$' EXIT
while whiptail --title Networking --menu "Make your choice" 16 78 5 \
	"1)" "Display info" "2)" "Another option" "3)" "something else" 2> choice$$
do	read CHOICE < choice$$
	case $CHOICE in
	("1)")	./displayinfo.sh;;
	(*)	whiptail --msgbox "To Be Implemented." 20 78;;
	esac
done

The 2>file redirects the standard error output from that command to the file named file.
This User Gave Thanks to Don Cragun For This Post:
# 7  
Old 07-13-2014
You could also try this as a way without an intermediate file, using the fact that in a command substitution stderr is directed to the the terminal. So if we flip stdout and stderr then all should be well:

Code:
#!/bin/bash
clear

whiptail --msgbox "Entering networking sub-menu" 20 78

CHOICE=$(
whiptail --title Networking --menu "Make your choice" 16 78 5 \
  "1)" "Display info" 3>&2 2>&1 1>&3 
)
case $CHOICE in
  "1)") ./displayinfo.sh
esac

---
Quote:
Originally Posted by Don Cragun
[..] I am, however, a little confused by his implication that commands in a pipeline are run in the background. Pipelines are run in the foreground unless the pipeline is terminated with an & (which runs all commands in a pipeline in the background).
Hi Don. I meant to say subshell instead of background, but even that is not relevant, what is relevant is that its stdout cannot go to the terminal, which it needs control of. I corrected it in my post. Thanks...

Last edited by Scrutinizer; 07-13-2014 at 05:25 AM..
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Passing positioning array as whiptail -- menu option

I may have asked this before, so forgive OF. Problem: I can pass positioning array as -- menu option to whiptail, but it does not show in the whiptail form as an array - only single (first member "lsusb" ) entry / line shows up. Code: DynamicEntry=$(whiptail \ --title "DEBUG... (1 Reply)
Discussion started by: annacreek
1 Replies

2. Shell Programming and Scripting

Redirecting stdout output to whiptail menu box

As a result of whiptail menu option I am getting a data from a file. Naturally it is output to terminal as stdour. I like to redirect the output back to the menu. It can be done with single input of line of text , see attached. I just cannot see where or how the sample... (0 Replies)
Discussion started by: annacreek
0 Replies

3. Shell Programming and Scripting

Creating bash script to process a data file based on the menu

Hey, im fairly new to unix and Im trying to make this unix project that would display a menu and do the following. MENU =========================== (p, P) Print users info (a, A) Add new user (s, S) Search user (d, D) Delete user (x,X) Exit Enter your choice: Trying to... (3 Replies)
Discussion started by: ultimaxtrd
3 Replies

4. Shell Programming and Scripting

Menu Driven Bash Shell Script with Default Option

Hi All, I have written a menu driven bash shell script. Current Output is as below: ------------------------------------- Main Menu ------------------------------------- Option 1 Option 2 Option 3 Option 4 Exit ===================================== Enter your... (3 Replies)
Discussion started by: kiran_j
3 Replies

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

6. Shell Programming and Scripting

Whiptail menu, getting back the variable

Hi all Only learning so if any mistakes, let me know I am trying to create a menu box with Whiptail, taking in the variables from a txt.file called Name.txt which has just 4 names listed for now, one below each other..ie Dave John Mike Mary Bash script is below and calls the txt... (8 Replies)
Discussion started by: olearydc
8 Replies

7. Shell Programming and Scripting

Menu in Menu script issue

Problem: I am trying to create a menu in a menu script and I am running into an issue with the calculator portion of the script. I am first presented with the ==Options Menu== which all 5 options working correctly. Now comes the fun part. I select option 1 which takes me to my ==Calculator... (1 Reply)
Discussion started by: iDdraig
1 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

Execute interactive bash menu script in browser with PHP

I have an interactive menu script written in bash and I would like use PHP to open the interactive bash menu in a browser. Is this possible? Using the sytem() function in php runs the script but it's all garbled. Seems like maybe a terminal window needs to be opened in php first? ... (1 Reply)
Discussion started by: nck
1 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