2

I would like to use whiptail to create a toolbox which runs external scripts when a menu item is chosen.

I have the following code

#!/bin/bash

OPTION=$(whiptail --title "IT ToolBox v0.1" --menu "Choose your option" 15 60 4 \
"1" "Get users dropped from Active Directory" \
"2" "Work in progress" \
"3" "Work in progress" \
"4" "Work in progress"  3>&1 1>&2 2>&3)

exitstatus=$?
if [ $exitstatus = 0 ]; then
    echo "Your chosen option:" $OPTION
else
    echo "You chose Cancel."
fi

case $RETVAL in
    1) echo "Get users dropped from Active Directory"
    2) echo "Invalid option. Quitting";;
    3) echo "Invalid option. Quitting";;
    4) echo "Invalid option. Quitting";;
    *) echo "Invalid option. Quitting";;
esac

How do I tell the script to execute the file gudfad.sh when chosen? My understanding is that the command is to be added in the case area -- for example:

case $RETVAL in
    1) echo "Get users dropped from Active Directory"
       gudfad.sh;;
    2) echo "Invalid option. Quitting";;
    3) echo "Invalid option. Quitting";;
    4) echo "Invalid option. Quitting";;
    *) echo "Invalid option. Quitting";;
esac

However, when I run this and choose option 1, I get the message:

Your chosen option: 1
Invalid option. Quitting
Jeff Schaller
  • 66,199
  • 35
  • 114
  • 250
Ktakl
  • 23
  • 4

1 Answers1

4

User choice is in OPTION, not RETVAL.

Change:

case $RETVAL in

To:

case "$OPTION" in

It's good to add set -u at the top of your bash scripts so that you are warned when you use undefined variables.

It's also good to always quote your variables: "$var", not $var. There are exceptions but they are rare.

xhienne
  • 17,075
  • 2
  • 52
  • 68