2021-07-28 09:32:08 -05:00
|
|
|
#!/bin/bash
|
|
|
|
|
|
|
|
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-11-15 14:33:21 -06:00
|
|
|
y | Y)
|
2021-07-31 18:45:47 -05:00
|
|
|
echo ""
|
|
|
|
return 0
|
|
|
|
;;
|
2021-11-15 14:33:21 -06:00
|
|
|
n | N)
|
2021-07-31 18:45:47 -05:00
|
|
|
echo ""
|
|
|
|
return 1
|
|
|
|
;;
|
2021-11-15 14:33:21 -06:00
|
|
|
*) echo -e "\nInput must be either y, Y, n, or N" ;;
|
2021-07-28 09:32:08 -05:00
|
|
|
esac
|
|
|
|
done
|
|
|
|
}
|