A yes/no dialog box

From Linux Shell Scripting Tutorial - A Beginner's handbook
Jump to: navigation, search
← dialog customization with configuration fileHomeAn input dialog box →
  • You can display a yes/no dialog box using the following syntax:
dialog --common-options --yesno text height width
  • This dialog box is useful for asking questions that require the user to answer either yes or no.
  • The dialog box has a Yes button and a No button, in which the user can switch between by pressing the TAB key.
  • On exit, no text is written to dialog’s output. In addition to the "Yes" and "No" exit codes and ESC exit status may be returned as follows:
    • 0 - Yes chosen.
    • 1 - No chosen.
    • 255 - Escape key was pressed i.e. box closed.

Example

  • Create a script called dynbox.sh:
#!/bin/bash
# dynbox.sh - Yes/No box demo
dialog --title "Delete file" \
--backtitle "Linux Shell Script Tutorial Example" \
--yesno "Are you sure you want to permanently delete \"/tmp/foo.txt\"?" 7 60
 
# Get exit status
# 0 means user hit [yes] button.
# 1 means user hit [no] button.
# 255 means user hit [Esc] key.
response=$?
case $response in
   0) echo "File deleted.";;
   1) echo "File not deleted.";;
   255) echo "[ESC] key pressed.";;
esac

Save and close the file. Run it as follows:

chmod +x dynbox.sh
./dynbox.sh


Asking questions that require the user to answer either yes or no
← dialog customization with configuration fileHomeAn input dialog box →