0

I'm working with a device that needs to receive time instructions formatted on a text file to make backup copies on hour-based timeframes. It has 4 channels so I still need to flag the channels. Format is MMDDhhmmYYYY;C (where MM=month, DD=day, hh=24-hour hour, mm=minute, YYYY=year, C=channel number)

Lets say if I need to specify a 2-hour interval between Jan 25 2016 14:00 and Jan 25 2016 16:00 for channels 1 and 2, I need to put 4 lines on the text file.

012514002016;1 012514002016;2 012515002016;1 012515002016;2 

It's OK to do this manually for small queries, but for a full day on 4 channels, I have to write 96 lines. I'm willing to make some script to specify the start date and time, end date and time and which channels to request, then print the output to a text file on the required format, but I don't even know where to start. I saw lots of scripts that can count the interval between dates, but nothing for actually SHOW the interval. The client environment is limited, but at least it's bash 4.0

3
  • I assume you have no say in the "date format" (which I find incredibly daft - why don't people use ISO!?!)? Commented Jan 25, 2016 at 22:23
  • The date format came specified on the device. Unfortunately there's no way to change it. I don't even know why I have to input the minute digits, since the device can only search at minute 00. Commented Jan 25, 2016 at 22:31
  • 1
    Does it help that on GNU you can use e.g. date -d 'Mon Jan 25 15:14:11 PST 2016 + 1 hour' to increment by an hour, and date -d 'Mon Jan 25 16:14:11 PST 2016' +'%m%d%H%M%Y;1' to format? Commented Jan 25, 2016 at 23:15

1 Answer 1

1

Well, here is a start. This one only increments hours and doesn't carry hours to days, or minutes to hours, or actually anything.

#!/bin/sh MM=$1 dd=$2 hh=$3 mm=$4 yyyy=$5 h2=$6 ni=$7 while [ $ni -gt 0 ]; do for i in 1 2 3 4; do printf '%02d%02d%02d%02d%04d;%d\n' $MM $dd $hh $mm $yyyy $i done hh=$(($hh + $h2)) ni=$(($ni - 1)) done 

So to do 12 2-hour increments in a single day:

$ sh interval 01 25 14 00 2016 2 12 
Sign up to request clarification or add additional context in comments.

1 Comment

printf's first $mm should be $MM.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.