2022-07-16 17:18:03 +02:00
|
|
|
function ask_user_if_empty {
|
2023-03-21 14:11:10 +01:00
|
|
|
# If variable named $1 is empty, then ask for user input.
|
2022-07-16 17:18:03 +02:00
|
|
|
#
|
2023-03-21 14:11:10 +01:00
|
|
|
# One line of user input is read.
|
|
|
|
# The user input must not be empty.
|
2022-07-16 17:18:03 +02:00
|
|
|
#
|
2023-03-16 17:30:32 +01:00
|
|
|
# arg $1: Name of variable to store user input
|
|
|
|
# arg $2: Text to display (e.g. "Enter hostname:")
|
2023-03-21 14:11:10 +01:00
|
|
|
validate_args 2 "$@" || return $?
|
|
|
|
local -n user_input_ptr=$1
|
|
|
|
local display_text="${2}"
|
2022-07-16 17:18:03 +02:00
|
|
|
|
2023-03-21 14:11:10 +01:00
|
|
|
# If user_input_ptr is not empty, return
|
|
|
|
if [ -n "${user_input_ptr:-}" ]; then
|
|
|
|
return 0
|
2022-07-16 17:18:03 +02:00
|
|
|
fi
|
2023-03-21 14:11:10 +01:00
|
|
|
# Else, ask for user input!
|
|
|
|
|
|
|
|
printf '%s\n' "${display_text}"
|
|
|
|
read -r user_input_ptr || return $?
|
2022-07-16 17:18:03 +02:00
|
|
|
|
2023-03-21 14:11:10 +01:00
|
|
|
# User input must not be empty
|
|
|
|
if [ -z "${user_input_ptr}" ]; then
|
|
|
|
echo 'The input must not be empty!'
|
|
|
|
return 1
|
2022-07-16 17:18:03 +02:00
|
|
|
fi
|
|
|
|
}
|
|
|
|
|
|
|
|
function single_choice_if_empty {
|
2023-03-21 14:11:10 +01:00
|
|
|
# If variable named $1 is empty,
|
|
|
|
# then let user select one of the given options.
|
2022-07-16 17:18:03 +02:00
|
|
|
#
|
2023-03-21 14:11:10 +01:00
|
|
|
# arg $1: Name of variable to store the selected option-name
|
2023-03-16 17:30:32 +01:00
|
|
|
# arg $2: Text to display
|
2023-03-21 14:11:10 +01:00
|
|
|
# arg $3: Name of variable with array of options to display (for each option there must be two entries in the array: name and description)
|
|
|
|
validate_args 3 "$@" || return $?
|
|
|
|
local -n selected_option_ptr=$1
|
|
|
|
local display_text="${2}"
|
|
|
|
local -n menu_options_ptr=$3
|
2022-07-16 17:18:03 +02:00
|
|
|
|
2023-03-21 14:11:10 +01:00
|
|
|
# If selected_option_ptr is not empty, return.
|
|
|
|
if [ -n "${selected_option_ptr:-}" ]; then
|
|
|
|
return 0
|
2022-07-16 17:18:03 +02:00
|
|
|
fi
|
2023-03-21 14:11:10 +01:00
|
|
|
# Else, ask for user input!
|
|
|
|
|
|
|
|
selected_option_ptr=$(dialog --stdout --menu "${display_text}" 0 0 0 "${menu_options_ptr[@]}") || {
|
|
|
|
echo 'Error during menu selection!'
|
|
|
|
return 1
|
|
|
|
}
|
|
|
|
clear
|
2022-07-16 17:18:03 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
function multi_choice_if_empty {
|
2023-03-21 14:11:10 +01:00
|
|
|
# If variable named $1 is empty,
|
|
|
|
# then let user select one or more of the given options.
|
2022-07-16 17:18:03 +02:00
|
|
|
#
|
2023-03-21 14:11:10 +01:00
|
|
|
# arg $1: Name of variable to store the selected option-names as array
|
2023-03-16 17:30:32 +01:00
|
|
|
# arg $2: Text to display
|
2023-03-21 14:11:10 +01:00
|
|
|
# arg $3: Name of variable with array of options to display (for each option there must be three entries in the array: name, description, on/off)
|
|
|
|
validate_args 3 "$@" || return $?
|
|
|
|
local -n selected_option_ptr=$1
|
|
|
|
local display_text="${2}"
|
|
|
|
local -n menu_options_ptr=$3
|
2022-07-16 17:18:03 +02:00
|
|
|
|
2023-03-21 14:11:10 +01:00
|
|
|
# If selected_option_ptr is not empty, return.
|
|
|
|
if [ -n "${selected_option_ptr:-}" ]; then
|
|
|
|
return 0
|
|
|
|
fi
|
|
|
|
# Else, ask for user input!
|
2022-07-16 17:18:03 +02:00
|
|
|
|
2023-03-21 14:11:10 +01:00
|
|
|
selected_option_ptr=$(dialog --stdout --checklist "${display_text}" 0 0 0 "${menu_options_ptr[@]}") || {
|
|
|
|
echo 'Error during menu selection!'
|
|
|
|
return 1
|
|
|
|
}
|
|
|
|
clear
|
2022-07-16 17:18:03 +02:00
|
|
|
|
2023-03-21 14:11:10 +01:00
|
|
|
# Result of dialog is a space separated list.
|
|
|
|
# Store this as an array.
|
|
|
|
# Without newline at last array element: https://unix.stackexchange.com/a/519917/315162
|
|
|
|
# readarray -d " " -t ptr < <(printf '%s' "selected_option_ptr")
|
|
|
|
#
|
|
|
|
space_separated_to_array selected_option_ptr selected_option_ptr || return $?
|
2022-07-16 17:18:03 +02:00
|
|
|
}
|