Unix & Linux Stack Exchange is a question and answer site for users of Linux, FreeBSD and other Un*x-like operating systems. Join them; it only takes a minute:

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

In machine A (running Oracle Linux Server release 6.4), I am able to get the date of 1 month ago intelligently by using the following command:

$(date -d"1 month ago" '+%Y0%m')

But it is not working in machine B(AIX), is there an alternative way to achieve this? Both are in .sh files and run with:

sh Test.sh

Error shown in machine B:

date: illegal option -- d
Usage: date [-u] [+Field Descriptors]
share|improve this question
2  
When you say, not working in ksh, do you mean, not working on the same machine that the sh solution works on, when using ksh or do you mean not working on another machine entirely which has ksh as the default shell. If it's two machines, are they running the same Linux / UNIX OS? Can you provide more detail. – EightBitTony Feb 26 at 10:01
    
hi, i already re-edited my question, yes is different machine OS – hades Feb 26 at 10:06
    
i put .sh as file extension and deployed to both machine A and machine B, works for machine A but not machine B – hades Feb 26 at 10:16
    
What is the output from oslevel -r on the AIX box? Recent versions have ksh93. – Jeff Schaller Mar 3 at 2:04

It has nothing to do with the shell, but with the date command. The -d option is specific to the GNU implementation of the date command. On non-GNU systems, that won't work unless you install the GNU version of date as a separate package (that would probably be installed as gdate or as /opt/gnu/bin/date...).

Note that recent versions of ksh93 have a similar feature with their printf builtin command:

printf '%(%Y%m)T\n' '1 month ago'

(see also zsh for another shell with builtin date manipulation support (strftime builtin in the zsh/datetime module)).

Some other date implementations also have features to adjust dates. For instance, with BSD date, you could do:

date -v -1m +%Y%m

I'm not aware that AIX comes with a command that does date calculation and there is no command in the POSIX toolchest, so no standard/portable command for that. You could revert to perl or do the calculation by hand:

eval "$(date +'y=%Y m=%m')"
m=$((${m#0} - 1))
[ "$m" -gt 0 ] || m=12 y=$((y - 1)) # January case
printf '%d%02d\n' "$y" "$m"
share|improve this answer
1  
I've always ended up reverting to Perl on AIX for this very reason. OP could also compile GNU date on AIX if they have the relevant build tools (a challenge in its own right). – EightBitTony Feb 26 at 11:08
    
Just found a post from 2002 on another site suggesting manipulating TZ on AIX before calling date. TZ=GMT24BST,M3.5.0,M10.5.0/02; date to get yesterday's date - seems a little overkill though. I don't have access to an AIX box any more to test if that works, and if it works for large offsets (like a year). – EightBitTony Feb 26 at 11:10
    
Or /opt/freeware/bin ? – DarkHeart Mar 10 at 12:05

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

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