2021-04-29 20:25:30 +02:00
|
|
|
#!/bin/bash
|
|
|
|
# source: https://stackoverflow.com/a/52678279/6334421
|
|
|
|
|
|
|
|
# A variable can be assigned the nameref attribute using the -n option
|
|
|
|
# to the declare or local builtin commands (see Bash Builtins) to create
|
|
|
|
# a nameref, or a reference to another variable. This allows variables to
|
|
|
|
# be manipulated indirectly.
|
|
|
|
|
|
|
|
function text_input {
|
|
|
|
# If variable of name $1 is zero, then ask user for input.
|
|
|
|
# Only one line user input is allowed.
|
|
|
|
# User input must not be empty.
|
|
|
|
#
|
|
|
|
# $1: variable name to store input to
|
|
|
|
# $2: text to display (e.g. "Enter hostname:")
|
|
|
|
if [ -z "$1" ] || [ -z "$2" ]; then
|
|
|
|
echo "text_input requires two args!";
|
|
|
|
return 1
|
|
|
|
fi
|
|
|
|
|
2021-05-01 13:41:31 +02:00
|
|
|
local -n ptr=$1 || return $?
|
2021-04-30 21:42:12 +02:00
|
|
|
if [ -z "$ptr" ]; then
|
2021-04-29 20:25:30 +02:00
|
|
|
echo "$2"
|
2021-04-30 21:42:12 +02:00
|
|
|
read -r ptr || return $?
|
2021-04-29 20:25:30 +02:00
|
|
|
fi
|
|
|
|
|
|
|
|
# check string length to be greater than zero!
|
2021-04-30 21:42:12 +02:00
|
|
|
if [ "${#ptr}" -lt 1 ]; then
|
2021-04-29 20:25:30 +02:00
|
|
|
echo "text_input must not be empty!";
|
|
|
|
return 1;
|
|
|
|
fi
|
|
|
|
}
|
|
|
|
|
|
|
|
text_input foo "Enter something funny:"
|
|
|
|
echo "Input: $foo"
|
|
|
|
|
|
|
|
bar=""
|
|
|
|
text_input bar "Enter something even funnier:"
|
|
|
|
echo "Input: $bar"
|