Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How can I detect BSD vs. GNU version of date in shell script

Tags:

I am writing a shell script that needs to do some date string manipulation. The script should work across as many *nix variants as possible, so I need to handle situations where the machine might have the BSD or the GNU version of date.

What would be the most elegant way to test for the OS type, so I can send the correct date flags?

EDIT: To clarify, my goal is to use date's relative date calculation tools which seem distinct in BSD and GNU.

BSD example

date -v -1d 

GNU example

date --date="1 day ago" 
like image 479
bryan kennedy Avatar asked Jan 05 '12 18:01

bryan kennedy


1 Answers

You want to detect what version of the date command you're using, not necessarily the OS version.

The GNU Coreutils date command accepts the --version option; other versions do not:

if date --version >/dev/null 2>&1 ; then     echo Using GNU date else     echo Not using GNU date fi 

But as William Pursell suggests, if at all possible you should just use functionality common to both.

(I think the options available for GNU date are pretty much a superset of those available for the BSD version; if that's the case, then code that assumes the BSD version should work with the GNU version.)

like image 121
Keith Thompson Avatar answered Oct 05 '22 16:10

Keith Thompson