Created
January 21, 2014 15:14
-
-
Save sstelfox/8541908 to your computer and use it in GitHub Desktop.
A quick bash common function that can be used to ask multiple choice questions and ensure an answer.
This file contains hidden or 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
| #!/bin/bash | |
| function ask_multi_choice() { | |
| local QUESTION=$1 | |
| while [ -z "$CHOICE" ]; do | |
| echo | |
| for S in ${!POSSIBLE_ANSWERS[*]}; do | |
| echo " ${S}) ${POSSIBLE_ANSWERS[$S]}" | |
| done | |
| echo | |
| read -r -p "${QUESTION}: " | |
| if [ -n "$REPLY" ]; then | |
| CHOICE="${POSSIBLE_ANSWERS[$REPLY]}" | |
| fi | |
| done | |
| export ANSWER="${CHOICE}" | |
| } | |
| declare -A POSSIBLE_ANSWERS | |
| POSSIBLE_ANSWERS["1"]="eth0" | |
| POSSIBLE_ANSWERS["2"]="at0" | |
| POSSIBLE_ANSWERS["3"]="wlan0" | |
| ask_multi_choice "Pick an interface" | |
| echo $ANSWER |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment