shell-base/modules/scripting/confirm.func

52 lines
1.3 KiB
Plaintext
Raw Normal View History

#!/bin/bash
# ======================================================================
#
# flags: -s, use strong confirmation, only yes or YES or Yes is accepted
#
# Parameters:
# $@ - The confirmation message
#
# Examples:
# > # Example 1
# > # The preferred way to use confirm
# > confirm Delete file1? && echo rm file1
# > or
# > [[ confirm Delete file1 ]]; then
# > rm file1
# > fi
# >
# > # Example 2
# > # Use the $? variable to examine confirm's return value
# > confirm Delete file2?
# > if [ $? -eq 0 ]
# > then
# > echo Another file deleted
# > fi
# >
# > # Example 3
# > # Tell bash to exit right away if any command returns a non-zero code
# > set -o errexit
# > confirm Do you want to run the rest of the script?
# > echo Here is the rest of the script
#
# ======================================================================
function confirm()
{
local res="y Y yes YES Yes Sure sure SURE OK ok Ok"
local strong="yes YES Yes"
[[ $1 = "-s" ]] && { res=$strong; strong=true shift; } || strong=""
echo -n "$@" "$([[ $strong ]] && echo \< $res \>) ? "
read -e answer
for response in $res
do
if [ "_$answer" == "_$response" ]
then
return 0
fi
done
# Any answer other than the list above is considered a "no" answer
return 1
}