I have a function that enables me to check for unset variables:
ValidateArgs () {
for Var in "$@" ; do
echo "Validating argument $Var with value \"${!Var}\"."
if [ -z "${!Var}" ] ; then
echo "Argument \"$Var\" is required. Please define $Var."
read -rp "Enter $Var: " Var
echo -e "\n"
fi
done
}
I'd like to enhance this function so it is able to set a value for the arguments passed after reading it with read -rp.
I tried several combinations, is it possible to do this and if so what would be the ebst way?
The function is called like this:
ValidateArgs Action HostName
if Action and HostName are unset after going through the ValidateArgs function a value is asked and should be set. I would prefer to use the function in the main script.
-voperator and${var:?msg}parameter expansions.