English 中文(简体)
Getting past dates in HP-UX with ksh
原标题:

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!

最佳回答

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.

问题回答

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

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





相关问题
Mysql compaire two dates from datetime?

I was try to measure what is faster and what should be the best way to compare two dates, from datetime record in MySql db. There are several approaches how to grab a date from date time, but I was ...

iPhone Date Picker rolls over to 2010 on December 27th?

So I implemented a UIDatepicker in one of my applications for scheduling and autodialing teleconferences... everything is pretty much ready to go except, while testing I noticed that when the date ...

Convert date Python

I have MMDDYY dates, i.e. today is 111609 How do I convert this to 11/16/2009, in Python?

specifying date format when using $form->inputs() in CakePHP

I am wondering if there is a way to specify the date format in the forms created using CakePHP s $form->inputs(); Please note that this is not the individual $form->input() but instead $form->inputs() ...

NSDateFormat, super simple! Where am I screwing up?

Ok, this is really simple, maybe I m a getting a bit burnt out, but seems like it should work, Query XML feed, put out date string, format, display in a cell. The issue is I m a getting a NULL ...

sqlite writing a date into an email

I am exporting a date value from sqlite and placing it into an email. The date appears like this 279498721.322872 I am using Objective C in an Iphone App. Does anyone know how to make this export ...

热门标签