Skip to content

Instantly share code, notes, and snippets.

@JKirchartz
Created February 26, 2019 16:30
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save JKirchartz/ec63471fdd894f8f37b3f88101aa1230 to your computer and use it in GitHub Desktop.
Save JKirchartz/ec63471fdd894f8f37b3f88101aa1230 to your computer and use it in GitHub Desktop.
A simple "google forms"-style question and answer framework for BASH.
We can make this file beautiful and searchable if this error is corrected: It looks like row 7 should actually have 3 columns, instead of 2. in line 6.
kirch What type of bear is best? Brown Bear
kirch Where is the fish? right
kirch How many fish can you eat? as many fish as fit in my mouth
bob What type of bear is best? Polar Bear
bob Where is the fish? left
bob How many fish can you eat? 3
larry What type of bear is best? Black Bear
larry Where is the fish? up
larry How many fish can you eat? 999999999
#! /bin/bash
source ./QA-framework.bash
database="$HOME/qa.tsv"
ask "What type of bear is best?" "Black Bear" "Brown Bear" "Polar Bear" "Panda Bear"
ask "Where is the fish?" "up" "down" "left" "right"
ask "How many fish can you eat?"
write
#! /bin/bash
#
# QA-framework.bash
# Copyright (C) 2019 jkirchartz <me@jkirchartz.com>
#
# Distributed under terms of the NPL (Necessary Public License) license.
#
# A simple "google forms"-style way to survey people via bash
#
# usage:
# #! /bin/bash
# database="path/to/db.tsv"
# source /path/to/QA-framework
# ask "a multiple choice question:" "A" "B" "C"
# ask "an open-ended question"
# write
questions=()
results=()
function write() {
# ensure db exists
if [ ! -e "$database" ]; then
db_dir=`dirname $database`
[ ! -d $db_dir ] && mkdir -p $db_dir
touch "$database"
fi
# write results to db
printf "%s\n" "${results[@]}" >> $database
}
function ask() {
question="$1"
shift;
answers=("$@")
if [ "$#" -le 2 ]; then
echo "$question"
read -p "?> " answer
results+=("$USER $question $answer")
return
else
echo $question
select answer in "${answers[@]}"
do
results+=("$USER $question $answer")
return
done
fi
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment