generating a sequence depending on the time of the day


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting generating a sequence depending on the time of the day
# 1  
Old 04-16-2009
generating a sequence depending on the time of the day

ould we generate a sequence number based on the time of the day?

e.g. an application is scheduled to run from 0800 hrs to 1700 hrs with a frequency of Once every five minutes

the intention is to "assign" a certain value to a variable when depending on the time of the day

i.e. if the time of the day is 0800 hrs then [since it is the 01st run of the job] the value will be 01 [i.e. 1st run]
at 0805 it will be the second run so run_value=02

the need here is that we could calculate the "latest" run value at any time. Thus say, the time of the day is:

1033 hrs then the current "run number" of the job should be [from 0800-1000 hrs --> 12 run per hour times 2 hours i.e. 12 X 2 = 24 +
6 runs in the 30 minutes since 1000 hrs ....

therefore the run number being 24+6 = 30 i.e. the run number of the last run of the application was 30]

the constraint here is that ... these run numbers are limited i.e. they can be only two characters .... the possible values are
Code:
01 to 99, then a0 [a zero] to a9, aa, ab, ac to az, bo to b9, ba to bz ... z0 to zz]



could we create a tool with awk/perl that will take in
(i) start time of the application
(ii) end time of the application
(iii) frequency of the application [once every x minutes]

and based on this data - calculcate the latest run number of the job.

I know this sounds korny ... but please advise.
# 2  
Old 04-16-2009
What doesn't make sense to me, is that this "run number" will have no discernable meaning after a while. What would happen if you get a run value of say, Q52 ? What would that mean? What happens when you reach zz? Is this some sort of homework assignment?
# 3  
Old 04-16-2009

Code:
#!/bin/bash
## requires bash3.0 or later
## USAGE: scriptname HHMM   HHMM   M
##                   start  end    minutes between runs

btime=${1:-1000}  ## start time HHMM
etime=${2:-1700}  ## end time HHMM
freq=${3:-5}      ## minutes between runs

hm2s()
{
  printf -v "$2" $(( ${1%??} * 3600 + ${1#??} * 60 ))
}

hm2s "$btime" bsecs
hm2s "$etime" esecs

psecs=$(( $freq * 60 ))

varlist=( {{0..9},{a..z}}{{0..9},{a..z}} )

now=$( date +%H%M )
hm2s "$now" nsecs

runs=$(( ($nsecs - $bsecs) / psecs ))
printf "Run no. %s: %s\n" "$runs" "${varlist[$runs]}"

# 4  
Old 04-17-2009
thanks a lot cfajohnson.

I am faced with once restriction here, I have the following shells installed on the machine:

hash
refresh
msh
recsh
chsh
csh
tsh
sh
psh
ksh
bsh
Rsh
rsh
remsh
ssh
fcinit.sh
fcinit.csh

the bash is absent. is there a way we could change the above code to use ksh/bsh?
# 5  
Old 04-17-2009

In any POSIX shell:

Code:
btime=${1:-1000}  ## start time HHMM
etime=${2:-1700}  ## end time HHMM
freq=${3:-5}      ## minutes between runs
bsecs=$(( ${btime%??} * 3600 + ${btime#??} * 60 ))
esecs=$(( ${etime%??} * 3600 + ${etime#??} * 60 ))
psecs=$(( $freq * 60 ))
str='0 1 2 3 4 5 6 7 8 9 a b c d e f g h i j k l m n o p q r s t u v w x y z'
for l1 in $str;
do
  for l2 in $str
  do
    varlist[${#varlist[@]}]=$l1$l2
  done
done
now=$( date +%H%M )
nsecs=$(( ${now%??} * 3600 + ${now#??} * 60 ))"
runs=$(( ($nsecs - $bsecs) / psecs ))
printf "Run no. %s: %s\n" "$runs" "${varlist[$runs]}"

# 6  
Old 04-17-2009
cfajohnson - your script is awesome ... it is working ... lots of thanks

UXBLCASP01 jain1r:/home/jain1r > generate_sequence.sh.2 0800 1700
Run no. 35: 0z
UXBLCASP01 jain1r:/home/jain1r >

But please be aware that the intention is that the first run numbers can be as:
Code:
01
02
03
04
05
06
07
08
09
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
96
97
98
99


only after 99th run number has completed, can we have
Code:
a0
a1
a2
a3
a4a5
....



Please suggest a method.
# 7  
Old 04-17-2009
Quote:
Originally Posted by zainravi
cfajohnson - your script is awesome ... it is working ... lots of thanks

UXBLCASP01 jain1r:/home/jain1r > generate_sequence.sh.2 0800 1700
Run no. 35: 0z
UXBLCASP01 jain1r:/home/jain1r >

But please be aware that the intention is that the first run numbers can be as:
Code:
01
...
99

only after 99th run number has completed, can we have
Code:
a0
a1
a2
a3
a4a5
....

Please suggest a method.

Break the loop into two; the first to create the numbered elements and the second to add the letters.
Login or Register to Ask a Question

Previous Thread | Next Thread

9 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Get day of week from epoch time

Need assistance . Below code gives me the date but I wanted output as day of the week (wday) . Code: use Time::Local; my $time=timelocal(1,2,3,9,11,2013); $theTime = localtime($time); print "$theTime\n"; Result: Mon Dec 9 03:02:01 2013 Wanted output as only Mon (2 Replies)
Discussion started by: ajayram_arya
2 Replies

2. Shell Programming and Scripting

Converting date/time and generating offsets in bash script

Hi all, I need a script to do some date/time conversion. It should take as an input a particular time. It should then generates a series of offsets, in both hour:minute form and number of milliseconds elapsed. For 03:00, for example, it should give back 04:02:07 (3727000ms*) 05:04:14... (2 Replies)
Discussion started by: emdan
2 Replies

3. AIX

Time of Day Question

I have a question i need clarification. We had a problem on a p520 system and had to pull the battery on the service processor card. That did the trick and system booted. We forgot to set the time in the service processor before booting. Once AIX was booted we changed the date and time there. ... (1 Reply)
Discussion started by: ryan0911
1 Replies

4. UNIX for Dummies Questions & Answers

create time sequence in bash

hi all, hope someone can assist in this. I'm trying to make a sequence of time in bash so that the output prints the following; 1950-01-01 1950-02-01 1950-03-01 ... 1999-11-01 1999-12-01 In R, i can issue the following command; t1 <- ISOdate(1950,1,1,0,0,0) t2 <-... (0 Replies)
Discussion started by: Muhammad Rahiz
0 Replies

5. Shell Programming and Scripting

How to? Every day, same time... script...

I manage to find and proces one script, now i need to automate this script for every day at the same time... (2 Replies)
Discussion started by: voltaza
2 Replies

6. Shell Programming and Scripting

perl merge two files by the time sequence

Hi Guys, i have two files: fileA: 20090611 00:00:11 20090611 00:00:11 20090611 00:00:24 20090611 00:01:10 20090611 07:13:00 fileB: 20090611 00:00:01 20090611 00:00:12 20090611 00:00:24 20090611 00:01:12 20090611 09:13:00 want to make two files into a single file, but follow the... (14 Replies)
Discussion started by: jimmy_y
14 Replies

7. Shell Programming and Scripting

Moving files which are generating time to time

Hi all, I always getting great response from this forum, that why i am putting again.... I am working in a company which is giving ATM support.In one of my production server a lot of files are getting generated every day. I want to move these files to another name. The file name which is... (4 Replies)
Discussion started by: Renjesh
4 Replies

8. Shell Programming and Scripting

generating reports based on time field of network data

hi i have data extracted in the following format ranging around 300000 to 800000 records in a text file , the format is of network data . No. Time Source Destination Protocol 1 1998-06-05 17:20:23.569905 HP_61:aa:c9 HP_61:aa:c9 ... (1 Reply)
Discussion started by: renukaprasadb
1 Replies

9. Shell Programming and Scripting

Generating files with time interval of fifteen minutes

Hi Guys, I have two dates as start date and end date.. i need to generate files within these two dates with time interval of half an hour.... i.e. Start Date=25/09/07 12:00:00 End Date=26/09/07 12:00:00 Now i need to generate files every half an... (0 Replies)
Discussion started by: aajan
0 Replies
Login or Register to Ask a Question