Linux illegal date format - linux

#!/bin/bash
DAYS=${2:-0}
DATE=$(date '+%Y-%m-%d' -d "2016-11-20 +$DAYS days")
I am trying to perform the above action in in my shell script but i keep getting this error:
date: illegal time format
usage: date [-jnu] [-d dst] [-r seconds] [-t west] [-v[+|-]val[ymwdHMS]] ...
[-f fmt date | [[[mm]dd]HH]MM[[cc]yy][.ss]] [+format]
2016-12-14 17:31:31,779
Not sure what is wrong

If you are using a non-GNU date command may be the FreeBSD version on a Mac, use the -j flag.
date -j -v+2d -f "%Y-%m-%d" "2016-11-20" +%Y-%m-%d
2016-11-22
to use a variable, just double-quote it
DAYS=2
date -j -v+"$DAYS"d -f "%Y-%m-%d" "2016-11-20" +%Y-%m-%d
2016-11-22

Related

LC_TIME="en_GB.utf8" not more working since March

On German Linux-system I run script to grep information by date from English document.
LC_TIME="en_GB.utf8" grep -a "\[$(date -d "yesterday" +%b\ %d)" file.log
Since March script don't grep information anymore. Before it worked fine over months.
Manual I see the problem, that not "Mar" but German "Mär" is in search string:
LC_TIME="en_GB.utf8" echo $(date -d "yesterday" +%b\ %d)
Mär 27
Following LC_TIME="en_GB.utf8" is not more working correct.
What can I do?
You are changing LC_TIME for grep and for echo not for date.
echo $(LC_TIME="en_GB.utf8" date -d "yesterday" +%b\ %d)
grep -a "[$(LC_TIME="en_GB.utf8" date -d "yesterday" +%b\ %d)"

How do I get a date of yesterday in expect

I write a expect script 1.sh:
#!/usr/bin/expect
set yest [exec `date -d "yesterday" '+%Y%m%d'`]
send_user $yest
exit 1
And run in linux ,use expect -d ./1.sh
But I get this error:
expect version 5.44.1.15
executing commands from command file ./1.sh
invalid command name "/bin/date"
while executing
"date -d "yesterday" '+%Y%m%d'"
invoked from within
"set yest [date -d "yesterday" '+%Y%m%d']"
So,how do I fix this.I googled somethings and have no idea.
I find clock to instead date:
https://www.tcl.tk/man/tcl8.5/TclCmd/clock.htm
And I get a way to run expect in bash:
#!/bin/bash
expect <<!
## expect code ##
!
Finally use this way to get yesterday string in expect script:
set yest [clock scan "yesterday"]
set yest1 [clock format $yest -format {%Y%m%d}]
Try this
#!/usr/bin/expect
set yest [ exec /bin/date -d "yesterday" +%Y%m%d]
send_user $yest
exit 1
Try out this way:
For yesterday :
date -d '-1 day' '+%Y%d%m'
For day before yesterday :
date -d '-2 day' '+%Y%d%m'

How to convert the date in a http header to an accepted input date for the linux command

I am currently grabbing the date from an http header using this command:
wget --no-cache -S -O /dev/null google.com 2>&1 | sed -n -e 's/ *Date: *//p' -eT -eq
It's output is: Thu, 26 Oct 2017 20:19:57 GMT
I then need to convert this output to an accepted input that the BusyBox date command will accept, i.e.:
date --set="YYYY-MM-DD HH:MM:SS"

How to declare a variable which store pipe command output in Linux shell script

dt=`echo date --date "-15 min"|awk '{print $4}'`;
dts=`echo sar -P ALL -s $dt`;
echo $dts
What is wrong with code?? Here, I want to take 15 min previous sar output but what I get is "sar -P ALL -s min" as output.
Use a subshell rather than backticks:
#!/bin/bash
DT=$(date --date "-15 min" | awk '{print $4}')
DTS=$(sar -P ALL -s $DT)
echo "$DTS"
See: http://tldp.org/LDP/abs/html/subshells.html
try this;
dt=`date --date "-15 min"|awk '{print $4}'`;
dts=`sar -P ALL -s $dt`;
echo $dts
Eg:
user#host:/tmp/test$ ./test.sh
sar -P ALL -s 16:37:44

Compare last modification dates of local and remove files

I can get a last modification date of the remote file using
curl --head http://url 2>/dev/null | grep -Po '(?<=^Last-Modified:\s).*$'
This gets me date/time such as
Wed, 04 Sep 2013 19:53:18 GMT
For local file I can use
find /path/file -exec stat \{} --printf="%y\n" \;
and it gets me date/time such as
2012-01-09 09:50:30.000000000 -0500
How can I compare this dat/time with last modification date of the local file? Please note that time zone may be different for remote and local file.
You can actually use date -d to parse the string, as #fedorqui says. Try running the below commands:
$ date -d "$(<your curl command grepped>)" +%s #+%s gets you timestamp.
$ date -d "$(<your find command>)" +%s
To actually compare, you can subtract the timestamps, something like:
$ echo $(( $(date -d "$(<curl cmd>)" +%s) - $(date -d "$(<find cmd>)" +%s) ))

Resources