-
Notifications
You must be signed in to change notification settings - Fork 0
/
fizzbuzz.sh
89 lines (74 loc) · 2.19 KB
/
fizzbuzz.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
#!/bin/bash
# =======================
# CONFIG
# =======================
# Color code variables
purple="\e[1;35m"
blue="\e[0;34m"
red="\e[0;31m"
reset="\e[0m"
# File to write
directory=results
file=${directory}/results-$(date +%s).log
# Sequence config
# check if a variable is set: https://stackoverflow.com/a/13864829
# read start
if [ -z ${1+x} ]; then start=0; else start=$1; fi
# read stop
if [ -z ${2+x} ]; then stop=10; else stop=$2; fi
# =======================
# HEADER
# =======================
echo -e "
${purple}888888 88 8888P 8888P 8888Yb 88 88 8888P 8888P
88__ 88 dP dP 88__dP 88 88 dP dP
88'' 88 dP dP 88''Yb Y8 8P dP dP
88 88 d8888 d8888 88oodP 'YbodP' d8888 d8888${reset}
Start: $start | Stop: $stop
"
echo "$MONGO_SERVER"
# =======================
# SCRIPT
# =======================
# Check if the directory to write exists
[[ -d "$directory" ]] || mkdir "$directory"
# FizzBuzz loop
while [ $start -le $stop ]
do
# Check if divide by 3 & 5
if [[ 0 -eq "($start%3) + ($start%5)" ]]
then
echo -e "${purple}fizz buzz${reset}"
echo "fizz buzz" >> $file
mongosh ${MONGO_SERVER} -u ${MONGO_USERNAME} -p ${MONGO_PASSWORD} <<EOF
use fizzbuzz
db.results.insertOne( { num: $start, value: "fizz buzz" } )
EOF
# Check if divide by 5
elif [[ 0 -eq "($start%5)" ]]
then
echo -e "${red}buzz${reset}"
echo "buzz" >> $file
mongosh ${MONGO_SERVER} -u ${MONGO_USERNAME} -p ${MONGO_PASSWORD} <<EOF
use fizzbuzz
db.results.insertOne( { num: $start, value: "buzz" } )
EOF
# Check if divide by 3
elif [[ 0 -eq "($start%3)" ]]
then
echo -e "${blue}fizz${reset}"
echo "fizz" >> $file
mongosh ${MONGO_SERVER} -u ${MONGO_USERNAME} -p ${MONGO_PASSWORD} <<EOF
use fizzbuzz
db.results.insertOne( { num: $start, value: "fizz" } )
EOF
else
echo "$start"
echo "$start" >> $file
mongosh ${MONGO_SERVER} -u ${MONGO_USERNAME} -p ${MONGO_PASSWORD} <<EOF
use fizzbuzz
db.results.insertOne( { num: $start } )
EOF
fi
start=$(( $start + 1 ))
done