-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathquiz.sh
273 lines (240 loc) · 6.83 KB
/
quiz.sh
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
#!/bin/bash
##
# A BASH Wrapper for the quizapi.io API
# Version: 1.0.0
##
##
# Colors
##
green='\e[32m'
blue='\e[34m'
clear='\e[0m'
orange='\e[33m'
red='\e[31m'
##
# Check if jq is installed
##
if ! [ -x "$(command -v jq)" ] ; then
echo "The jq command is required! Please install it and then try again"
exit 1
fi
##
# API URL
##
url='quizapi.io'
quiz_endpoint='api/v1/questions'
##
# Quiz session file
##
temp_quiz=$(mktemp /tmp/temp-quiz.XXXXXX)
##
# Color Functions
##
ColorGreen(){
echo -ne $green$1$clear
}
ColorBlue(){
echo -ne $blue$1$clear
}
ColorRed(){
echo -ne $red$1$clear
}
ColorOrange(){
echo -ne $orange$1$clear
}
##
# Help function
##
function usage() {
echo "Usage: $0 -a API_KEY [-c Category] [-d Difficulty] [-t Tags]" 1>&2; exit 1;
}
##
# Read the arguments with the getopts command
##
while getopts "a:c:d:t:" o; do
case "${o}" in
a)
API_KEY=${OPTARG}
;;
c)
category=${OPTARG}
;;
d)
difficulty=${OPTARG}
;;
t)
tags=${OPTARG}
;;
*)
usage
;;
esac
done
shift $((OPTIND-1))
##
# Check if an API key was specified
##
if [ -z "${API_KEY}" ]; then
usage
fi
##
# Question formatting
##
function get_questions(){
content=$( curl -Ls ${url}/${quiz_endpoint} -G -d limit=1 -d category=${category} -d difficulty=${difficulty} -d tags=${tags} -H "X-Api-Key: ${API_KEY}" -o ${temp_quiz} )
}
##
# Check if the API request was successful
##
function check_error(){
check_auth=$(cat ${temp_quiz} | jq .error?)
if [ "$check_auth" == '' ] ; then
content_check="1"
else
content_check="0"
fi
}
##
# Check if there are any questions found
##
function check_if_null(){
question_count=$(cat ${temp_quiz})
if [ "$question_count" == "No questions found.." ] ; then
echo "No questions found.."
echo "Try with different category or tag."
rm ${temp_quiz}
exit 1
fi
}
##
# Parse the Json output
##
function format_question(){
#jq .[0].question
question=$( cat ${temp_quiz} | jq .[0].question )
answer_a=$( cat ${temp_quiz} | jq .[0].answers.answer_a )
answer_b=$( cat ${temp_quiz} | jq .[0].answers.answer_b )
answer_c=$( cat ${temp_quiz} | jq .[0].answers.answer_c )
answer_d=$( cat ${temp_quiz} | jq .[0].answers.answer_d )
answer_e=$( cat ${temp_quiz} | jq .[0].answers.answer_e )
answer_f=$( cat ${temp_quiz} | jq .[0].answers.answer_f )
answer_a_correct=$( cat ${temp_quiz} | jq -r .[0].correct_answers.answer_a_correct )
answer_b_correct=$( cat ${temp_quiz} | jq -r .[0].correct_answers.answer_b_correct )
answer_c_correct=$( cat ${temp_quiz} | jq -r .[0].correct_answers.answer_c_correct )
answer_d_correct=$( cat ${temp_quiz} | jq -r .[0].correct_answers.answer_d_correct )
answer_e_correct=$( cat ${temp_quiz} | jq -r .[0].correct_answers.answer_e_correct )
answer_f_correct=$( cat ${temp_quiz} | jq -r .[0].correct_answers.answer_f_correct )
correct=$( cat ${temp_quiz} | jq -r .[0].correct_answer )
multiple_correct_answers=$( cat ${temp_quiz} | jq -r .[0].multiple_correct_answers )
#correct_check=$( echo ${correct} | awk -F'Correct: ' '{ print $2 }' )
correct_check=$( echo ${correct} )
}
##
# Check if multiple possible
##
function multiple_correct_answers(){
if [ $multiple_correct_answers == true ] ; then
echo -ne "$(ColorGreen 'There are multiple answers!')
"
else
echo -ne "$(ColorGreen 'There is only 1 correct answer!' )
"
fi
}
##
# Show answers that do not have null values
##
function check_answers_value(){
options=()
correct_options=()
for x in {a..f} ; do
correct_answers="answer_${x}_correct"
correct_answers_value=$(eval echo "\$$correct_answers")
answer_value="answer_${x}"
answer_value_check=$(eval echo "\$$answer_value")
if ! [ "$answer_value_check" == null ] ; then
#echo $(ColorGreen 'a:)') ${answer_a}
options+=("${answer_value_check}")
if [ $correct_answers_value == true ] ; then
correct_options+=("${answer_value_check}")
fi
fi
done
}
##
# Call all functions
##
function main(){
get_questions > /dev/null
check_error 2>/dev/null
check_if_null 2>/dev/null
format_question 2>/dev/null
check_answers_value 2>/dev/null
}
main
##
# Check if the answer is correct
##
function check_answer() {
answer="answer_${answer_str_lower}_correct"
answer_value=$(eval echo "\$$answer")
if [ "$answer_value" == "true" ] ; then
echo -ne "$(ColorGreen 'Correct Answer' )
" ;
else
echo -ne "$(ColorRed 'Wrong Answer' )
" ;
fi
}
menu() {
echo ""
multiple_correct_answers
echo -ne "
${question}
"
for i in ${!options[@]}; do
printf "%3d%s) %s\n" $((i+1)) "${choices[i]:- }" "${options[i]}"
done
if [[ "$msg" ]]; then echo "$msg"; fi
}
##
# Print Question if the API call was successful
##
if [ "$content_check" -gt "0" ] ; then
prompt="Check an option (again to uncheck, ENTER when done): "
while menu && read -rp "$prompt" num && [[ "$num" ]]; do
[[ "$num" != *[![:digit:]]* ]] &&
(( num > 0 && num <= ${#options[@]} )) ||
{ msg="Invalid option: $num"; clear; continue; }
((num--)); msg="${options[num]} was ${choices[num]:+un}checked"
[[ "${choices[num]}" ]] && choices[num]="" || choices[num]="+"
clear
done
answer_selected=()
echo ""
printf "Selected was: "; msg=" nothing"
for i in ${!options[@]}; do
[[ "${choices[i]}" ]] && { printf " %s" "${options[i]}"; answer_selected+=(${options[i]}) ; msg=""; }
done
echo "$msg"
echo "Correct: is: " ${correct_options[@]}
correct_ansers_string=${correct_options[@]}
answers_given_string=${answer_selected[@]}
if [ "${answers_given_string}" == "$correct_ansers_string" ] ; then
echo -ne "$(ColorGreen 'Correct Answer' )" ;
echo ""
else
echo -ne "$(ColorRed 'Wrong Answer' )" ;
echo ""
fi
else
echo "An error occured:"
cat ${temp_quiz}
echo ""
rm -f ${temp_quiz}
exit 1
fi
##
# Clean temp files
##
rm -f ${temp_quiz}