Date Incremental in AIX

Hi,
I am trying to find an option in unix to increment for date filed.
I tried in the unix forums. unable to find the direct command to increment the value.

code

a= 01/Apr/13

while [ $a -lt 5 ]
do
   echo $a
   a=`expr $a + 1`
done

example
Input:

a= 01/Apr/13

Output:

 01/Apr/13
 02/Apr/13
 03/Apr/13
 04/Apr/13
 05/Apr/13

Any help greatly appreciated.
Thanks

Try

#/bin/bash

a=01/Apr/13
a=${a//\//-}
n=$(echo "${a:0:2}-1" | bc) 

# OR n=0 also will work

while [ $n -lt 5 ]; do
     date -d "$a + $n day" +%d/%b/%y
n=$((n+1))
done
$ bash test.sh
01/Apr/13
02/Apr/13
03/Apr/13
04/Apr/13
05/Apr/13

Thanks for the quick response but it is giving as an error

a=${a//\//-}: bad substitution

Don't know about your bash

replace a=${a//\//-} by a=$(echo "$a" | sed 's/\//-/g') and then try let me know about result

now that statement not giving an error but giving different error.

"${a:0:2}-1": bad substitution

which bash run this

bash --version | head -1

if you have ksh93 try with ksh

GNU bash, version 4.1.9(1)

I tested on GNU bash, version 4.2.25(1)-release (i686-pc-linux-gnu)

if you have ksh93 use this

#!/bin/ksh93

a=01/Apr/13
for((i=0;i<5;i++))
do
        printf "%(%d/%b/%y)T\n" "$a + $i day"
done
$ ksh date.ksh
01/Apr/13
02/Apr/13
03/Apr/13
04/Apr/13
05/Apr/13

OR else in #2 make n=$(echo "${a:0:2}-1" | bc) to n=0 Since I just wanted to use same variable a for vaiable n so did like that.

Let me know whether it's working or not

tried same

for((i=${a:0:1};i<5;i++))

getting error

`(' unexpected.

How did you try ? which one you used bash or ksh ?

ksh ..

No idea about AIX if you have copied #8 code properly it should work.

and which ksh ?
run this on terminal

$ which ksh
$ ksh --version

/usr/bin/ksh

I've worked on AIX 5.3 before and the ksh93 was version 93n which doesn't support the %T printf formatter.

Usual solution is to either use perl code or download the coreutils package from the AIX linux toolbox site. Once it's installed a GNU date command that supports the --date option can be found /opt/freeware/bin/

---------- Post updated at 08:09 AM ---------- Previous update was at 07:39 AM ----------

If you don't have ability to install packages this perl script should work OK on AIX (don't have access to AIX to test for you):

#! /usr/bin/env perl
use Time::Local;
use POSIX qw(strftime);

my %mon;
@mon{qw/Jan Feb Mar Apr May Jun Jul Aug Sep Oct Nov Dec/} = 0..11;

my ($dd, $mmm, $yy) = split(/[-\/ ]/, $ARGV[0]);
print strftime("%d/%b/%y\n", localtime(timelocal(0,0,0,$dd,$mon{$mmm},2000 + $yy) + $ARGV[1]*24*60*60));

Usage:

$ ./dateadd.pl 01/Apr/13 20
21/Apr/13

Cluber_XL,

COuld you please explain the below ?

my %mon;
@mon{qw/Jan Feb Mar Apr May Jun Jul Aug Sep Oct Nov Dec/} = 0..11;

Thanks,
Pravin

Parameter number 5 of localtime() is month number (0 for Janurary, 1 for Feburary ... 11 for December). So we need to convert the input string from a 3 character string (Jan, Feb, etc) to and integer from 0 to 11.

In a normal 3rd generation language (like C) we would have to use 12 if statements or a big case statement to assign the month number for each string (eg. if (strcmp(mmm, "Jun") == 0) mon=5; ). Perl has a nifty datatype called an associative array, this is like a normal array except the index value can be a string. I use these to lookup them month string and fetch then required number:

my %mon; Here I've defined an associative array called mon.

@mon{qw/Jan Feb Mar Apr May Jun Jul Aug Sep Oct Nov Dec/} = 0..11;

This creates array elements $mon{"Jan"}=0 , $mon{"Feb"}=1 thru $mon{"Dec"}=11

The above statement can be written a number of ways for example:

@mon{"Jan", "Feb", "Mar", "Apr", "May", "Jun", "Jul", "Aug", "Sep", "Oct", "Nov", "Dec"} = (0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11);

%mon = ("Jan", 0, "Feb", 1, "Mar", 2, "Apr", 3, "May", 4, "Jun", 5, "Jul", 6, "Aug", 7, "Sep", 8, "Oct", 9, "Nov", 10, "Dec", 11);

$mon{"Jan"} = 0;
$mon{"Feb"} = 1;
$mon{"Mar"} = 2;
$mon{"Apr"} = 3;
$mon{"May"} = 4;
$mon{"Jun"} = 5;
$mon{"Jul"} = 6;
$mon{"Aug"} = 7;
$mon{"Sep"} = 8;
$mon{"Oct"} = 9;
$mon{"Nov"} = 10;
$mon{"Dec"} = 11;
1 Like