I currently have a shell script that utilizes the "Date" binary - this application is slightly different on OS X (BSD General Commmand) and Linux systems (gnu date). In particular, the version on OS X requires the following to get a date 14 days in the future "date -v+14d -u +%Y-%m-%d" where gnu date requires "date -d "+ 14 days" -u +%Y-%m-%d".
I desire to make my script portable - so I believe that I'll need to utilize a test to determine which version of the date application is installed and thus, what command to run. I've come up with the following two methods, but am looking for advice:
Use "File" tool:
date_binary_full_path=`which date`
date_binary=`file -b $date_binary_full_path`
if [[ $date_binary ~= "Mach-O 64-bit executable x86_64" ]]
then date_command="date -v+14d -u +%Y-%m-%d"
elif [[ $date_binary == "ELF 64-bit LSB executable, x86-64, version 1 (SYSV), dynamically linked (uses shared libs), for GNU/Linux 2.6.18, stripped" ]]
date_command="date -d \"+ 14 days\" -u +%Y-%m-%d"
else
date_command="date -d \"+ 14 days\" -u +%Y-%m-%d"
fi
echo $date_command
Use "date" --version switches:
if [[ `date --version 2>&1 | grep -c "date (GNU coreutils)"` > 1 ]]
then date_command="date -d \"+ 14 days\" -u +%Y-%m-%d"
else
$="date -v+14d -u +%Y-%m-%d"
fi
echo $date_command