Created
June 4, 2014 12:55
-
-
Save jorisroovers/0eab1eab6d75c78649d2 to your computer and use it in GitHub Desktop.
Generic bash snippets
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
# 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