14

Sometimes I need to ask the user for yes/no to confirm something.

Usually I use something like this:

# Yes/no dialog. The first argument is the message that the user will see.
# If the user enters n/N, send exit 1.
check_yes_no(){
    while true; do
        read -p "$1" yn
        if [ "$yn" = "" ]; then
            yn='Y'
        fi
        case "$yn" in
            [Yy] )
                break;;
            [Nn] )
                echo "Aborting..."
                exit 1;;
            * )
                echo "Please answer y or n for yes or no.";;
        esac
    done;
}

Is there a better way to do it? Is this utility maybe already in my /bin folder?

3
  • 2
    You can try using a select, but otherwise I don't see a simpler way.
    – muru
    Nov 19, 2014 at 12:28
  • 2
    @muru, I'm totally stealing your ideas. I wish I could hand my rep to you. Nov 19, 2014 at 15:22
  • @glennjackman I'd call it collaboration. ;)
    – muru
    Nov 19, 2014 at 15:26

5 Answers 5

13

Ah, there is something built-in: zenity is a graphical dialog program:

if zenity --question --text="Is this OK?" --ok-label=Yes --cancel-label=No
then
    # user clicked "Yes"
else
    # user clicked "No"
fi

In addition to zenity, you can use one of:

if dialog --yesno "Is this OK?" 0 0; then ...
if whiptail --yesno "Is this OK?" 0 0; then ...
3
  • 3
    If dialog programs are acceptable, wouldn't dialog or whiptail be more suited to CLI?
    – muru
    Nov 19, 2014 at 15:04
  • 2
    Indeed. Added to the answer. Nov 19, 2014 at 15:16
  • 1
    Personally, I prefer the fork, yad, which has more improvements and less bugs IMO.
    – Sparhawk
    Nov 26, 2014 at 12:07
11

That looks fine to me. I would just make it a bit less "do or die":

  • if "Y" then return 0
  • if "N" then return 1

That way you can do something like:

if check_yes_no "Do important stuff? [Y/n] "; then
    # do the important stuff
else
    # do something else
fi
# continue with the rest of your script

With @muru's select suggestion, the function can be very terse:

check_yes_no () { 
    echo "$1"
    local ans PS3="> "
    select ans in Yes No; do 
        [[ $ans == Yes ]] && return 0
        [[ $ans == No ]] && return 1
    done
}
1

As a conclusion I wrote this script:

#!/bin/bash

usage() { 
    echo "Show yes/no dialog, returns 0 or 1 depending on user answer"
    echo "Usage: $0 [OPTIONS]
    -x      force to use GUI dialog
    -m <string> message that user will see" 1>&2
    exit 1;
}

while getopts m:xh opts; do
    case ${opts} in
        x) FORCE_GUI=true;
            ;;
        m) MSG=${OPTARG}
            ;;
        h) usage
            ;;
    esac
done

if [ -z "$MSG" ];then
    usage
fi

# Yes/no dialog.
# If the user enters n/N, return 1.
while true; do
    if [ -z $FORCE_GUI ]; then
        read -p "$MSG" yn
        case "$yn" in
            [Yy] )
                exit 0;;
            [Nn] )
                echo "Aborting..." >&1
                exit 1;;
            * )
                echo "Please answer y or n for yes or no.";;
        esac
    else
        if [ -z $DISPLAY ]; then echo "DISPLAY variable is not set" >&1 ; exit 1; fi
        if zenity --question --text="$MSG" --ok-label=Yes --cancel-label=No; then
            exit 0
        else
            echo "Aborting..." >&1
            exit 1
        fi
    fi
done;

Latest version of script can be found here. Fill free to change/edit

0

I'm using the following:

  • default to no:
    read -p "??? Are You sure [y/N]? " -n 1
    if [[ ! $REPLY =~ ^[Yy]$ ]]; then
        echo "!!! Canceled by user."
        exit 1
    fi
  • default to yes:
    read -p "??? Are You sure [Y/n]" -n 1
    if [[ $REPLY =~ ^[Nn]$ ]]; then
        echo "!!! Canceled by user."
        exit 1
    fi
0
 read -p 'Are you sure you want to continue? (y/n) ' -n 1 confirmation
 echo ''                                                                                                   
 if [[ $confirmation != 'y' && $confirmation != 'Y' ]]; then                                               
   exit 3                                                                                                
 fi
 # Code to execute if user wants to continue here.

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .