My script has one command : date -d -30days . The script when run on Linux machine , runs as expected . However, when run on AIX machine , gives error "illegal option -- d". So, what code/command should be written which will be universal , irrespective of underlying operating system on which the script is run. I need to basically subtract 30 days from current date and use the manipulated date.
Asked
Active
Viewed 814 times
-1
-
You can use Perl: http://stackoverflow.com/a/16028365/1983854 – fedorqui Oct 08 '14 at 11:00
-
Well , not well-versed with perl ,need to use bash scripting due to environment restrictions :( – Hitesh Jain Oct 08 '14 at 11:06
-
possible duplicate of [Date manipulation in AIX shell script](http://stackoverflow.com/questions/9060519/date-manipulation-in-aix-shell-script) – Etan Reisner Oct 08 '14 at 13:15
-
In addition to that possible duplicate [this answer](http://stackoverflow.com/a/21283578/258523) looks like it might be useful. – Etan Reisner Oct 08 '14 at 13:16
-
1I don't think you will find one command that will be "universal, irrespective of underlying operating system". If you can narrow down the field a bit (i.e. only AIX and Solaris and Linux, but not Windows, DOS, Mac, Multics, Plan 9, ....), you might have better luck... – twalberg Oct 08 '14 at 17:25
1 Answers
0
"bash scripting" means that you call non-builtin utilities, like date
and yes perl
.
Since the date command on AIX cannot do what you want it to do, you need to be open to alternatives.
past_date=$(
perl -MTime::Local -MPOSIX=strftime -le '
$now = timelocal(0,0,12,(localtime)[3,4,5]);
$ago = $now - 30 * 86400;
print strftime("%Y-%m-%d %T %Z",localtime($ago))
'
)
Adjust your strftime format to suit your needs.
I'm assuming your perl distribution is not up-to-date. I would do this instead:
perl -MDateTime -E 'say DateTime->now->set_time_zone("local")->add(days=>-30)->strftime("%F %T %Z")'

glenn jackman
- 238,783
- 38
- 220
- 352