grader

Created Tuesday 07 April 2020

#!/bin/bash


function quiz_to_resultfile () { # 
	quiz="$1"
	key="./THEKEYGOESHERE"
	resultfile=$(mktemp)
	tempforcount=$(mktemp)
	numberofquestions=20

	grep -Fxvf "$key" "$quiz" > "$resultfile"

	# file thing per number? let's see?

	tail -n +2 "$resultfile" > $tempforcount
	
	for i in $(seq 1 $numberofquestions); do
		numberchonk="\[q${i}\]"
		result=$(grep "$numberchonk" "$tempforcount")
#		echo -n "-- RESULT IS #${result}# --"
	        if [[ $result == "" ]]; then
			echo -n ","
		else
		 	answer=$(echo $result | cut -d ' ' -f3)
			echo -n "${answer},"
		fi
	done	



	# FSUID
        idline=$(grep fsuid "$resultfile")
	fsuid=$(echo $idline | cut -d ' ' -f3 | tr [A-Z] [a-z])

	echo -n "${fsuid},"

	# code

	code=$(echo "$1" | cut -d '-' -f3)

	echo -n "${code},"
	#number wrong:
	wrong=$(echo "$(wc -l < "$resultfile") - 1" | bc)


	right=$(echo $numberofquestions	- $wrong | bc)
	

	rawgrade=$(echo $right / .${numberofquestions} | bc)

	echo -n "${rawgrade},"


	
	echo -n "${wrong},"		



#	cat "$resultfile" | tr -d '\n'
	echo ""


}

function keyline () {
	# I got number of quesitons above, no?
	keylinekey="$1"
	tempy=$(mktemp)
	tail -n +4 $keylinekey > $tempy
	while read line; do
		echo -n "$line , " | tr -d [\[\]]
	done < $tempy	
	echo ""
}

keyline "./THEKEYGOESHERE" | tee ./finalresults.csv

for i in *thing*; do quiz_to_resultfile "$i" | tee -a ./finalresults.csv ; done