1834800 Members
2609 Online
110070 Solutions
New Discussion

Expect If Statement

 
Lee Harris_5
Valued Contributor

Expect If Statement

Hello,

I can't for the life of me remember where, but I've seen a script in the past that used expect, but also had an if statement within the expect code. Can anyone tell me the syntax of if statements in expect?

I want to kind of do something like this...

Send a command, and get expect to do one thing if it gets one string back but do something else if it gets another string back. So for example, if I ran a command, but the response was "No such file or directory" I want the script to do a certain action, but if the command succeeds and expect gets back something else I want the script to do a different action...does that make sense?

Regards - Lee
2 REPLIES 2
rajatnaik
Occasional Contributor

Re: Expect If Statement

check whether this will help

#executes the command
send "$cmd\r"

#looks for pattern for the result
expect -re "\[#%$].*" {

#$expect_out(buffer) expect variable that stores the output of the command. split this by \r\n to get the result

set dataBuff [split $expect_out(buffer) "\r\n"]
set result [lindex $dataBuff 2]

}
#syntax for if
if {$result == "something"} {

} else {

}
Peter Godron
Honored Contributor

Re: Expect If Statement