The yesnoPrompt Shell Script, illustrated here:
- Uses the “read” statement with a “-p” option indicating this is a display prompt.
- Displays a message for valid (y/n) responses.
- An invalid response displays an error message and exits the script.
- Displays the value of the variable varYesNo
The Shell Script reads:
#! /bin/bash
read -p "Asking a yes or no question here? (y/n) " varYesNo
if [ "$varYesNo" == "y" ]
then
echo "The answer was yes"
echo "Here is where the \"yes\" actions to take are coded"
elif [ "$answer" == "n" ]
then
echo "The answer was no"
echo "Here is where the \"no\" actions to take are coded"
else
echo "Bad reply, try again."
exit
fi
echo "The reply was $varYesNo"