How do I use a date in an Expect script?

doon picture doon · Jul 6, 2013 · Viewed 19.2k times · Source

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.

Answer

glenn jackman picture glenn jackman · Jul 6, 2013

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}]