Short bash function routine to prompt the user to enter Yes or No (which may be abbreviated to Y or N) in response to a prompt.
The function displays the prompt and will continue to prompt the user until they enter a valid response or press ctrl-C to abort the script. (The user can’t just press enter to continue, though it would be trivial to change the function to allow them to do so).
# syntax: confirm [<prompt>]
# Prompts the user to enter Yes or No and returns 0/1.
# This program is free software: you can redistribute it and/or modify it
# under the terms of the GNU General Public License as published by the Free
# Software Foundation, either version 3 of the License, or (at your option)
# any later version.
# This program is distributed in the hope that it will be useful, but
# WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
# or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
# for more details.
# You should have received a copy of the GNU General Public License along
# with this program. If not, see <http://www.gnu.org/licenses/>
# 04 Jul 17 0.1 - Initial version - MEJT
local _prompt _default _response
if [ "$1" ]; then _prompt="$1"; else _prompt="Are you sure"; fi
_prompt="$_prompt [y/n] ?"
# Loop forever until the user enters a valid response (Y/N or Yes/No).
while true; do
read -r -p "$_prompt " _response
case "$_response" in
[Yy][Ee][Ss]|[Yy]) # Yes or Y (case-insensitive).
[Nn][Oo]|[Nn]) # No or N.
*) # Anything else (including a blank) is invalid.
This entry was posted in Programming
and tagged bash
. Bookmark the permalink