How to handle with problems with different commands on FreeBSD/Linux/macOS

Introduction

TL; DR

Determine the environment by uname

case "$(uname)"; in
Linux)
# GNU coreutils
;;
Darwin)
# macOS
;;
esac

Take advantage of the difference in syntax

if stat --help >/dev/null 2>&1; then
modified_time_fmt="-c%y" # GNU coreutils
else
modified_time_fmt="-f%m" # FreeBSD, macOS
fi
stat $modified_date_fmt /path/to/file # It works!

Substitute with a command that has the same syntax

$ openssl md5 test
MD5(test)= d41d8cd98f00b204e9800998ecf8427e

Conclusion

DevOps Consultant. I’m strongly focused on automation, security, and reliability.