#!/bin/bash # # Author: Georg Voell - georg.voell@standby.cloud # Version: @(#)confirm 3.2.1 10.10.2024 (c)2024 Standby.cloud # Licensed under the Universal Permissive License v 1.0 as shown at https://oss.oracle.com/licenses/upl/ # # This script can be used free of charge. Use it as is or customize as needed. It is not guaranteed to be # error free and you will not be reimbursed for any damage it may cause. # #@ Display first parameter (question) and wait for an answer (yes to confirm). #@ If answer is yes, returncode is 0 else returncode is > 0. #@ A default answer can be specified marked within "[]" when return is pressed. #@ #@Usage: confirm [options] question #@ Options: #@ -h, --help : Displays helptext. #@ -v, --version : Displays the version of the script. #@ -y, --yes : : Allowed "Yes" answers e.g. "y/[yes]" #@ -n, --no : : Allowed "No" answers e.g. "n/no" #@ -a, --abort : : Allowed "Abortion" answers e.g. "q/quit" #@ Question: #@ : Display and wait for confirmation. #@ #@Examples: #@ confirm "Do you want to continue?" #@ Result: "Do you want to continue? (y/yes n/no) [no]:" #@ confirm "Install the scripts?" -y "y/[yes]" #@ Result: "Install the scripts? (y/yes n/no) [yes]:" #@ confirm "Sind Sie sicher?" --yes "j/[ja]" --no "n/nein" #@ Result: "Sind Sie sicher? (j/ja n/nein) [ja]:" #@ confirm "¿Hablas español?" --yes "s/si" --no "n/[no]" #@ Result: "¿Hablas español? (s/si n/no) [no]:" #@ confirm "Do you want to continue?" --abort "q/quit" #@ Result: "Do you want to continue? (y/yes n/no q/quit) [quit]:" # # Exit codes: # 00: Confirmed / Ok # 01: Unknown or wrong parameter or abort. # 02: No confirmation. # 99: User interrupt. # # See also: # **install-scripts**(1) # # Code example: # # confirm "Do you want to continue?" # result=$? # if [ $result -eq 0 ]; then # # Answer was yes # echo "We want to continue" # else # # Answer was no # echo "We want to stop" # fi # # Update history: # # V 1.0.0 08.01.2017 New version # V 1.0.1 10.05.2017 Small changes # V 1.0.2 24.10.2017 String comparison in "sh/bash" is "=" not "==" # V 3.0.1 27.04.2020 Ask for more than just yes or no # V 3.0.2 24.05.2020 Rename script from 'askforinput' to 'confirm' # V 3.0.3 11.06.2020 Using library # V 3.1.0 05.06.2023 New copyright # V 3.2.0 12.08.2024 New minor version # V 3.2.1 10.10.2024 Small bug: tolower # # Find executable bash library and source it lib=`which lib.bash 2>/dev/null | sed 's|^no 'lib.bash' in .*||'` if [ "$lib" != "" ]; then source "$lib" else progdir=`dirname "$0"` if [ -r "${progdir}/lib.bash" ]; then source "${progdir}/lib.bash" else echo "Unexpected error: Unable to locate bash library 'lib.bash'." exit 1 fi fi # Preset yesanswers="" noanswers="" abortanswers="" default="" question="" # Check parameters: Loop until all parameters are used up while [ $# -gt 0 ]; do pname=${1} case "$pname" in -y | --yes) shift if [ "$1" != "" ]; then answers=`echo $1 | tolower` grepresult=`echo "$answers" | grep '\['` if [ "$grepresult" != "" ]; then default=`echo "$answers" | cut -d'[' -f2 | cut -d']' -f1` yesanswers=`echo "$answers" | tr -d '[' | tr -d ']'` else yesanswers="$answers" fi shift else errstr="Please specify a string with possible answers (separated with '/') after parameter '$pname'." fi ;; -n | --no) shift if [ "$1" != "" ]; then answers=`echo $1 | tolower` grepresult=`echo "$answers" | grep '\['` if [ "$grepresult" != "" ]; then default=`echo "$answers" | cut -d'[' -f2 | cut -d']' -f1` noanswers=`echo "$answers" | tr -d '[' | tr -d ']'` else noanswers="$answers" fi shift else errstr="Please specify a string with possible answers (separated with '/') after parameter '$pname'." fi ;; -a | --abort) shift if [ "$1" != "" ]; then answers=`echo $1 | tolower` grepresult=`echo "$answers" | grep '\['` if [ "$grepresult" != "" ]; then default=`echo "$answers" | cut -d'[' -f2 | cut -d']' -f1` abortanswers=`echo "$answers" | tr -d '[' | tr -d ']'` else abortanswers="$answers" fi shift else errstr="Please specify a string with possible answers (separated with '/') after parameter '$pname'." fi ;; -v | --version) shift showversion=true ;; -h | --help) shift showhelp=true ;; *) shift paramck=`echo "$pname" | grep '^-'` # Types don't begin with '-' if [ "$paramck" != "" ]; then errstr="Unknown option '$pname'." else if [ "$question" = "" ]; then question="$pname" else errstr="Question was already given: '$question'. Unknown additional parameter: '$pname'." fi fi esac done # Display help or error message DisplayHelp # Set defaults if not specified if [ "$yesanswers" = "" ]; then yesanswers="y/yes" fi if [ "$noanswers" = "" ]; then noanswers="n/no" fi # Build an array of possible answers yesarray=(`echo $yesanswers | sed 's|/| |g'`) noarray=(`echo $noanswers | sed 's|/| |g'`) if [ "$abortanswers" != "" ]; then abortarray=(`echo $abortanswers | sed 's|/| |g'`) fi # If default was not specified, set a default if [ "$default" = "" ]; then if [ "$abortanswers" != "" ]; then len="${#abortarray[@]}" let len-- default="${abortarray[$len]}" else len="${#noarray[@]}" let len-- default="${noarray[$len]}" fi fi # Check if we have at least a parameter with the question specified if [ "$question" = "" ]; then exitcode=1 errormsg $exitcode "($progstr) Please ask a question: e.g.: '$progstr \"Are you doing ok?\"'." exit $exitcode else tset=`filecheck -x tset` if [ "$tset" != "" ]; then $tset fi inp="" while [ "$inp" = "" ]; do if [ "$abortanswers" != "" ]; then printf "${question} (${yesanswers} ${noanswers} ${abortanswers}) [${default}]: " else printf "${question} (${yesanswers} ${noanswers}) [${default}]: " fi read inp inp=`echo $inp | tolower` if [ "$inp" = "" ]; then inp="$default" fi for i in "${yesarray[@]}"; do if [ "$inp" = "$i" ]; then exit 0 fi done if [ "$abortanswers" != "" ]; then for i in "${abortarray[@]}"; do if [ "$inp" = "$i" ]; then exit 1 fi done fi for i in "${noarray[@]}"; do if [ "$inp" = "$i" ]; then exit 2 fi done inp="" done fi exit $exitcode