Bash_Scripts/Templates/Functions/Confirmation.bash

40 lines
793 B
Bash
Raw Permalink Normal View History

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