It is recommended to use feature-based defection rather than the os-based or platform-based detection.
Following sample code show how it works:
if is-compatible-date-v; then
date -v -1d
elif is-compatible-date-d; then
date --date="1 day ago"
fi
function is-compatible () {
"$@" >/dev/null 2>&1
}
function is-compatible-date-v () {
is-compatible date -v +1S
}
function is-compatible-date-d () {
is-compatible date -d '1 second'
}
I was having the exact need to writing some shellcode to deal with date
, and hoping the code be able to run on both BSD and GNU version of date
.
I end up with a set of scripts that provides a uniform interface for both BSD and GNU version of date
.
Example:
Follow command will output a date that is 21 days later than 2008-10-10
, and it works with both BSD and GNU version of date
.
$ xsh x/date/adjust +21d 2008-10-10
2008-10-31
The scripts can be found at:
It's a part of a library called xsh-lib/core
. To use them you need both repos xsh
and xsh-lib/core
, I list them below:
Hope this will help people with the same need.