function get_text_input { # If variable with name $1 is zero, then ask user for input. # Only one line user input is allowed. # User input must not be empty. # # arg $1: name of variable to store user input # arg $2: text to display (e.g. "Enter hostname:") if [ "$#" -ne 2 ]; then echo "get_text_input requires two args!"; return 1 fi for i in "$@"; do if [ -z "$i" ]; then echo "get_text_input: all given args must not be empty"; return 1; fi done local -n ptr=$1 || return $? if [ -z "$ptr" ]; then # if ptr has no value yet, ask user for input! echo "$2" read -r ptr || return $? fi # check string length to be greater than zero! if [ "${#ptr}" -lt 1 ]; then echo "get_text_input must not be empty!"; return 1; fi } function get_single_choice { # If variable with name $1 is zero, then ask user to select one option. # # 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) if [ "$#" -ne 3 ]; then echo "get_single_choice requires three args!"; return 1 fi for i in "$@"; do if [ -z "$i" ]; then echo "get_single_choice: all given args must not be empty"; return 1; fi done local -n ptr=$1 || return $? if [ -z "$ptr" ]; then # if ptr has no value yet, ask user for input! local -n MENU_OPTIONS=$3 || return $? ptr=$(dialog --stdout --menu "$2" 0 0 0 "${MENU_OPTIONS[@]}") || { echo "Error during menu selection!" exit 1 } clear fi } function get_multi_choice { # If variable with name $1 is zero, then ask user to select one ore more options. # # 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) if [ "$#" -ne 3 ]; then echo "get_multi_choice requires three args!"; return 1 fi for i in "$@"; do if [ -z "$i" ]; then echo "get_multi_choice: all given args must not be empty"; return 1; fi done local -n ptr=$1 || return $? if [ -z "$ptr" ]; then # if ptr has no value yet, ask user for input! local -n MENU_OPTIONS=$3 || return $? TMP1=$(dialog --stdout --checklist "$2" 0 0 0 "${MENU_OPTIONS[@]}") || { echo "Error during menu selection!" 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 }