Skip to content

Instantly share code, notes, and snippets.

@macmladen
Created April 3, 2015 08:26
Show Gist options
  • Save macmladen/e63cf318adac50690540 to your computer and use it in GitHub Desktop.
Save macmladen/e63cf318adac50690540 to your computer and use it in GitHub Desktop.
Some useful bash functions to be included (sourced) in your bash scripts
# promptyn "{message}" {default_answer}
# $1 "{message}" like "Do you wish to procede? [Yn]"
# $2 {default_answer} you can pass default value, just notify that in message above
# @return
# 0 for any of y, Y, yes, YAYA, yabbadabbadoo
# 1 for any of n, N, Nein, nikako, nonononoooo
promptyn() {
while true; do
read -p "$1 " yn
CHOICE=${yn:0:1}
case ${CHOICE:-$2} in
[Yy]* ) return 0;;
[Nn]* ) return 1;;
* ) echo "Please answer yes or no.";;
esac
done
}
# CheckTask
# $1 Message
# $2 Variable name
CheckTask() {
if [ "$BTN_AUTO" == "1"]; then
eval ${2}=1
return
fi
while true; do
read -p "$1 " yn
CHOICE=${yn:0:1}
case ${CHOICE:-${!2}} in
[Yy1]* ) eval ${2}=1 ; return ;;
[Nn0]* ) eval ${2}=0 ; return ;;
* ) echo "Please answer [ 1 y yes ] or [ 0 n no ] or [ENTER] to keep unchanged.";;
esac
done
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment