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.
36 lines
810 B
36 lines
810 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() { |
|
default=${2:-"y"} |
|
default=$(echo "$default" | tr '[:upper:]' '[:lower:]') |
|
|
|
# Set prompt string |
|
case "$default" in |
|
y) prompt="Y/n" ;; |
|
n) prompt="y/N" ;; |
|
*) echo "Invalid default answer"; exit 1 ;; |
|
esac |
|
|
|
while true; do |
|
echo -n "$1 ($prompt): " |
|
stty raw -echo |
|
answer=$(head -c 1) || exit |
|
stty -echo |
|
printf "\n" |
|
case "$answer" in |
|
"" ) answer="$default"; break;; |
|
y|Y) answer="y"; break;; |
|
n|N) answer="n"; break;; |
|
esac |
|
done |
|
|
|
case "$answer" in |
|
y|Y) echo 1;; |
|
n|N) echo 0;; |
|
esac |
|
} |
|
|
|
ask_yn "$@" |