Script example of asking the user for input.
Ask for a string value:
echo "Make a choice"
read -p "Whats your name?" -a NAME
Read a string value, wait for 10 seconds.
echo "Is your name $NAME?"
read -t 10 -p "Choice 1 YES, 2 NO:" -a YESNO
check if the YESNO value is set.
if [[ -z $YESNO ]]; then
echo "YESNO not set exit"
exit 0
fi
Check the of YESNO
if [ $YESNO -eq 1 ]; then
echo "Yes: $YESNO"
elif [ $YESNO -eq 2 ] ; then
echo "No: $YESNO"
else
echo "Unkown choice: $YESNO"
fi
Whole script:
#!/bin/bash
#
#
echo "Make a choice"
read -p "Whats your name?" -a NAME
echo "Is your name $NAME?"
read -t 10 -p "Choice 1 YES, 2 NO:" -a YESNO
if [[ -z $YESNO ]]; then
echo "YESNO not set exit"
exit 0
fi
if [ $YESNO -eq 1 ]; then
echo "Yes: $YESNO"
elif [ $YESNO -eq 2 ] ; then
echo "No: $YESNO"
else
echo "Unkown choice: $YESNO"
fi
Geen opmerkingen:
Een reactie posten