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 "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?

Try this in your case statement:-
1) ./displayinfo.sh ;;

Tried. it still doesnt work :frowning:

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:

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

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! :slight_smile:

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:

#!/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.

1 Like

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:

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

---

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

Alright got it.

The following code doesn't seem to be working

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

However, the one that directed to "somefile" works just fine. Therefore, i'll stick to that code as it is clearer.

----------------------------------------------------------------

And, there's one more question that i would like to ask.

Basically, i have two network card installed and i would like the code display the information of the selected network card and to display only available network card as the choices for the user. How do i go about doing that in Whiptail?

I tried doing it in "Select" and it work like this.

#!/bin/bash
clear

presentNIC=$(ifconfig -a | sed -n 's/^\([^ ]\+\).*/\1/p')

echo "Present NIC: "
echo ""
echo $presentNIC
echo ""

read -p "Which NIC?" ansNIC

echo ""
echo $(ifconfig $ansNIC)
echo ""

And when i tried to do it in the Whiptail, the answer stack as one choice... someone guide me pls.

#!/bin/bash
clear

presentNIC=$(ifconfig -a | sed -n 's/^\([^ ]\+\).*/\1/p')

whiptail --title "Networking" --menu "Make your choice" 16 78 5 \
"1." "$presentNIC" 2>somefile
CHOICE=$(cat somefile)
             case $CHOICE in
             "1)") *link it to chosen network card* ;;
             esac

Stacked as one choice