Interview Prep/Shell Scripting/Troubleshooting: tools, portability, permissions and environment

A cleanup script fails on a developer's Mac with `date: illegal option -- d`.

BeginnerTroubleshootingTroubleshooting: tools, portability, permissions and environment
Answer

Likely causes → date -d '7 days ago' is a GNU extension. BSD date uses -v-7d for relative dates and -j -f to parse. readlink -f, stat -c and sed -r have the same GNU-only problem.

Diagnose → date --version 2>/dev/null || echo "BSD date"; uname -s shows Darwin.

Fix → Branch once in a helper, or let find do the date math so no date arithmetic is needed:

days_ago() {   # prints YYYY-MM-DD for N days ago on GNU or BSD date
  if date --version >/dev/null 2>&1; then date -u -d "$1 days ago" +%F; else date -u -v-"$1"d +%F; fi
}
find /var/tmp/app -type f -mtime +7 -delete      # portable, no date parsing at all

Prevent → Keep a small lib/compat.sh for platform differences, and test it on both platforms in CI.

All Shell Scripting questions Roadmap: Shell scripting (Bash)

More on Troubleshooting: tools, portability, permissions and environment