Search code examples
bashwhiptail

handle errors in whiptail gauge with a msgbox


I am trying to run a whiptail gauge where I am showing progress as multiple commands are run. I have some error checking, but since the commands are running in a subshell, I can't seem to display the error in a msgbox. Any help would be greatly appreciated! Below is a sample of what I have tried:

#!/usr/bin/env bash
{
   echo 0
   some_cmd1
   if [ $? -ne 0 ]; then
      whiptail --msgbox "error running cmd" 8 48
   fi
   echo 20
   some_cmd2
   if [ $? -ne 0 ]; then
      whiptail --msgbox "error running cmd" 8 48
   fi
   ...
   echo 100
} | whiptail --gauge "checking pre-reqs..." 8 48 0

I've also tried returning a status code, but I'm at a loss of how to utilize it if since it's being piped into whiptail

#!/usr/bin/env bash
{
   echo 0
   some_cmd1
   if [ $? -ne 0 ]; then
      echo "error running cmd"
      return 1
   fi
   echo 20
   some_cmd2
   if [ $? -ne 0 ]; then
      echo "error running cmd"
      return 1
   fi
   ...
   echo 100
} | whiptail --gauge "checking pre-reqs..." 8 48 0

Also, if I'm going about this wrong, please let me know as well!


Solution

  • Like this:

    #!/usr/bin/env bash
    
    myfunc() {
       echo 0
    
       sleep 1
    
       echo 20
       sleep 1
       if ! false; then
          whiptail --msgbox "error running cmd" 8 48 >&2
          return 1
       fi
       :
       echo 100
       sleep 1
    }
    
    myfunc | whiptail --gauge "checking pre-reqs..." 8 48 0