How to check if a variable is set in expect script?
Asked Answered
T

1

9

I am doing something like this:

#!/usr/bin/expect -f

if {$out != ""} {
  send_user $out
}

But it doesn't work. Error message:

can't read "out": no such variable
    while executing
"if {$out != ""} {
send_user $out
}"
    (file "./test" line 3)
Taffrail answered 15/1, 2016 at 14:7 Comment(0)
H
19

The error you got is because of non-existence of the variable out.

To check variable's existence, use the following

if {[info exists out]} {
    puts "variable does exist"
}

info exists returns 1 if variable exist, else 0.

If variable exists, then you can use the code what you posted.

Hemimorphic answered 15/1, 2016 at 14:29 Comment(2)
I think you mean "variable does exist" rather than "variable does not exist" :-)Lessee
For future readers, you may want to import variable from shell. So you have to use this too: set out $env(out)Panay

© 2022 - 2024 — McMap. All rights reserved.