Am using a script in ksh to get a date 91 days prior to today using datecalc on a Solaris 10 server. What would be the equivalent to this in Linux ?

month=`datecalc -a $(date +"%Y %m %d") - 1 |awk {'print $2'}`
day=`datecalc -a $(date +"%Y %m %d") - 91 |awk {'print $3'}`
year=`datecalc -a $(date +"%Y %m %d") - 1 |awk {'print $1'}`
  • Isn't datecalc a ksh script? Save yourself trouble and install it on the Linux machine. – Gilles 'SO- stop being evil' Oct 20 '15 at 21:15

You can use date like

date -d "91 days ago" +"%Y %m %d"

Though if you're just feeding it to awk to get specific fields after, you could just print the part you want like

month=$(date -d "91 days ago" +"%m")
day=$(date -d "91 days ago" +"%d")
year=$(date -d "91 days ago" +"%Y")

GNU date command to the rescue.

$ date
Tue Oct 20 17:27:51 BST 2015
$ date --date='91 days ago'
Tue Jul 21 17:27:52 BST 2015

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.