Created
March 1, 2018 09:40
-
-
Save tomasaschan/9090ad5dc02669e190340850233a27ce to your computer and use it in GitHub Desktop.
Bash snippet for a confirmation prompt
This file contains hidden or 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
function confirm() { | |
confirmed= | |
read -p "$1 (y/n): " choice | |
while [[ -z $confirmed ]]; do | |
case "$choice" in | |
y|Y) | |
confirmed=true | |
;; | |
n|N) | |
confirmed=false | |
;; | |
*) | |
read -p "Invalid choice; answer y for yes or n for no (y/n): " choice | |
;; | |
esac | |
done | |
echo "$confirmed" | |
} |
This file contains hidden or 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
if [ $(confirm 'Are you sure?') = true ]; then | |
echo 'OK, I'll do it!' | |
else | |
echo 'OK, nevermind then.' | |
fi |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment