Skip to content

Instantly share code, notes, and snippets.

@jorisroovers
Created June 4, 2014 12:55
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save jorisroovers/0eab1eab6d75c78649d2 to your computer and use it in GitHub Desktop.
Save jorisroovers/0eab1eab6d75c78649d2 to your computer and use it in GitHub Desktop.
Generic bash snippets
# Asserts that the script was invoked with valid arguments
assert_usage(){
if [ $# -ne 1 ]; then
echoerr "Usage: $0 (start | start | develop)"
exit 1
fi
}
# Begin actual script:
assert_usage $@
# do something based on first param
case "$1" in
install) install;;
clean) clean;;
*) install;;
esac
# Prompts a given yes/no question.
# Returns 0 if user answers yes, 1 if no
# Reprompts if different answer
ask_yes_no(){
question=$1
while true; do
read -e -p "$question" -i "Y" yn
case $yn in
[Yy]* ) return 0;;
[Nn]* ) return 1;;
esac
done
}
if ask_yes_no "Backup directory '$dest' already exists. Overwrite? [Y/n]";
then
# do stuff
fi
# TODO(jroovers): modify this to pass array along (now it comes from the global scope)
ask_question_options(){
question=$1
PS3=$1 # PS3 is used by the select command to show question below the options
select opt in "${options[@]}"
do
if array_contains "$opt" "${options[@]}"; then
echo $opt
return;
fi
done
}
options=("stable/havana" "stable/icehouse" "master")
branch=$(ask_question_options "Select a devstack branch: ")
echo "Chosen branch: $branch"
exit 1
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment