user3744885 user3744885 - 5 months ago 12
Bash Question

How to use bash dialog --yesno correctly

I want a simple Yes/No dialog and do make an action dependent on the users choice. My try so far:

operation=$(dialog --stdout --title "What to do?" \
--backtitle "Backup-Verwaltung" \
--yesno "Yes: Delete, No: Restore" 7 60)

if [[ $operation == true ]]; then
dialog --title "Information" --msgbox "TRUE" 6 44
else
dialog --title "Information" --msgbox "FALSE" 6 44
fi

Answer

The --yesno flag returns with the exit status (0 = Yes, 1 = No). Simply check it:

if dialog --stdout --title "What to do?" \
          --backtitle "Backup-Verwaltung" \
          --yesno "Yes: Delete, No:  Restore" 7 60; then
    dialog --title "Information" --msgbox "TRUE" 6 44
else
    dialog --title "Information" --msgbox "FALSE" 6 44
fi

Alternative the exit status is stored in $?, which can be saved for later:

dialog --stdout --title "What to do?" \
  --backtitle "Backup-Verwaltung" \
  --yesno "Yes: Delete, No:  Restore" 7 60
dialog_status=$?

# Do something

if [ "$dialo_status" -eq 0 ]; then
  # The previous dialog was answered Yes
else
  # The previous dialog was answered No or interrupted with <C-c>
fi 
Comments