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.

44 lines
1.2 KiB

#!/bin/sh
2 years ago
# 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=$(echo "$default" | tr '[:upper:]' '[:lower:]')
if [ "$default" = "y" ]; then
prompt="${1} (Y/n): "
default_output=1
else
2 years ago
prompt="${1} (y/N): "
default_output=0
fi
2 years ago
while true; do
2 years ago
# Show prompt and get user input
printf "%s" "$prompt"
2 years ago
if [ "$(uname)" = "Darwin" ]; then
# On macOS, use "read -n 1"
read -n 1 -r REPLY
else
# On Linux and others, use "read -n 1 -s"
read -r -n 1 -s REPLY
fi
2 years ago
# If user hits Ctrl+C or Enter, break and return default output
if [ -z "$REPLY" ] || [ "$REPLY" = "$(printf '\003')" ]; then
REPLY=$default
printf "\n"
break
fi
case "$REPLY" in
y|Y ) printf "\n"; return 1;;
n|N ) printf "\n"; return 0;;
* ) printf "\nInvalid input. Please type 'y' or 'n'.";;
esac
done
2 years ago
return $default_output
}
ask_yn "$@"