Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How do I use a date in an Expect script?

Tags:

expect

I need to have access to the current date (or time) within the Expect script so that I can add it to the directories that are created within the Expect script, e.g. something similar to these needs to get done:

mkdir file<date>

I can get the date via the shell as:

date | tr " " "-" | cut -f 2,4 -d "-"

However, I cannot get access to it in Expect, e.g. I cannot do something like:

set var = `date | tr " " "-" | cut -f 2,4 -d "-"`

I put this in a shell script, echo it and get the output in $expect_out(buffer) as detailed here. However, the buffer also gets the prompt which needs to be removed as mentioned Also, note that $expect_out(buffer) doesn't really hold what people want; it typically needs to be filtered down at least to eliminate the prompt.

like image 458
doon Avatar asked Jul 06 '13 06:07

doon


Video Answer


1 Answers

In Expect, you would use the builtin clock command:

set now [clock seconds]
set date [clock format $now -format {%b-%d}]
set file file.$date

Or in one go:

set file file.[clock format [clock seconds] -format {%b-%d}]

I would strongly encourage you to use a date format that sorts sensibly

set file file.[clock format [clock seconds] -format {%Y-%m-%d}]
like image 62
glenn jackman Avatar answered Oct 09 '22 15:10

glenn jackman