-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathfortuneteller.sh
executable file
·244 lines (186 loc) · 6.77 KB
/
fortuneteller.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
#!/bin/bash
###############################################################################
#
# Fortuneteller pseudocode - MNXB01-2018 Homework
#
# Author: Florido Paganelli florido.paganelli@hep.lu.se
#
# Description: this script downloads a fortune database
# from a public API and presents it to the
# user at login.
# It takes in input the options
# --printinfo
# --cleanup
# -h|--help
# that shows the downloaded fortunes.
#
###############################################################################
#
# Student: Jonas Petersson
#
# E0 (1 point)
# inspect the website http://fortunecookieapi.herokuapp.com/
# and understand the format of the data that the API returns
# at http://fortunecookieapi.herokuapp.com/v1/fortunes
# understand what kind of data structure teh website returns and write it here:
#
# Datastructure format is JSON
###### DO NOT TOUCH THE CODE BELOW #####################################
# This part of code defines variables and functions you can use
# during the exercises. It's like a small code library.
# This variable contains the name of this script.
SCRIPTNAME=$0
# Read options using GNU getopt.
# see info at
# - http://www.bahmanm.com/blogs/command-line-options-how-to-parse-in-bash-using-getopt
# - https://dustymabe.com/2013/05/17/easy-getopt-for-a-bash-script/
OPTS=`getopt -o h --long help,printinfo,cleanup -n $SCRIPTNAME -- "$@"`
# I use this function to show help.
usage() {
echo
echo "usage:"
echo " $0 [--printinfo|--cleanup|-h|--help]"
echo -e "\t--printinfo prints the contents of intermediate fortune files and exits"
echo -e "\t--cleanup delete temporary files and ask the user for each of them"
echo -e "\t-h this help"
}
# This line strips the quotes from the options, it's required when using
# the getopt command.
eval set -- "$OPTS"
# this while block processes the command line options.
while true; do
case $1 in
-h|--help)
# print help using the usage function
usage
# exit with no error
exit 0
;;
--printinfo)
# This variable contains --printinfo if the user wants to see information
# about where the fortunes file is saved.
PRINTINFO=$1
shift
;;
# This variable contains --cleanup if the user wants to delete the
# temporary folders and their contents
--cleanup)
CLEANUP=$1
shift
;;
--)
shift
break
;;
*)
echo "Invalid option: $1"
# print help using the usage function
usage
# exit with error
exit 1
;;
esac
done
# Helper functions
# The dumpfile function takes in input a filename
# and prints its contents if and only if the
# --printoption option was used when calling fortuneteller.
# Example usage of the function:
# dumpfile /path/to/filename
dumpfile() {
# Store the passed filename in a variable called FILENAME
FILENAME=$1
# test if the printinfo option is passed to the command line then
# print the content of the downloaded fortunes file using cat.
if [[ "${PRINTINFO}x" == "--printinfox" ]]; then
echo "printing $FILENAME..."
cat $FILENAME
fi
}
# Cleanup function. usage:
# cleanup <foldername>
# It deletes files if and only if
# the option --cleanup is passed to ./fortuneteller.sh
# It asks the user for permission to remove each file. (rm -i)
cleanup() {
DIRTOREMOVE=$1
# perform cleanup if specified by the user
if [[ "${CLEANUP}x" == "--cleanupx" ]]; then
echo "cleaning temporary folders..."
rm -ir $DIRTOREMOVE
fi
}
############ END OF UNTOUCHABLE CODE ##################################
############ START OF EXERCISES (EDIT THIS PART!) #####################
# E1 (1 point)
# Create a temporary directory with the mktemp command.
# save its name in a variable named FTTEMPDIR
# make sure the directory is created in /tmp and is called ftellerXXX
# where XXX are random characters created by mktemp. Read mktemp
# documentation to understand more.
FTTEMPDIR=$(mktemp -d /tmp/ftellerXXX)
# E2 (1 point)
# Download the database of fortunes from the URL
# http://fortunecookieapi.herokuapp.com/v1/fortunes
# using the wget command.
# save it into FTTEMPDIR
# make sure the output of wget is not visible to the user.
cd $FTTEMPDIR
wget -o fortunes http://fortunecookieapi.herokuapp.com/v1/fortunes
# E3 (1 point)
# use the dumpfile function to print the content of the fortunes file.
dumpfile fortunes.1
# E4 (2 point) Use the tool json_pp to print nicely the
# fortunes file and save it as $FTTEMPDIR/fortunes_pp
# (hint: use cat and the pipe | to pass input to json_pp, and use
# the redirector > to write the output of the commands to a file.)
touch prettyjson.json
cat fortunes.1 | json_pp > prettyjson.json
# E5 (1 points)
# use the dumpfile function to print the content of the fortunes_pp file.
dumpfile prettyjson.json
# E6 (2 points) exit with 0 if --printinfo specified.
# cleanup if --cleanup is specified.
# Inform the user with a message:
# "Exiting because --printinfo specified"
# call the cleanup function to delete the temporary files.
# To write this you can take inspiration in the IF statements in the
# helper functions at the beginning of this file.
if [[ "${PRINTINFO}x" == "--printinfox" ]]; then
echo "Exiting because --printinfo specified"
if [[ "${CLEANUP}x" == "--cleanupx" ]]; then
cleanup $FTTEMPDIR
fi
exit 0
fi
# E7 (1 point)
# Extract only the lines containing "message" and save them as $FTTEMPDIR/fortunes_messages
touch fortunes_messages.txt
jq .[].message prettyjson.json > fortunes_messages.txt
# E8 (2 points) calculate the number of lines in the fortunes_messages file
# and store it in a variable NUMMSG
# hint: use the command wc, the pipe and a tool called "cut"
# see cut examples here:
# https://www.thegeekstuff.com/2013/06/cut-command-examples
NUMMSG=$(wc -l fortunes_messages.txt)
NUMMSG=${NUMMSG// fortunes_messages.txt/}
# E9 (2 points)
# pick a random number between 1 and NUMMSG and save it into the variable
# CHOSEN
# use the predefined variable RANDOM and the bc command. For hints see:
# https://coderwall.com/p/s2ttyg/random-number-generator-in-bash
r=$RANDOM
CHOSEN=$((r %= $NUMMSG))
#E10 (2 points)
# Pick the CHOSEN message from the list and print it on screen
# use head and tail to achieve this. Search on the internet:
# "print by line number using bash"
# put the message in a variable called MESSAGE
MESSAGE=$(sed "${CHOSEN}q;d" fortunes_messages.txt)
# E11 (1 point) extract only the message content using cut
MESSAGE=${MESSAGE//'"'/}
# E12 (1 point) Use echo -e to print spaces and newlines to show the
# output as in the simple_call_output file
echo -e $MESSAGE
# calling cleanup to clean the tmp folder
cleanup $FTTEMPDIR