Bash_Scripts/Functions/Confirmation.bash
2021-07-28 09:32:08 -05:00

33 lines
689 B
Bash
Executable File

#!/bin/bash
confirmation() {
# Receive confirmation from user as y, Y, n, or N
#
# 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) return 1 ;;
n | N) return 0 ;;
*) echo -e "\nInput must be either y, Y, n, or N" ;;
esac
done
}