You are not logged in.

#1 2010-02-19 14:55:40

imchairmanm
Member
Registered: 2009-09-26
Posts: 13

Really simple bash time question

I'm really new to bash and scripting in general, so bare with me.

The script I'm trying to make is supposed to take an initial time as a parameter and schedule the atd to run a command at that time, and every 5 minutes afterwards for the next hour.  The really simple problem that I'm having trouble with is converting to the next hour after it hits 60 minutes.  So say that the starting time I give is 1330.  I want it to schedule a command to run every 5 minutes until 1430, but I don't know how to get it to translate 1360 to 1400, 1365 to 1405 and so on.

This is what I have so far with an echo statement being substituted for the actual scheduling:

#!/bin/bash
 
starttime=$1
i=1

while [ "$i" -lt "14" ]
do
     echo "start time = $starttime"
     starttime=$(( $starttime + 5 ))
     i=$[$i+1]
done

As you'd guess, if you run it with "1330" as the parameter, it goes to 1390 instead of 1430.  What's the best way to translate those numbers?  This problem seems to be embarrassingly simple, but I'm having trouble with it.

Offline

#2 2010-02-19 15:30:27

n0dix
Member
Registered: 2009-09-22
Posts: 956

Re: Really simple bash time question

Get two variables, one for the min and other to the hours. I think it is the easy way, so startime become in two arguments.

Offline

#3 2010-02-19 15:56:16

vacant
Member
From: downstairs
Registered: 2004-11-05
Posts: 816

Re: Really simple bash time question

Here's a clue if you want to use one input:

$ date -d "+100minutes 14:30" +%H:%M
16:10

Offline

#4 2010-02-19 16:05:59

lolilolicon
Member
Registered: 2009-03-05
Posts: 1,722

Re: Really simple bash time question

It seems you want to feed the time in %H%M format to some other program (atd?). Does this receiver program only support this format?

Either way, you can always calculate time in seconds/minutes, and then convert to/from hour_min format, e.g.

min2hhmm() { printf '%02d%02d\n' $(($1 / 60)) $(($1 % 60)); }
hhmm2min() { echo $((${1:0:2} * 60 + ${1:2:2})); }
delta=5
min2hhmm $(($(hhmm2min $1) + $delta))

$ ./t.sh 0359
0404


This silver ladybug at line 28...

Offline

Board footer

Powered by FluxBB