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.
31 lines
680 B
31 lines
680 B
#!/bin/bash |
|
|
|
question=$1 |
|
default_answer=$2 |
|
|
|
# Capitalize the default answer and construct the prompt |
|
if [[ $default_answer == "Y" ]]; then |
|
prompt="${question} (Y/n)? " |
|
else |
|
prompt="${question} (y/N)? " |
|
fi |
|
|
|
# Use stty to make read command work for single character input |
|
old_stty_cfg=$(stty -g) |
|
stty raw -echo |
|
printf "%s" "$prompt" |
|
answer=$( while ! head -c 1 | grep -i '[yn]'; do true; done ) |
|
stty $old_stty_cfg |
|
|
|
# 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 |
|
printf "%s\n" "$answer" |
|
if [[ $answer == "Y" || $answer == "y" ]]; then |
|
exit 0 |
|
else |
|
exit 1 |
|
fi
|
|
|