Last active
September 16, 2020 21:40
-
-
Save mangalbhaskar/71084ad0bae7fccb9f66e5cd7817ee35 to your computer and use it in GitHub Desktop.
bash yes or no loop for user prompt
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
#!/bin/bash | |
## Copyright (c) 2020 mangalbhaskar. All Rights Reserved. | |
##__author__ = 'mangalbhaskar' | |
###---------------------------------------------------------- | |
function test-1-case-1-yes_or_no_loop() { | |
local LSCRIPTS=$( cd "$( dirname "${BASH_SOURCE[0]}")" && pwd ) | |
source ${LSCRIPTS}/yes_or_no_loop.sh | |
yes_or_no_loop "Are you infected" && echo "Yes" || echo "No" | |
yes_or_no_loop "Are you sure" && echo "Yes" || echo "No" | |
yes_or_no_loop "Think once again, are you sure" && echo "Yes" || echo "No" | |
} | |
test-1-case-1-yes_or_no_loop |
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
#!/bin/bash | |
## Copyright (c) 2020 mangalbhaskar. All Rights Reserved. | |
##__author__ = 'mangalbhaskar' | |
###---------------------------------------------------------- | |
function yes_or_no_loop { | |
local msg | |
local response | |
[[ $# -eq 0 ]] && msg="Are you sure" || msg="$*" | |
msg=$(echo -e "\e[1;36m${msg}? \e[1;37m[y/n]\e[1;33m>\e[0m ") | |
while true; do | |
read -p "${msg}" response | |
case ${response} in | |
[Yy]*) return 0 ;; | |
[Nn]*) return -1 ;; | |
# *) _echo "Invalid input" ;; | |
esac | |
done | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment