ao-mud is a spellbook of well-commented atomic bash scripts that each do one thing. we are building semantic building blocks for an autonomously-evolving digital spellcasting language.
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

37 lines
873 B

#!/bin/sh
# Define ask_yn, which asks a yes or no question and defaults to the specified choice.
# Outputs 1 for yes and 0 for no
ask_yn() {
2 years ago
default=${2:-"y"}
default=$(printf "%s" "$default" | tr '[:upper:]' '[:lower:]')
2 years ago
# Set prompt string
case "$default" in
y) prompt="Y/n" ;;
n) prompt="y/N" ;;
*) printf "Invalid default answer\n"; exit 1 ;;
2 years ago
esac
2 years ago
while true; do
trap 'stty sane; exit' INT
printf "%s (%s): " "$1" "$prompt"
stty raw -echo
answer=$(head -c 1) || exit
stty -echo
printf "\n"
2 years ago
case "$answer" in
"" ) answer="$default"; break;;
2 years ago
y|Y) answer="y"; break;;
n|N) answer="n"; break;;
esac
done
2 years ago
case "$answer" in
y|Y) printf "1";;
n|N) printf "0";;
2 years ago
esac
}
ask_yn "$@"