How to date as a user input in shell scripting? - shell

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.

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:
In what format can you pass dates to the date command. I tested that GNU date can accept YYYY-mm-dd format (and probably many others), while BSD needs YYYYmmddHHMM.
In what format can you ask for a difference. With GNU date simply 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.

You can just do:
date --date="3 days ago"
to get get date of 3 days back.

Related

Sends from epoch, for 1 year ago on MacOS / BSD?

I'm trying to calculate the number of seconds since the Epoch, using date on MacOS BSD.
I can get a one year ago date string:
$ date -v -1y
Tue Apr 21 10:44:47 EST 2020
...but I can't figure out how to convert it into seconds since Epoch. Any suggestions?
Add +%s to tell it to print the datetime as seconds since the epoch:
date -v -1y +%s
The + is a date option to set the output format, and %s is strftime format for "seconds since epoch".
Portability note: while the +%s part is pretty standard and portable (though the %s format is not actually required by POSIX), the -v -1y part is wildly nonportable. With GNU date (e.g. on most Linuxes), you'd use something like --date='1 year ago' instead. On NetBSD, -d '1 year ago' works. Check your local man page to see what your system supports.

bash: query timestamp of UTC date on BSD

What I intent to get is
$ xxx 2019-10-11 <= insert your command
1570752000
The output is timestamp in Oct 11 00:00:00 UTC 2019. I find a good way to do this in gnu, but not in bsd
This should work:
date -j -f '%F %T %Z' '2019-10-11 00:00:00 U' '+%s'
-j is for dry-run; i.e it prevents date from changing system date and time,
-f is for specifying input format,
+%s is for converting given date to seconds since Epoch.
On NetBSD the following will work:
TZ=GMT0 date -d '2019-10-11 00:00:00' '+%s'
Note the use of the TZ environment variable to specify the input timezone instead of trying to have it parsed from the input (though it may be possible to have a more properly formatted timezone parsed from the input, though then that leaves the question of what timezone the output should be formatted in).
On MacOS you might try something similar to what Oguz suggested:
TZ=GMT0 date -j -f '%Y-%m-%d %H:%M:%S' '2019-10-11 00:00:00' '+%s'

Yesterday's date variable in BASH on AIX Server

I want to store yesterday's date in BASH variable to search for yesterday's files with that variable in the file-name wildcard search. I am using the following format for New York City, NY, USA (EST) time zone, and wanted to know whether it is guaranteed to fetch yesterday's date from the system date; else I can make further changes to the variable.
yesterday=$(TZ=GMT+28 date +%Y%m%d)
...
for file in $HOME_DIR/*$yesterday*.txt;
...
The text filename in HOME_DIR would be as follows:
"ABC_20171011064612.txt"
update 1: Attempt for removing daylight savings related issues:
yesterday=$(echo -e "$(TZ=GMT+28 date +%Y%m%d)\n$(TZ=GMT+18 date +%Y%m%d)"|grep -v $(date +%Y%m%d)|sort|tail -1)
1) Convert two dates to string, 24 hours and 14 (picked arbitrarily to be less than 24 hours) hours before today's date
2) Filter for dates that are not today's date
3) Sort strings from 2) in ascending order
4) Assign yesterday variable to last tail -1 entry of the list
It may not be always right due to DST, although it will not be a big issue.
You could rather say:
yesterday=$(date -d yesterday +%Y%m%d)
You attempted
yesterday=$(echo -e "$(TZ=GMT+28 date +%Y%m%d)\n$(TZ=GMT+18 date +%Y%m%d)|
grep -v $(date +%Y%m%d)|sort|tail -1)
I think it worked.

UNIX Shell scripting (digit based subtraction)?

I have an input like 2013_07_02 (It may be any date). I want to retrieve date which is 2 days back from the input date. How to do so?
In case you want the output in the same format as the input, i.e. 2013-07-02, substitute _ with - before passing to date:
$ inputdate='2013_07_02';
$ date --date=${mydate//_/-}'-2 day' +'%Y_%m_%d'
2013_06_30
date is your friend:
date -d "2013-01-01 -2 days" +"%Y-%m-%d"
This prints out 2012-12-30.
use date with -d option. It understands relative dates, even with common language like
date -d "two days ago".
In your case something like
date -d "2013-07-01 -2 days"
would be enough.

getting a previous date in bash/unix

I am looking to get previous date in unix / shell script .
I am using the following code
date -d ’1 day ago’ +’%Y/%m/%d’
But I am getting the following error.
date: illegal option -- d
As far as I've read on the inetrnet , it basically means I am using a older version of GNU. Can anyone please help with this.
Further Info
unix> uname -a
SunOS Server 5.10 Generic_147440-19 sun4v sparc SUNW,Sun-Fire-T200
Also The below command gives an error.
unix> date --version
date: illegal option -- version
usage: date [-u] mmddHHMM[[cc]yy][.SS]
date [-u] [+format]
date -a [-]sss[.fff]
try this:
date --date="yesterday" +%Y/%m/%d
dtd="2015-06-19"
yesterday=$( date -d "${dtd} -1 days" +'%Y_%m_%d' )
echo $yesterday;
you can use
date -d "30 days ago" +"%d/%m/%Y"
to get the date from 30 days ago, similarly you can replace 30 with x amount of days
In order to get 1 day back date using date command:
date -v -1d It will give (current date -1) means 1 day before .
date -v +1d
This will give (current date +1) means 1 day after.
Similarly below written code can be used in place of d to find out year,month etc
y-Year,
m-Month
w-Week
d-Day
H-Hour
M-Minute
S-Second
Several solutions suggested here assume GNU coreutils being present on the system. The following should work on Solaris:
TZ=GMT+24 date +’%Y/%m/%d’
SunOS ships with legacy BSD userland tools which often lack the expected modern options. See if you can get the XPG add-on (it's something like /usr/xpg4/bin/date) or install the GNU coreutils package if you can.
In the meantime, you might need to write your own simple date handling script. There are many examples on the net e.g. in Perl. E.g. this one:
vnix$ perl -MPOSIX=strftime -le 'print strftime("%Y%m", localtime(time-86400))'
201304
(Slightly adapted, if you compare to the one behind the link.)
the following script prints the previous date to the targetDate (specified Date or given date)
targetDate=2014-06-30
count=1
startDate=$( echo `date -d "${targetDate} -${count} days" +"%Y-%m-%d"`)
echo $startDate
I have used the following workaround to get to the required solution .
timeA=$(date +%Y%m)
sysD=$(date +%d)
print "Initial sysD $sysD">>LogPrint.log
sysD=$((sysD-1))
print "Final sysD $sysD">>LogPrint.log
finalTime=$timeA$sysD
I hope this is useful for people who are facing the same issue as me.
$ date '+%m/%d/%Y' --- current date
$ TZ=Etc/GMT+24 date '+%m/%d/%Y' -- one dayprevious date
Use time zone appropriately
Problem
You are using backticks, rather than single quotes, for your arguments. You may also not be using GNU date, or a version of date that supports the flag you are using.
Solution
Quote your arguments properly. For example:
$ date -d '1 day ago' +'%Y/%m/%d'
2013/04/14
Try This:
gdate -d "1 day ago" +"%Y/%m/%d"
date -d "yesterday" '+%Y-%m-%d'

Resources