Unix Date command not working for few servers

1

I am trying to execute date command in unix server for yesterday. The commands tried are :

date --date="1 day ago"
date --date="1 days ago"  
date --date="yesterday"
date --date="-1 day"

These command work in a server but the same command does not work in few other servers, where date prints properly the current date. Could anyone suggest what could be the issue with the other servers? I get an error like : illegal option -- date=1 day ago

The server Details: SunOS wupsa02a0014 5.10 Generic_147440-15 sun4u sparc SUNW,SPARC-Enterprise

Swagatika

Posted 2013-03-27T07:10:31.323

Reputation: 113

1Two suggestions: (1) ask on a site where the question isn't off-topic; (2) include uname -a for the servers where this isn't working. – NPE – 2013-03-27T07:12:06.133

That's because --date="1 day ago" isn't portable. – None – 2013-03-27T07:12:22.680

@NPE Thanks for suggestion. Could you please give an example how can I add the uname -a ? – None – 2013-03-27T07:13:38.757

1He isn't asking you to use uname -a to solve your problem, he wants to know which version(s) you are running on the machines giving you the issue. – Randy Howard – 2013-03-27T07:14:29.527

SunOS wupsa02a0014 5.10 Generic_147440-15 sun4u sparc SUNW,SPARC-Enterprise – None – 2013-03-27T07:15:50.523

You (or someone who uses date --date) suffers from the all the world's a GNU/Linux system. Learn what are POSIX options and what are GNUisms, Linuxisms, bashisms. Then you can write portable scripts working on Solaris as well as Linux and many other OSs. Often the manual page tells you which options are standard and which are not. – Jens – 2013-03-27T09:03:43.260

Answers

5

--date is not a conventional date option on many unix systems.

If you have a wide variety of UNIX/linux-like platforms to work with, try to examine the man page for date (1) on each of them (if man pages are installed) and see what date options are common to all of them as a starting point.

You might find date -v (adjust) reasonably portable.

Something like date -v-1d may work. Test it on each system to see if it works on all of them. It displays the current data value adjusted one day in the past.

Randy Howard

Posted 2013-03-27T07:10:31.323

Reputation: 165

Thanks. Then what are the other ways I can get the date/yesterday ? – None – 2013-03-27T07:14:17.870

subtract a day from the current date? :-) Edited above. – Randy Howard – 2013-03-27T07:14:50.420

If you could provide an example would be helpful. – None – 2013-03-27T07:18:15.357

I'm curious if the above works on the SunOS you reference in the comments above. – Randy Howard – 2013-03-27T07:23:17.290

The above mentioned all the ways works in this server : Linux wnl-svr202b 2.6.32-220.7.1.el6.x86_64 #1 SMP Fri Feb 10 15:22:22 EST 2012 x86_64 x86_64 x86_64 GNU/Linux – None – 2013-03-27T07:27:31.403

Well, if you find that it solved your problem, I'd appreciate you accepting it. If you still have an issue though, tell us what the remaining problem is. – Randy Howard – 2013-03-27T07:31:57.077

1

You can simply subtract 24 hours from the TZ environment variable prior to calling date.

$ TZ=GMT date
Wed Mar 27 09:11:02 GMT 2013
$ TZ=GMT+24 date
Tue Mar 26 09:11:05 GMT 2013

Works on both Solaris and Linux.

Jens

Posted 2013-03-27T07:10:31.323

Reputation: 507