#!/usr/bin/env bash # # Author: Georg Voell - georg.voell@standby.cloud # Version: @(#)confirm 3.3.1 16.03.2026 (c)2026 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, then returncode is 0. If no, returncode is 2 and quit returns 1. #@ 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. # # 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 # V 3.3.0 19.01.2026 Revised with support of Claude Code # V 3.3.1 16.03.2026 Optimized: replaced backticks with $(); [ ] with [[ ]]; # echo|grep with [[ == *[* ]]; replaced let with (( )); # replaced echo|sed array build with IFS read-array # # Find executable bash library and source it lib=$(command -v lib.bash 2>/dev/null) if [[ -n "$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 # Helper: parse an answer string like "y/[yes]" into (yesanswers, default) # Usage: parse_answers "$1" answers_var default_var function parse_answers() { local raw="${1}" local answers_var="${2}" local default_var="${3}" local cleaned raw=$(ToLower "$raw") if [[ "$raw" == *"["* ]]; then # Extract default from [...] local def="${raw#*[}" def="${def%%]*}" printf -v "$default_var" '%s' "$def" # Remove brackets cleaned="${raw//[/}" cleaned="${cleaned//]/}" else cleaned="$raw" fi printf -v "$answers_var" '%s' "$cleaned" } # 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 [[ -n "$1" ]]; then parse_answers "$1" yesanswers default shift else errstr="Please specify a string with possible answers (separated with '/') after parameter '$pname'." fi ;; -n | --no) shift if [[ -n "$1" ]]; then parse_answers "$1" noanswers default shift else errstr="Please specify a string with possible answers (separated with '/') after parameter '$pname'." fi ;; -a | --abort) shift if [[ -n "$1" ]]; then parse_answers "$1" abortanswers default 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 if [[ "$pname" == -* ]]; then errstr="Unknown option '$pname'." else if [[ -z "$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 [[ -z "$yesanswers" ]]; then yesanswers="y/yes" fi if [[ -z "$noanswers" ]]; then noanswers="n/no" fi # Build arrays of possible answers using IFS read (no echo|sed subshell) IFS='/' read -r -a yesarray <<< "$yesanswers" IFS='/' read -r -a noarray <<< "$noanswers" if [[ -n "$abortanswers" ]]; then IFS='/' read -r -a abortarray <<< "$abortanswers" fi # If default was not specified, set a default if [[ -z "$default" ]]; then if [[ -n "$abortanswers" ]]; then default="${abortarray[${#abortarray[@]}-1]}" else default="${noarray[${#noarray[@]}-1]}" fi fi # Check if we have at least a parameter with the question specified if [[ -z "$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 [[ -n "$tset" ]]; then $tset fi inp="" while [[ -z "$inp" ]]; do if [[ -n "$abortanswers" ]]; then printf "%s (%s %s %s) [%s]: " "$question" "$yesanswers" "$noanswers" "$abortanswers" "$default" else printf "%s (%s %s) [%s]: " "$question" "$yesanswers" "$noanswers" "$default" fi read -r inp inp=$(ToLower "$inp") if [[ -z "$inp" ]]; then inp="$default" fi for i in "${yesarray[@]}"; do if [[ "$inp" == "$i" ]]; then exit 0 fi done if [[ -n "$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