Reputation: 49
I am new to shell scripting.. I want a script to get any date as a input from user and print date of 3 days back?
example: If user enters date as 2013-01-01 then output should be
2012-12-29.
Upvotes: 1
Views: 2060
Reputation: 124724
If you have GNU date, then this will work:
user_date=2013-01-01
date +%Y-%m-%d -d "$user_date - 3 days"
With BSD date, you'd have to do like this:
user_date=2013-01-01
date -j -v -3d +%Y-%m-%d -d "${user_date//-}0000"
because BSD date needs date to be in the format YYYYmmddHHMM
.
I don't have a Solaris now to test there. If you're in Solaris then hopefully there is gdate
, and you can use the first option, just replace the date
command with gdate
.
Whichever OS you are in, there are two important points:
date
command. I tested that GNU date can accept YYYY-mm-dd
format (and probably many others), while BSD needs YYYYmmddHHMM
.DATE - 3 days
works, with BSD date it's trickier with -j -v -3d
flags.man date
of your system should help you get through these hurdles. In the worst case, you could do all the date
operations you need in perl
or similar.
Upvotes: 3
Reputation: 785671
You can just do:
date --date="3 days ago"
to get get date of 3 days back.
Upvotes: 1