command line on esxi cant get date X day ago [duplicate] - date

This question already has answers here:
Get date of last saturday - BusyBox 1.1.0
(2 answers)
Closed 5 years ago.
I'm currently working on an ESXI and want to get the date X day ago
I have this command line working on other linux :
now=`date +"%Y/%m/%d"`
earlier=`date -d "$now -15 days" +%d/%m/%Y`
but when i try to use it on my esxi server the line :
earlier=`date -d "$now -15 days" +%d/%m/%Y`
is not working, i get the error
date: invalid date '2018/01/30 -4 days'
So I tried to write the date in differents way like american format but still have the error.
the esxi version is 6.0.0
I have searched on internet but i didn't find anything.
Can someone know what is the problem ?
Thank you.
edit: for those having the same problem i got the solution in the comments below

It turns out this is busybox date. Busybox date is very limited in its abilities, but fortunately it supports (undocumentedly) the #seconds syntax:
date -d "#$(( $(busybox date +%s) - 86400 * 7 ))" +%d/%m/%Y
This of course requires that you have a modern version of bash. If this command doesn't work, try typing "bash" and if that works try typing the command again.
If you don't have modern bash...then do you have awk or bc installed? Python? Perl? Basically the tactical goal is to get date to spit out seconds, subtract 7 days worth of seconds, and then feed the output into date again to convert format.
Old
Try this command line. It avoids the $now reference which might confuse some versions of date.
date -d "15 days ago" +%d/%m/%Y
If this doesn't work, can you tell us which version of date you are using? date --version and/or date --help should provide the necessary information.

Related

Crontab using yesterday date

I'm using old version of Unix and i want to run script everyday using yesterday date
My script lets say
30 08 * * * script.ksh dd mm yy
How to make it
You can do:
script.ksh $(date -d yesterday "+\%d \%m \%y")
% is quoted because that is a new-line symbol in a crontab.

Format string on Linux and Solaris

I have a Korn Shell script, and one part of it is that it takes a given date in YYYYMMDD format and outputs it in YYYY/MM/DD format. At first I tried
typeset displaystart=`date --date="${gbegdate}" '+%Y/%m/%d'`
which works fine on Linux, but Solaris's date doesn't have a --date option. I then tried
typeset displaystart=`echo ${gbegdate:0:4}`/`echo ${gbegdate:4:2}`/`echo ${gbegdate:6:2}`
which also works on Linux, but on Solaris it just outputs //.
How can I format this date string in a way that works on Linux and Solaris?
The ${variable:start:length} extension to POSIX shell syntax was introduced in the version of ksh released in 1993, precisely named ksh93, and was also introduced in bash 1.13 the very same year.
The Advanced bash scripting guide from the Linux Documentation Project states:
Variable expansion / Substring replacement
These constructs have been adopted from ksh.
${var:pos}
Variable var expanded, starting from offset pos.
${var:pos:len}
Expansion to a max of len characters of variable var,
from offset pos. See Example A-13 for an example of the creative use
of this operator.
The issue is that on Solaris 10 and older, /bin/ksh is providing a previous ksh standard, ksh88, which didn't implemented this feature.
On the other hand, on Linux, ksh is often ksh93 which supports substring extraction. That explains why your script works under Linux ksh (if you really tested it on ksh.)
An old derivative of ksh93 is available on Solaris 10 though. It is named dtksh ans is located in /usr/dt/bin/dtksh. Your command should work unchanged with it however I wouldn't recommend to fully switch to dtksh, this shell being phased out from Solaris but you might still use it from a regular ksh script to workaround your issue:
typeset displaystart=$(/usr/dt/bin/dtksh -c "gbedate=$gbedate; echo \${gbegdate:0:4}/\${gbegdate:4:2}/\${gbegdate:6:2}")
Note that Solaris 11 and newer provide both GNU date and ksh93 so you wouldn't have that issue in the first place.
Korn shell doesn't have ${variable:start:length} syntax; this is a bash extension to POSIX shell syntax.
You can use echo "$variable" | cut -cstart-end instead.
typeset displaystart=`echo $gbegdate | cut -c1-4`/`echo $gbegdate | cut -c5-6`/`echo $gbegdate | cut -c7-8`
Or maybe you could change your script to use bash instead of ksh.

How to convert any date to YYYYMMDDHHMMSS using unix shell script?

How to convert any date to YYYYMMDDHHMMSS using unix shell script?
It is not specific date it may any date. how to convert it?
example date format is: 20110101050603
Thanks
Puspa
DATE = "15 Jun 2015 10:10:10"; date -d"$DATE" +%Y%m%d%H%M%S
Output :-
20150615101010
More on Dates
Note this only works on GNU date.
I have read that:
Solaris version of date, which is unable to support -d can be resolve with replacing sunfreeware.com version of date.
Update:-
Use ls command to find all files and use grep command for all files.
ls | grep "[0-9]{14}"
grep command also accepts regular expression.
But it won't validate your file accurate. For example, if a file name is 20151835101010, then it will validate this file. But actually month and date both are wrong.
So my suggestion is to write a shell script to identify valid file name and find oldest of them by processing one by one.

find 30 minutes ago modified file in Solaris

i want to find 30 minutes ago modified file in solaris. i have written below command to find 1 day ago modified file.
find . -mtime 1 -exec ls -l {} \;
please help me to find out 30mins ago modified file. My server is solaris.
I found the similar question and answer on unix.stackexchange.com.
https://unix.stackexchange.com/questions/72861/delete-n-minutes-old-file-in-solaris
As Solaris' find command has no -mmin option, the article suggests to use the absolute time.
And there are some other solution helped by the usage of script language like perl.
http://www.unix.com/shell-programming-and-scripting/69234-how-delete-files-30-min-older.html
Hope it helps.
You can use "gfind" on later versions of Solaris. It's GNU find and does have the "-mmin" option.

How to simulate "set -o vi" in csh , like we do in ksh?

I used ksh a lot but now moving to csh as this company has all scripts etc written in that.
One thing I loved about ksh was the ease of using the command history.
In case I have to edit the last command or second last command, I could easily press "Escape - k" to cycle through the commands and easily edit them.
csh does not seem to have a good equivalent. It displays all the history of commands then I have to copy paste one of them and then edit.
That's a pain when you want just change the number of in the file name for example :
cat abcdef1 | grep "Linking"
You could use (as a workaround):
"The most commonly used feature of csh command history is "!!" (pronounced "bang bang") which recalls the previous command":
% date
Mon Nov 25 13:52:36 PST 1996
% !!
date
Mon Nov 25 13:52:52 PST 1996
Here some other useful commands.
But you could just work in ksh and make script and whatever in csh.
If it is istalled simply launch:
/bin/ksh