10

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.

4

2 回答 2

17

在 Expect 中,您将使用内置clock命令:

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

或者一口气:

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

我强烈建议您使用合理排序的日期格式

set file file.[clock format [clock seconds] -format {%Y-%m-%d}]
于 2013-07-06T14:08:27.663 回答
1

从 Expect 脚本中剪切:

# Set DATE variable in expect script
set DATE [exec date +%F]
send "This is a test with Expect on $DATE - try 4\r"
send ".\r"
于 2014-05-27T19:39:30.550 回答