-
-
Save jmatthewturner/37549fdd0ca51b6fe62cedbd722b2ab6 to your computer and use it in GitHub Desktop.
This is the ask.sh BASH script that I downloaded a number of years ago. Apparently it's gone now, so here is my copy. I did not write this, and have made no modifications to it.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# This is a general-purpose function to ask Yes/No questions in Bash, either | |
# with or without a default answer. It keeps repeating the question until it | |
# gets a valid answer. | |
ask() { | |
# https://gist.github.com/davejamesmiller/1965569 | |
local prompt default reply | |
if [ "${2:-}" = "Y" ]; then | |
prompt="Y/n" | |
default=Y | |
elif [ "${2:-}" = "N" ]; then | |
prompt="y/N" | |
default=N | |
else | |
prompt="y/n" | |
default= | |
fi | |
while true; do | |
# Ask the question (not using "read -p" as it uses stderr not stdout) | |
echo -n -e "$1 [$prompt] " | |
# Read the answer (use /dev/tty in case stdin is redirected from somewhere else) | |
read reply </dev/tty | |
# Default? | |
if [ -z "$reply" ]; then | |
reply=$default | |
fi | |
# Check if the reply is valid | |
case "$reply" in | |
Y*|y*) return 0 ;; | |
N*|n*) return 1 ;; | |
esac | |
done | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment