How do I get a date of yesterday in expect
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 -d ./1.sh
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:
clock
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:
expect script
set yest [clock scan "yesterday"]
set yest1 [clock format $yest -format %Y%m%d]
2 Answers
2
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'
But I think the point of this error is :
invalid command name "date", the commands date -d "yesterday" '+%Y%m%d' also worked fine in the linux– code.g
Aug 27 at 6:45
invalid command name "date"
date -d "yesterday" '+%Y%m%d'
i m not clear with your question. If your code works fine then y are u getting error?
– SolveProblem
Aug 27 at 6:53
By clicking "Post Your Answer", you acknowledge that you have read our updated terms of service, privacy policy and cookie policy, and that your continued use of the website is subject to these policies.
Possible duplicate of invalid date error in the date command linux
– SolveProblem
Aug 27 at 6:57