Skip to content

Instantly share code, notes, and snippets.

@sstelfox
Created January 21, 2014 15:14
Show Gist options
  • Save sstelfox/8541908 to your computer and use it in GitHub Desktop.
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.
#!/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