Fix prompt_yn silently discarding its default on empty input
Confirmed live: pressing Enter on any "(y/n): y"-style prompt set the
variable to an empty string instead of the stated default, since prompt_yn
had no ${response:-$default} fallback (prompt_text already had one). Every
downstream [[ "$VAR" =~ ^[Yy]$ ]] check treated "just press Enter" as no.
Also shows the default value in the prompt text itself for both helpers,
since neither displayed it before.
This commit is contained in:
+12
-5
@@ -413,21 +413,28 @@ validate_password() {
|
|||||||
|
|
||||||
# Prompt yes/no, honoring unattended. prompt_yn "Question?" "default" VARNAME
|
# Prompt yes/no, honoring unattended. prompt_yn "Question?" "default" VARNAME
|
||||||
prompt_yn() {
|
prompt_yn() {
|
||||||
local question="$1" default="$2" varname="$3" response
|
local question="$1" default="$2" varname="$3" response hint=""
|
||||||
if [ "$UNATTENDED" = true ]; then
|
if [ "$UNATTENDED" = true ]; then
|
||||||
eval "$varname='$default'"; echo "$question [auto: $default]"; return
|
eval "$varname='$default'"; echo "$question [auto: $default]"; return
|
||||||
fi
|
fi
|
||||||
read -p "$question " response
|
# Confirmed live: this used to have no fallback to $default at all here —
|
||||||
eval "$varname='$response'"
|
# pressing Enter on a stated "(y/n): y" default silently set the variable
|
||||||
|
# to an EMPTY string, not "y", so every downstream `[[ "$VAR" =~ ^[Yy]$ ]]`
|
||||||
|
# check treated "just press Enter to accept the default" as a no. Every
|
||||||
|
# prompt_yn call in every service was affected.
|
||||||
|
[ -n "$default" ] && hint=" [$default]"
|
||||||
|
read -p "${question}${hint} " response
|
||||||
|
eval "$varname='${response:-$default}'"
|
||||||
}
|
}
|
||||||
|
|
||||||
# Prompt text, honoring unattended. prompt_text "Question?" "default" VARNAME
|
# Prompt text, honoring unattended. prompt_text "Question?" "default" VARNAME
|
||||||
prompt_text() {
|
prompt_text() {
|
||||||
local question="$1" default="$2" varname="$3" response
|
local question="$1" default="$2" varname="$3" response hint=""
|
||||||
if [ "$UNATTENDED" = true ]; then
|
if [ "$UNATTENDED" = true ]; then
|
||||||
eval "$varname='$default'"; echo "$question [auto: $default]"; return
|
eval "$varname='$default'"; echo "$question [auto: $default]"; return
|
||||||
fi
|
fi
|
||||||
read -p "$question " response
|
[ -n "$default" ] && hint=" [$default]"
|
||||||
|
read -p "${question}${hint} " response
|
||||||
eval "$varname='${response:-$default}'"
|
eval "$varname='${response:-$default}'"
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user