I am looking to create a shell script which can take (read) 1 and 2 as inputs. I tried this below script but something looks to be missing. Here are my requirements :
- It should echo when no input is passed.
- It should accept values 1 or 2
- It should throw error if any other value is passed other that 1 or 2.
- when any one of above cases is not met it should go back to read and ask user for input again.
I might be wrong using while loop. But this is what I acheived so far.
#!bin/sh
while :
echo "enter value for a:";read -r a; do
if [ -z "${a}" ]; then
echo "That was empty, do it again!"
elif [ "${a}" -eq "1" ] || [ "${a}" -eq "2" ]; then
## echo "Checking now..."
echo "value passed is :$a"
echo "Condition success"
elif [ "${a}" -ge "3" ]
then
echo "Select either option :1 or :2 "
fi
break
done
Output
When I pass 1
. Test_input2.sh
enter value for a:
1
value passed is :1
Condition success
When I pass nothing
. Test_input2.sh
enter value for a:
That was empty, do it again!
I want the be able to prompt the user for input untill he gives me the expected input i.e. 1 or 2.How can I put this into loop ?