In my program I need to validate user input getting by "read" in shell script. It check user input has spaces and if validation fails we should give user a another chance to input again.
stringValidator() {
if [[ ${1} =~ " " ]]; then
echo Should not contain spaces
echo Enter Again!
read input
echo $2=$input
return $2
else
return $1
fi
}
echo "Enter below details"
echo -e "client-id :"
read clientId
stringValidator "$clientId" ${!clientId@}
Here I tried to do was send the variable name also into stringValidator() and reassign value to that name (here it is clientId).
But the problem is I can't return that variable from stringValidator(). I use many read inputs though I showed only one here. Therefore I need a separate function for validation.
If you have better way to do this please mention.