Bash_Scripts/Templates/Functions/Confirmation.bash

42 lines
801 B
Bash
Raw Normal View History

2021-07-28 09:32:08 -05:00
#!/bin/bash
2021-08-04 21:17:20 -05:00
2021-08-04 01:21:11 -05:00
2021-07-28 09:32:08 -05:00
confirmation() {
# Receive confirmation from user as y, Y, n, or N
2021-07-31 18:45:47 -05:00
# returns 0 when answer is yes and 1 when answer is no
2021-07-28 09:32:08 -05:00
#
# 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
2021-07-31 18:45:47 -05:00
read -p "${message} " -n 1 -r choice
2021-07-28 09:32:08 -05:00
case "$choice" in
2021-07-31 18:45:47 -05:00
y | Y)
echo ""
return 0
;;
n | N)
echo ""
return 1
;;
2021-07-28 09:32:08 -05:00
*) echo -e "\nInput must be either y, Y, n, or N" ;;
esac
done
}