-
Notifications
You must be signed in to change notification settings - Fork 10
/
Copy pathpower_consumption.sh
executable file
·95 lines (74 loc) · 2.4 KB
/
power_consumption.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
#!/bin/bash
echo ""
echo -e "\033[0;31m
PLEASE NOTE THAT THIS SCRIPT WILL CALCULATE THE MONTHLY COST BASED ON THE PROVIDED POWER CONSUMPTION.
Decimals should be preceded by a point or a comma es. ' 21.4 or 21,4 '
\033[0m"
echo ""
# Watt consumption
###################################################################################################
# Regex to exclude all except for " 0-9 and . " from the variable
pattern='^[0-9]+([,.][0-9]+)?$'
# Check if the number provided is good
while true; do
read -p "Enter the watts: " w
w=${w/,/.}
if [[ $w =~ $pattern ]]; then
break
else
echo "Invalid input. Please enter a number."
fi
done
result_w=$(echo "scale=4; $w / 1000" | bc -l)
if (( $(echo "$result_w < 1" | bc -l) )); then
result_w="0$result_w"
fi
echo "Kilowatts: $result_w"
###################################################################################################
# Kw in 24 hours
# Check if the number provided is good
while true; do
read -p "Hours of uptime in a day: " uptime
uptime=${uptime/,/.}
if [[ $uptime =~ $pattern ]]; then
break
else
echo "Invalid input. Please enter a number."
fi
done
result_kw24=$(echo $result_w \* $uptime | bc)
if (( $(echo "$result_kw24 < 1" | bc -l) )); then
result_kw24="0$result_kw24"
fi
echo "Kw/24h: $result_kw24"
###################################################################################################
# Kw in 365 days
result_kw365=$(echo $result_kw24 \* 365 | bc)
if (( $(echo "$result_kw365 < 1" | bc -l) )); then
result_kw365="0$result_kw365"
fi
echo "Kw/year: $result_kw365"
###################################################################################################
# € in one month
# Check if the number provided is good
while true; do
read -p "Price of kw/h? " kw_hour
kw_hour=${kw_hour/,/.}
if [[ $kw_hour =~ $pattern ]]; then
break
else
echo "Invalid input. Please enter a number."
fi
done
kw_costs_365=$(echo "scale=2; ($kw_hour * $result_kw365 + 0.005) / 1" | bc)
if (( $(echo "$kw_costs_365 < 1" | bc -l) )); then
kw_costs_365="0$kw_costs_365"
fi
echo "€/year: $kw_costs_365"
###################################################################################################
# € in one year
euro_month=$(echo "scale=2; $kw_costs_365 / 12" | bc -l)
if (( $(echo "$euro_month < 1" | bc -l) )); then
euro_month="0$euro_month"
fi
echo "€/month: $euro_month"