Skip to content

Instantly share code, notes, and snippets.

@smhmic
Last active August 29, 2015 13:57
Show Gist options
  • Save smhmic/9840501 to your computer and use it in GitHub Desktop.
Save smhmic/9840501 to your computer and use it in GitHub Desktop.
bash shell yes/no confirmation prompt
function confirm() {
[ 1 != "`is_interactive`" ] && \
return 1
local prompt="$1"
local choices="y/n"
[ $# = 0 ] && \
prompt="Do you want to proceed?"
[[ -n "$prompt" && ! -n "$choices" ] && \
prompt="$prompt ($choices) "
[ -z "$choices" ] && \
choices="yes or nn"
local input
while true; do
read -p "$prompt" input
case "$input" in
y|Y|[yY][eE][sS] )
return
break
;;
n|N|[nN][oO] )
echo "Cancelled"
return 1
break
;;
* )
echo "Invalid response. Enter $choices"
;;
esac
done
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment