I'm a bash enthusiast. I've written many scripts, but this time I think this one is too big for me. I'm working on a script that would check the number of hours based on the schedule from the file and sum them up.
I have a file with weekly schedule. By using grep it returns me time schedule (for example 13:00 - 20:30 or 13:30 - 21:30). Those files will come always in the same format, only the hours will be different so my grep query will always work. The only difference is that sometimes it will return four lines (with hours), sometimes less, sometimes more, dependes what's in the given file (I can always use grep -c if the number of those lines matters).
When I subtract the finishing time from the starting time, the result is correct, although it won't work if the hours are a bit odd (for example 14:00 - 16:17). How can I achive that (2h and 17minutes)?
And then I think I should use a loop. I tried a couple of things and got super confused and decided to come here.
Here's what I've got so far (26to31August is obviously the file with the schedule).
#!/bin/bash
#COUNT=$(grep -c '[0-9][0-9]:[0-9][0-9]' 26to31August)
FINISH=$(grep '[0-9][0-9]:[0-9][0-9]' 26to31August |head -1 |awk '{ print $3 }')
START=$(grep '[0-9][0-9]:[0-9][0-9]' 26to31August |head -1 |awk '{ print $1 }')
VAR1=$(date -d "${FINISH}" +%s)
VAR2=$(date -d "${START}" +%s)
HOURS=$((VAR1-VAR2))
SECS2HOURS=$(expr $HOURS / 3600)
echo "That day it's $SECS2HOURS hours"
I want the script to echo each of those lines telling me how many hours it is per line and then add them all together.
Should I use For loop? Should I use a function in the loop?
I'll apprecieate your answers.