Skip to content

Instantly share code, notes, and snippets.

@tkuchiki
Created June 5, 2014 09:15
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 tkuchiki/60683515731f5a55d4ba to your computer and use it in GitHub Desktop.
Save tkuchiki/60683515731f5a55d4ba to your computer and use it in GitHub Desktop.
Term::UI for bash (未完成)
#!/bin/bash
CHOICES=$(echo "hoge foobar piyo")
CHOICES="${CHOICES} nothing"
NUM=$(echo $CHOICES | wc -w)
echo "question?"
for i in $(seq 1 $NUM); do
echo "${i}) $(echo $CHOICES | cut -d ' ' -f $i)"
done
echo
read -p "Please enter numbers?[$NUM] : " ANSWERS
ANSWERS=${ANSWERS:-$NUM}
_FS="[, ]"
echo "${ANSWERS}" | awk -F "${_FS}" 'BEGIN { OFS=" " } $1 = $1 { print $0 }'
for A in $(echo $ANSWERS | awk -F "${_FS}" 'BEGIN { OFS=" " } $1 = $1 { print $0 }'); do
declare -i __ANSWER=$A
if [ $NUM = $__ANSWER ]; then
echo "Canceled"
exit 0
elif [ $__ANSWER -le 0 -o $__ANSWER -gt $NUM ]; then
echo "Invalid number"
exit 1
else
echo $CHOICES | cut -d ' ' -f $__ANSWER
fi
done
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment