Created
June 25, 2021 18:09
-
-
Save tinytengu/9c2d9ce5d2cfa54529625cd3849c3db1 to your computer and use it in GitHub Desktop.
Bash Yes/Now prompt implementation with default answer
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
prompt_yn() { | |
def=$2 | |
# Normalize default answer and prompt | |
case $def in | |
[yY]) def="y"; read -r -p "${1} [Y/n] " input;; | |
[nN]) def="n"; read -r -p "${1} [y/N] " input;; | |
esac | |
# Echo 1 if yes else 0 | |
case $input in | |
"") | |
if [ $def = "y" ]; then | |
echo 1 | |
else | |
echo 0 | |
fi | |
;; | |
[yY][eE][sS]|[yY]) echo 1;; | |
[nN][oO]|[nN]|*) echo 0;; | |
esac | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment