we are using hp-ux servers
we need to get 2 hours ago datetime value in bash shell script ?
how can i do that any experiences ?
date -d -2hours; date --version
Thu Apr 14 02:38:08 CEST 2011
date (GNU coreutils) 7.4
I'm not a shell script expert, but you may want to check this site at unix.com. They provide this example for subtracting from dates:
# subtract from any date
date --date "$dte 3 days 5 hours 10 sec ago"
date --date "$dte -3 days -5 hours -10 sec"
Related
I am working in Sun OS environment, I want to add a functionality to my existing unix ksh script where it allows to read a date(in %Y%m%d format) from a file and add 1 day and rewrite the same into that file. [please note: not adding day to current date instead i want to add 1 day to i/p date present in a file].
Eg:DateFile.dat
20200620
I want my script to change it to 20200621 at the end of run.
My code as below:
#!/bin/ksh
ip_dte</home/{file_Path}
echo $ip_dte
dte_add=`TZ=AEST-24 "$ip_dte"`
echo $dte_add
Something like that ? :
#!/bin/ksh
# Starting date, YYYYMMDD (yes I should verify the format :) )
FIRST_DATE=$1
[[ -z $FIRST_DATE ]] && FIRST_DATE=$(date +"%Y%m%d")
# 1 day in seconds
PERIOD=86400
# Transform the starting date in second and add 1 day
SECOND_DATE=$(( $(date -d "$FIRST_DATE" +"%s")+$PERIOD ))
# Transform the second date from second to human date format
print "Second date is $(date -d #"$SECOND_DATE" +"%Y%m%d")"
We can let date do the calculations and formatting for us:
$ date1='20200415'
$ date -d "${date1}+1 day" # add 1 day, use default output format
Thu Apr 16 00:00:00 UTC 2020
$date -d "${date1}+1 day" '+%Y%m%d' # add 1 day, change to YYYYMMDD format
20200416
$ date2=$(date -d "${date1}+1 day" '+%Y%m%d') # save new date in variable in YYYYMMDD format
$ echo "${date2}"
20200416
Here's a ksh fiddle of the above.
The other answers will work if you have ‎CSWcoreutils or SUNWgnu-coreutils installed. You may have to run gdate or /usr/gnu/bin/date.
But if you have a recent version of Solaris, ksh will be ksh93, and you can use the %T format in printf:
$ cat ddd
20200620
$ ip_dte=`cat ddd`
$ printf "%(%Y%m%d)T\n" "$ip_dte tomorrow"
20200621
If you have perl with the Time::Local module on the old Solaris server, try this:
#!/bin/ksh
ip_dte=20200531 # or read date from file into ip_dte
echo $ip_dte
timestamp=`perl -MTime::Local=timelocal -e '($y,$m,$d) = $ARGV[0] =~ /(\d\d\d\d)(\d\d)(\d\d)/; $m=$m-1; print timelocal(1,1,1,$d,$m,$y);' $ip_dte`
dte_add=`perl -MPOSIX=strftime -e 'print strftime("%Y%m%d", localtime($ARGV[0] +86400));' $timestamp`
echo $dte_add
Is there any command to get Timestamp as 00:00:00
Basically i need Current Date with Time stamp as 00:00:00 in unix
I tried
$(date '+%Y-%m-%d' 00:00:00)
But i need actual command in unix for it instead of appending 0
If you want a string in the format 2020-02-05 00:00:00 then how about just using echo to print the date output of the date command and then appending the time?
Like
echo "$(date '+%Y-%m-%d') 00:00:00"
how can I configure for 31 days of month . currently I am able to do with 30 days.
/var/log/myLogs/* {
daily
missingok
rotate 30
olddir /prar/oney_logs/tomcat_m_logs/archive
compress
copytruncate
postrotate
ls /prar/oney_logs/tomcat_m_logs/GC_web_*.log |grep -v `date --date 'today' +%y%m%d`| xargs rm -f
find /prar/oney_logs/tomcat_m_logs/archive/GC_web_*.log* -mtime +30 -exec rm {} \;
endscript
}
It's not so easy with logrotate. You can set rotate 31 to keep the max number of days a month can have. Then add a monthly cron job to archive the files per month. By setting 31 as the number of logs to keep, you are sure you have at least a months of files. If the month has less the 31 days, you will have 1 or more daily logs from last month left over.
Otherwise you can:
a) Handle by postrotate script in order to rename / move files for maonths with less than 31 days;
b) Make a crontab script to change logrotate config file each month;
c) Make a config file for each month and set 12 entries in crontab as described here: Logrotate - daily log files for a month;
d) use some different utilities / program than logrotate
This converts the given datetime to epoch time (seconds)
date -d"2015-07-24 11:29:00" +%s
// gives 1437762540
Now I want to do the same thing, but subtract a day. Normally, this is as simple as adding "- 1 day". However, instead of subtracting a day, it actually adds a day.
date -d"2015-07-24 11:29:00 - 1 day" +%s
// gives 1437848940 (notice, this value is great than the one above)
If I take away the time portion from my timestamp it works great. The time portion seems to break it however. I know I can do this in two separate steps and avoid this problem. However, I was hoping to do it in one command. Is this possible?
There is some ambiguity in the date command about how to interpret the - 1 token in your date string. It resolves it as a time zone specification
$ date -d "2015-07-24 11:29:00 -1"
Fri Jul 24 08:29:00 EDT 2015
$ date -d "2015-07-24 11:29:00 UTC-1"
Fri Jul 24 08:29:00 EDT 2015
$ date -d "2015-07-24 11:29:00 - 2"
Fri Jul 24 09:29:00 EDT 2015
$ date -d "2015-07-24 11:29:00 UTC-2"
Fri Jul 24 09:29:00 EDT 2015
$ TZ=UTC date -d "2015-07-24 11:29:00 -1"
Fri Jul 24 12:29:00 UTC 2015
(your results may vary depending on your TZ setting)
The day part is then interpreted to mean add one day
$ date -d "2015-07-24 11:29:00 - 1 day"
Sat Jul 25 12:29:00 EDT 2015
$ date -d "2015-07-24 11:29:00 UTC-1 + 1 day"
Sat Jul 25 12:29:00 EDT 2015
Add a timezone spec to your date string, as #amdixon suggests, to resolve the ambiguity and get the expected results.
For reference, the GNU coreutils info page for "Date input formats": https://www.gnu.org/software/coreutils/manual/html_node/Date-input-formats.html
I don't have a precise answer. I do know that free-form datetime parsing is hard. Playing around with date:
$ date -d"2015-07-24 11:29:00 - 1 day" "+%F %T"
2015-07-25 08:29:00
$ date -d"2015-07-24 - 1 day" "+%F %T"
2015-07-23 00:00:00
$ date -d"11:29:00 - 1 day" "+%F %T"
2015-07-25 08:29:00
$ date -d"2015-07-24 - 1 day 11:29:00" "+%F %T"
2015-07-23 11:29:00
So, subtracting a day from a date seems to work, but subtracting a day from a time is problematic.
I have tried to calculate number of days from January 1st to given date in same year.
Option -d for UNIX command isn't working
date -d
date: illegal option -- d
Usage: date [-u] [+format]
date [-u] [mmddhhmm[[cc]yy]]
date [-a [-]sss.fff]
I'm using this script but is too long.
Is there a simple way to calculate a nuber of days?
EDIT
Result of a script:
$ ksh datecalc -a 2013 2 5 - 2013 1 1
$ 35
OK so this may be a bit far fetched, but mysql client (or other DB clients) can come in handy for this as they have reliable and well documented date functions.
$ mysql ..... --silent -e "select datediff('2013-02-05', '2013-01-01') from dual;"
35
$
where ..... are your connection options.
If you have Perl installed, you can do:
perl -MPOSIX=strftime -le 'print strftime("%j",localtime)'
For a specific day, e.g. Feb 5:
perl -MPOSIX=strftime -le '
#d=(2013,2,5);
print strftime("%j",0,0,0,$d[2],$d[1]-1,$d[0]-1900)
'
036