1

I have a bash script on AIX 7 that runs a database query and the output I get is a timestamp in the following format: 201607130319.

Now I would like to compare the timestamp with current time (date +%Y%m%d%H%M - 201607201802) and check the difference in minutes. I basically need to know if the difference is more than 10 minutes.

I know I can do this on Linux using date -d but it is not available on AIX. I am not allowed to install anything on the server either so what are my options here?

3

Perl is probably installed, so you can do

timestamp=$( some process )   # timestamp=201607130319
perl -se '
    use Time::Local;
    if ($ts =~ /^(\d{4})(\d\d)(\d\d)(\d\d)(\d\d)$/) {
        $time = timelocal(0,$5,$4,$3,$2-1,$1-1900);
        $now  = time;
        if (abs( $time - $now ) > 600) {
            print "more than 10 minutes\n";
        }
    }
' -- -ts="$timestamp"
2
  • You are a star! I have spent too long trying to figure this out without any luck. Just out of curiosity, is there a way to do this using bash or any other shell available on AIX?
    – Debianuser
    Jul 20 '16 at 17:10
  • 1
    No. bash does not have any facilities to work with dates and times.GNU date is an obvious choice. GNU awk has mktime() and strftime() functions, but unlikely you'll have that on AIX. Perl is the ubiquitous all-purpose language. Jul 20 '16 at 17:46

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.