Bash_Scripts/Templates/Functions/Confirmation.bash
2021-11-15 14:33:21 -06:00

40 lines
793 B
Bash
Executable File

#!/bin/bash
confirmation() {
# Receive confirmation from user as y, Y, n, or N
# returns 0 when answer is yes and 1 when answer is no
#
# Arguments:
# message <type: string> <position: 1> <required: true>
# - The confirmation prompt sent to the user, for example:
# Would you like to overwrite foobar.txt (y/N)?
#
# Usage:
# confirmation "Some prompt"
# - Sends "Some prompt" to the user and gets their input
#
# POSIX Compliant:
# Yes
#
local message
message="${1}"
local choice
while true; do
read -p "${message} " -n 1 -r choice
case "$choice" in
y | Y)
echo ""
return 0
;;
n | N)
echo ""
return 1
;;
*) echo -e "\nInput must be either y, Y, n, or N" ;;
esac
done
}