سؤال

I have a shell script to calculate the number of days from a particular date:

echo D$((($(date +%s)-$(date +%s --date "2018-01-01"))/(3600*24)))

On my 2019 MBP (Intel), it works. On my 2020 Mac mini (M1), it doesn't.

The error:

date: illegal time format
usage: date [-jnRu] [-d dst] [-r seconds] [-t west] [-v[+|-]val[ymwdHMS]] ...
            [-f fmt date | [[[mm]dd]HH]MM[[cc]yy][.ss]] [+format]
/usr/local/bin/days: line 1: (1614815225-)/(3600*24): syntax error: operand expected (error token is ")/(3600*24)")

The man pages are different. On the MBP it's

DATE(1) User Commands DATE(1)

On the Mac mini it's

DATE(1)  BSD General Commands Manual  DATE(1)
  1. What's going on here?
  2. How do I get back the old functionality?
هل كانت مفيدة؟

المحلول

The answer (thanks to a hint by @mmmmmm in the comments) is that in the M1 Mac mini you have to install the gnu coreutils (e.g. via Homebrew), but since the names of some of them overlap with the names of similar (but not identical!) utilities that come with the Mac mini, the system automatically prefixes "g" to any utilities whose names overlap.

Therefore, the problem was solved in my case by changing date to gdate in my code.

مرخصة بموجب: CC-BY-SA مع الإسناد
لا تنتمي إلى apple.stackexchange
scroll top