#!/bin/bash
fun_get_user_variables_no_default() { local VARIABLES=("${!1}") local TMP_DEFAULT local TMP_USER local TMP_READ
echo "Automatic timeout is 120 sec"
for i in "${VARIABLES[@]}"; do
TMP_DEFAULT=DEFAULT_$i
echo -n "GIMMME the $i (username, eg: ${!TMP_DEFAULT}), no default value:"
read -t 120 TMP_READ
echo ""
declare -g USER_$i=$TMP_READ
TMP_USER=USER_$i
if [ "${!TMP_USER}" == "" ]; then
echo -n "${TMP_USER} can not be empty. Please give it again:"
read -t 130 TMP_READ
declare -g USER_$i=$TMP_READ
TMP_USER=USER_$i
if [ "${!TMP_USER}" == "" ]; then
echo "Second try failed. Quitting..."
exit 1
fi
fi
done
}
Call the function with test variables
test_variables=("VAR1" "VAR2") fun_get_user_variables_no_default test_variables[@]
Display the results
echo "USER_VAR1: ${USER_VAR1}" echo "USER_VAR2: ${USER_VAR2}"