0

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!

3 Answers 3

1

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

Sign up to request clarification or add additional context in comments.

Comments

1

I know this is an old topic, but i had the same problem and this was the solution

Create a shell script, for example, last_month.sh

year=`date +%Y` month=`date +%m` month=`expr $month - 1` if [ $month -eq 0 ]; then month=12 year=`expr $year - 1` fi if [ $month -lt 10 ] then month="0$month" fi echo $year$month exit 0 

then you can call the script and assign to a variable

./last_month.sh | read anterior echo $anterior 

here returns the last month, but you can easily change to accept parameters

Comments

0

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.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.