2021-06-22 13:52:41 +02:00
|
|
|
function ask_user_if_empty {
|
2021-06-22 14:02:25 +02:00
|
|
|
# If variable with name $1 is empty, then ask for user input.
|
|
|
|
#
|
|
|
|
# Only one line of user input is allowed.
|
|
|
|
# The input must not be empty.
|
2021-05-01 12:25:25 +02:00
|
|
|
#
|
2021-05-02 14:28:28 +02:00
|
|
|
# arg $1: name of variable to store user input
|
|
|
|
# arg $2: text to display (e.g. "Enter hostname:")
|
2021-05-01 12:25:25 +02:00
|
|
|
if [ "$#" -ne 2 ]; then
|
2021-06-22 13:52:41 +02:00
|
|
|
echo 'ask_user_if_empty requires two args!';
|
2021-05-01 12:25:25 +02:00
|
|
|
return 1
|
|
|
|
fi
|
|
|
|
for i in "$@"; do
|
2021-06-22 13:52:41 +02:00
|
|
|
if [ -z "${i}" ]; then
|
|
|
|
echo 'ask_user_if_empty: all given args must not be empty';
|
2021-05-01 12:25:25 +02:00
|
|
|
return 1;
|
|
|
|
fi
|
|
|
|
done
|
|
|
|
|
2021-05-01 13:41:31 +02:00
|
|
|
local -n ptr=$1 || return $?
|
2021-06-22 13:52:41 +02:00
|
|
|
if [ -z "${ptr}" ]; then
|
2021-05-01 12:25:25 +02:00
|
|
|
# if ptr has no value yet, ask user for input!
|
2021-06-22 13:52:41 +02:00
|
|
|
echo "${2}"
|
2021-05-01 12:25:25 +02:00
|
|
|
read -r ptr || return $?
|
|
|
|
fi
|
|
|
|
|
|
|
|
# check string length to be greater than zero!
|
|
|
|
if [ "${#ptr}" -lt 1 ]; then
|
2021-06-22 13:52:41 +02:00
|
|
|
echo 'The input must not be empty!';
|
2021-05-01 12:25:25 +02:00
|
|
|
return 1;
|
|
|
|
fi
|
|
|
|
}
|
|
|
|
|
2021-06-22 14:02:25 +02:00
|
|
|
function single_choice_if_empty {
|
|
|
|
# If variable with name $1 is empty, then let user select one of the given options.
|
2021-05-01 12:25:25 +02:00
|
|
|
#
|
2021-05-02 14:28:28 +02:00
|
|
|
# arg $1: name of variable to store the selected option
|
|
|
|
# arg $2: text to display
|
|
|
|
# arg $3: name of variable with array of options to display (for each option there must be two entries in the array: Item and description)
|
2021-05-01 12:25:25 +02:00
|
|
|
if [ "$#" -ne 3 ]; then
|
2021-06-22 14:02:25 +02:00
|
|
|
echo 'single_choice_if_empty requires three args!';
|
2021-05-01 12:25:25 +02:00
|
|
|
return 1
|
|
|
|
fi
|
|
|
|
for i in "$@"; do
|
2021-06-22 13:52:41 +02:00
|
|
|
if [ -z "${i}" ]; then
|
2021-06-22 14:02:25 +02:00
|
|
|
echo 'single_choice_if_empty: all given args must not be empty';
|
2021-05-01 12:25:25 +02:00
|
|
|
return 1;
|
|
|
|
fi
|
|
|
|
done
|
|
|
|
|
|
|
|
|
2021-05-01 13:41:31 +02:00
|
|
|
local -n ptr=$1 || return $?
|
2021-06-22 13:52:41 +02:00
|
|
|
if [ -z "${ptr}" ]; then
|
2021-05-01 12:25:25 +02:00
|
|
|
# if ptr has no value yet, ask user for input!
|
|
|
|
|
2021-05-01 13:41:31 +02:00
|
|
|
local -n MENU_OPTIONS=$3 || return $?
|
2021-06-22 13:52:41 +02:00
|
|
|
ptr=$(dialog --stdout --menu "${2}" 0 0 0 "${MENU_OPTIONS[@]}") || {
|
|
|
|
echo 'Error during menu selection!'
|
2021-05-01 12:25:25 +02:00
|
|
|
exit 1
|
|
|
|
}
|
|
|
|
clear
|
|
|
|
fi
|
|
|
|
}
|
|
|
|
|
2021-06-22 14:02:25 +02:00
|
|
|
function multi_choice_if_empty {
|
|
|
|
# If variable with name $1 is empty, then let user select one or more of the given options.
|
2021-05-01 12:25:25 +02:00
|
|
|
#
|
2021-05-02 14:28:28 +02:00
|
|
|
# arg $1: name of variable to store array of selected options
|
|
|
|
# arg $2: text to display
|
|
|
|
# arg $3: name of variable with array of options to display (for each option there must be three entries in the array: Item, description, on/off)
|
2021-05-01 12:25:25 +02:00
|
|
|
if [ "$#" -ne 3 ]; then
|
2021-06-22 14:02:25 +02:00
|
|
|
echo 'multi_choice_if_empty requires three args!';
|
2021-05-01 12:25:25 +02:00
|
|
|
return 1
|
|
|
|
fi
|
|
|
|
for i in "$@"; do
|
2021-06-22 13:52:41 +02:00
|
|
|
if [ -z "${i}" ]; then
|
2021-06-22 14:02:25 +02:00
|
|
|
echo 'multi_choice_if_empty: all given args must not be empty';
|
2021-05-01 12:25:25 +02:00
|
|
|
return 1;
|
|
|
|
fi
|
|
|
|
done
|
|
|
|
|
|
|
|
|
2021-05-01 13:41:31 +02:00
|
|
|
local -n ptr=$1 || return $?
|
2021-06-22 13:52:41 +02:00
|
|
|
if [ -z "${ptr}" ]; then
|
2021-05-01 12:25:25 +02:00
|
|
|
# if ptr has no value yet, ask user for input!
|
|
|
|
|
2021-05-01 13:41:31 +02:00
|
|
|
local -n MENU_OPTIONS=$3 || return $?
|
2021-06-22 13:52:41 +02:00
|
|
|
TMP1=$(dialog --stdout --checklist "${2}" 0 0 0 "${MENU_OPTIONS[@]}") || {
|
|
|
|
echo 'Error during menu selection!'
|
2021-05-01 12:25:25 +02:00
|
|
|
exit 1
|
|
|
|
}
|
|
|
|
clear
|
|
|
|
|
|
|
|
# Result of dialog is 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' "$TMP1")
|
|
|
|
#
|
|
|
|
space_separated_to_array TMP1 "$1"
|
|
|
|
fi
|
|
|
|
}
|