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
  • 1
    $RETVAL is an empty string. – Ipor Sircer Jan 16 '17 at 11:58
  • Don't forget to click the checkmark to accept an answer that solves your problem! – Jeff Schaller Jan 16 '17 at 19:25
2

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.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.