1

I have script which takes many input from user and if user mistakenly press enter, then user should be asked to enter non-empty string again. There is way to implement it by using while loop like this

while [[ $input == '' ]]
do
    read -p "Enter string: " input
done

I don't want to use these four line everywhere. Is there a way to create a function of above code and pass variable name?

function input(){
    while [[ $1 == '' ]]
    do
        read -p "Enter string: " i
        set -- $i "${@:2}"
    done
}
input $cluster
echo $cluster

I tried this way but it doesn't work.

John Kugelman
  • 349,597
  • 67
  • 533
  • 578
prashant
  • 2,808
  • 5
  • 26
  • 41

0 Answers0