-
Notifications
You must be signed in to change notification settings - Fork 0
/
capitals.sh
executable file
·66 lines (53 loc) · 1.49 KB
/
capitals.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
#!/bin/bash
# {MYVAR%pattern} delete shortest match of pattern from the beginning
# {MYVAR##pattern} delete longest match of pattern from the beginning
# {MYVAR%pattern}
# {MYVAR%%pattern} .. same from the end
HERE=$(cd "$(dirname "$0")" && pwd)
declare -a COLORS
COLORS[0]='\033[0;31m'
COLORS[1]='\033[0;32m'
COLORS[2]='\033[0;33m'
COLORS[3]='\033[0;34m'
COLORS[4]='\033[0;35m'
COLORS[5]='\033[0;36m'
selectedColor=${COLORS[$RANDOM % ${#COLORS[@]}]}
factsUrl='http://www.sciencekids.co.nz/sciencefacts/countries'
keepImportantLines(){
sed -i '/<p align=\"left\">/!d' "$HERE/facts/$1.txt"
}
deletePTags(){
sed -i 's/<\/p>//g' "$HERE/facts/$1.txt"
sed -i 's/<p align=\"left\">//g' "$HERE/facts/$1.txt"
}
deleteLiTags(){
sed -i 's/<\li>//g' "$HERE/facts/$1.txt"
}
getFacts(){
a="$1"
if ! [ -f "$HERE/facts/$a.txt" ]; then
touch "$HERE/facts/$a.txt"
fi
curl -s "$factsUrl/$a.html" >> "$HERE/facts/$a.txt"
keepImportantLines "$a"
deletePTags "$a"
deleteLiTags "$a"
}
showSomeRandomFact(){
shuf -n 2 "$HERE/facts/$1.txt"|grep -v "img src="|grep -v "a href="
}
capitalsFile="$HERE/capitals.txt"
lineChosen=$(shuf -n 1 "$capitalsFile")
country=${lineChosen%:*}
capital=${lineChosen##*:}
countryLowerCase=$(echo "$country"|tr '[:upper:]' '[:lower:]')
read -e -p "What's the capital of $country? " answer
if [ "$answer" == "$capital" ]; then
echo "CORRECT!"
else
echo "FALSE!"
fi
if ! test -s "$HERE/facts/$countryLowerCase.txt" ; then
getFacts "$countryLowerCase"
fi
showSomeRandomFact "$countryLowerCase"