Thursday, December 16, 2010

How to show Yes/No prompt in Bash script

#!/bin/bash

read -p "Do you want to continue (Y/N)?"

[ "$(echo $REPLY | tr [:upper:] [:lower:])" == "y" ] || exit


or

#!/bin/bash

echo "Do you want to continue (yes/no)?"
read answer

if [ "$answer" == "yes" ]; then echo "YES"
else echo "NO"
fi


or

#!/bin/bash

while true; do
read -p "Do you want to continue (Y/N)?" answer
case $answer in
[Yy]* ) echo "YES"; break;;
[Nn]* ) echo "NO"; break;;
* ) echo "Please answer yes or no.";;
esac
done


or

#!/bin/bash

echo "What background color do you prefer?"

select answer in "White" "Blue" "Green"; do
case $answer in
White ) echo "WHITE"; break;;
Blue ) echo "BLUE"; break;;
Green ) echo "GREEN"; break;;
esac
done

No comments:

Post a Comment