Bash_Scripts/Templates/Functions/Confirmation.bash

47 lines
1.1 KiB
Bash
Raw Normal View History

2021-07-28 09:32:08 -05:00
#!/bin/bash
2021-08-04 01:21:11 -05:00
# Copyright 2021, Price Hiller - All Rights Reserved
#
# Copying, distribution, usage, or modification of this file or the following source code, via any
# method is strictly prohibited without the explicit written consent of Price Hiller (philler3138@gmail.com)
# The following file contains proprietary and confidential content
#
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
}