Search code examples
expectautoexpect

Expect: How does one export the executing environmt to the runtime environment? (where keys are sent)


I have tried the following:

set timeout -1
spawn $env(SHELL)
match_max 100000

send -- "ssh somewhere@addr"
expect "*"
for { set i 0 } { $i < [array size env] } { incr i } {
  send -- "echo env($i) = $env($i)"
  expect "*"
}
send -- "env > foo\r"
expect "*"
send -- "^D"
expect eof

The above fails with the following error:

no such variable
    (read trace on "::env(0)")
    invoked from within
"send -- "echo env($i) = env($i)""
    ("for" body line 2)
    invoked from within
"for { set i 0 } { $i < [array size env] } { incr i } {
    send -- "$::env($i)"
    expect "*"
}"
    (file "./script.exp" line 52)

I'll get around system issues by re-sourcing the rc files; I merely want to transport an unknown collection of vars (relative to the script) set by me to the remote execution environment.

Is this possible, I am unfamiliar with TCL (total beginner)?


Solution

  • Tcl arrays are more like a dictionary of keys and values than a list indexed by number. You can get the list of keys with [array names env] and traverse them with foreach:

    foreach n [array names env] {
      send -- "echo env($n) = $env($n)\r"
      expect "\n"
    }