3

I have a small script in Ubuntu I want to use on a Mac. The date function isn't the same so it fails on syntax

Here's the original Ubuntu BASH script;

function pass () {
    pass="$(ldapsearch -Y GSSAPI -Q -H ldap:///dc%3Dant%2Cdc%3Damazon%2Cdc%3Dcom -b DC=ant,DC=amazon,DC=com -s sub cn=$1 | grep -Ew 'pwdLastSet:' | awk '{print $2}')"
    pass_epoch="$(((pass / 10000000) - 11644473600))"
    pass_epoch_exp="$((pass_epoch + 86400*90))"
    today="$(date +%s)"
    countdown_epoch="$(expr $pass_epoch_exp - $today)"
    countdown="$((countdown_epoch / 86400))"
    pwdSet_date="$(date -d @$pass_epoch)"
    expires_date=$(date -d "@$((pass_epoch + 86400*90))")
    echo "pwdLastSet: "$pwdSet_date
    echo "pwdExpires: "$expires_date
    echo "  daysLeft: "$countdown
}

usage: date [-jnRu] [-d dst] [-r seconds] [-t west] [-v[+|-]val[ymwdHMS]] ... 
            [-f fmt date | [[[mm]dd]HH]MM[[cc]yy][.ss]] [+format]
usage: date [-jnRu] [-d dst] [-r seconds] [-t west] [-v[+|-]val[ymwdHMS]] ... 
            [-f fmt date | [[[mm]dd]HH]MM[[cc]yy][.ss]] [+format]
pwdLastSet: 
pwdExpires: 
  daysLeft: 23
0

1 Answer 1

10

MacOs is not the same family of UNIX as Linux, so options for commands can be different.

SOLUTION 1 : man date is your friend.

On MacOS, you need to use a combination of -j and -f .

E444:~ emas$ date -j -f %Y%m%d-%H%M%S 20180101-234852  +%Y/%m/%d\ %H:%M:%S
2018/01/01 23:48:52
E44:~ emas$ date -j -f %Y%m%d-%H%M%S 20180101-234852  +%s
1514868532
E444:~ emas$ date -j -f %s 1514868532
Mon Jan  1 23:48:52 EST 2018

SOLUTION 2 : use brew ( https://brew.sh/ ) to install some GNU/Linux tools.

brew install coreutils will install many basics tools and prefix them with g, so date becomes gdate and is located in your /usr/local/ folder.

0

You must log in to answer this question.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.