12

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.

Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
doon
  • 2,311
  • 7
  • 31
  • 52

2 Answers2

17

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}]
Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
glenn jackman
  • 238,783
  • 38
  • 220
  • 352
2

Cut from an Expect script:

# 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"
Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131