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.

42 lines
937 B

1 year ago
#!/bin/sh
question=$1
1 year ago
default_answer=$(echo $2 | tr a-z A-Z)
# Capitalize the default answer and construct the prompt
1 year ago
if [ "$default_answer" = "Y" ]; then
prompt="${question} (Y/n)? "
else
prompt="${question} (y/N)? "
fi
# Save previous stty config
old_stty_cfg=$(stty -g)
1 year ago
# Handle Ctrl-C
trap "stty '$old_stty_cfg'; printf '\n'; exit 2" 2
1 year ago
# Use stty to make read command work for single character input
stty raw -echo
printf "%s" "$prompt" >&2
while IFS= read -r -n1 answer
do
if [ "$answer" = "" ] || echo "$answer" | grep -iq "^y\|^n"
then
break
fi
done
stty "$old_stty_cfg" >&2 # Restore original stty configuration
# If no answer is given, use the default answer
if [ -z "$answer" ]; then
answer=$default_answer
fi
# Print the answer and return appropriate exit code
1 year ago
printf "%s\n" "$(echo $answer | tr a-z A-Z)" >&2
1 year ago
if [ "$answer" = "Y" -o "$answer" = "y" ]; then
exit 0
else
exit 1
1 year ago
fi