Skip to content

Instantly share code, notes, and snippets.

@mangalbhaskar
Last active September 16, 2020 21:40
Show Gist options
  • Save mangalbhaskar/71084ad0bae7fccb9f66e5cd7817ee35 to your computer and use it in GitHub Desktop.
Save mangalbhaskar/71084ad0bae7fccb9f66e5cd7817ee35 to your computer and use it in GitHub Desktop.
bash yes or no loop for user prompt
#!/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
#!/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