#!/bin/bash START_HOUR=$1 START_MINUTE=$2 if [ -z "$START_HOUR" ] || [ -z "$START_MINUTE" ]; then START_HOUR="9" START_MINUTE="45" fi date | gawk '{print $4}' | gawk -F":" '{print 8 * 60 - (($1 * 60 + $2) - ('"$START_HOUR"' * 60 + '"$START_MINUTE"'))}'It's actually very simple.

START_HOUR=$1 START_MINUTE=$2Here I create two variables which contain hour and minute I came to the office.

[ -z "$START_HOUR" ]This condition checks whether a variable contains some value. In case I don't pass the date when I came it sets my default which is 9:45. Then very simple oneliner:

date | gawk '{print $4}' | gawk -F":" '{print 8 * 60 - (($1 * 60 + $2) - ('"$START_HOUR"' * 60 + '"$START_MINUTE"'))}'

dateprints current date -> Mon 8 Feb 15:12:09 CET 2016

date | gawk '{print $4}'prints the fourth field (split by whitespace) -> 15:12:55

gawk -F":"-F allows you to specify how do you want the input string to be split - in this case it's ':' so $1 now contains current hour and $2 current minute.

'"$START_HOUR"'this is how you can access shell variables in gawk And then some simple math (note that I assume that working day == 8h):

'{print 8 * 60 - (($1 * 60 + $2) - ('"$START_HOUR"' * 60 + '"$START_MINUTE"'))}'8 * 60 = working day (minutes)

($1 * 60 + $2) - ('"$START_HOUR"' * 60 + '"$START_MINUTE"'))current minute of day minus minute I came to the office

The result of the script is: 148 which means I can go home after 148 minutes :)

You can obviously pass what time you came to work:

./howLong 10 0 prints 162

## No comments:

## Post a Comment