Loading...

Use whiptail in interactive shell scripts

Whiptail is a program that allows shell scripts to display dialog boxes to the user for informational purposes, or to get input from the user in a friendly way. It is included in several linux distributions like Debian or rhel.

Following script demonstrates a shell script skeleton for a rpm cleanup script. Whiptail is only displayed in interactive mode (run the script with bash -i).

#!/bin/bash
do_rpm_cleanup(){
  RPM=`which rpm`
  if [ "$RPM"="" ]; then
    echo "no rpm installed"
  else
    echo "do cleanup"
    $RPM --rebuildb
  fi
}
if [[ "$-" == *i* ]]; then
  if whiptail --title "Cleanup RPM" --yesno "Are you sure?" 10 40 ;then
    do_rpm_cleanup
  else
    echo "Cleanup aborted"
  fi
else
  echo "do something old school"
  do_rpm_cleanup
fi