tags:

views:

132

answers:

3

Ok, so I need to translate a script from a nice linux & bash configuration to ksh in hp-ux. Each and every command expects a different syntax and i want to kill myself. But let's skip the rant.

This is part of my script

anterior=`date +"%Y%0m" -d '1 month ago'`

I basically need to get a past date in format 201002. Never mind the thing that, in the new environment, %0m means "no zeroes", while actually in the other one it means "yes, please put that zero on my string". It doesn't even accept the "1 month ago". I've read the man date for HP-UX and it seems you just can't do date arithmetic with it.

I've been looking around for a while but all i find are lengthy solutions. I can't quite understand that such a typical administrative task like adding dates needs so much fuss. Isn't there a way to convert my one-liner to, well, i don't know, another one? Come on, i've seen proposed solutions that used bc, had thirty plus lines and magic number all over the script. The simplest solutions seem to use perl... but i don't know how to modify them, as they're quite arcane.

Thanks!

A: 

since yours is simple case of year and month, you could always craft your own date arrays. eg

year=`date +%Y`
month=`date +%m`
anterior=`awk -vm="$month" -vyr="$year" 'BEGIN{
  mth["01"]="12"; mth["02"]="01"
  mth["03"]="02"; mth["04"]="03"
  mth["05"]="04"; mth["06"]="05"
  mth["07"]="06"; mth["08"]="07"
  mth["09"]="08"; mth["10"]="09"
  mth["11"]="10"; mth["12"]="11"
  if ( m=="01") { yr-- }
  print yr mth[m]
}' `
echo $anterior

But note that it is only simple and serves to produce only previous 1 month. You can also try the ksh script here

ghostdog74
A: 

What I would do would be to actually grab the source code for date from Linux and compile it under HP-UX as betterdate, then just use that.

I haven't used HPUX since version 9 but it was getting a bit long in the tooth even way back then. The great advantage with open source is that you can just recompile it under the target platform.

Now this may not work due to incompatibilities with API s and such but I'd be looking at this as a first attempt. The work that date has to do is fairly simple in terms of what APIs it would need to call.

Then you have the full power of Linux date at your disposal.

paxdiablo
This would be the best solution, especially for the rest of the world :) but the client won't accept it.
Alex Ati
What _will_ they accept? Are you allowed to compile any C? Can you use the other "standard" tooling (awk, sed, grep et al)? Or does it _have_ to be ksh on its own (presumably date is okay)?
paxdiablo
It has to be a script, it's out of the question to start an homologation proccess for a third-party program...
Alex Ati
A: 

This is the solution I came to (warning: I'm working at a client with draconian security measures, so i don't even have a way to test it, i need to send my scripts to a sysadmin to test them and debug through him).

typeset -Z2 lastmonth
month=`date +%m`
year=`date +%Y`

lastmonth=$((month-1))
if (( month == 1));then
lastmonth=12
year=$((year-1))
fi

Anyway, it strikes me that there is no simpler way to do that.

Alex Ati

related questions