SneakyMummin
SneakyMummin

Reputation: 711

Expect script, neither branch of if gets executed

Can anybody take a look and advice why the following snippet does not execute neither branch of the if statement? Script does not throw any errors. It simply reaches the expect line and waits until it times out. arg1 is just an argument from command line.

set arg1 [lindex $argv 0]
set strname "teststring"

expect {
       "prompt#" {
                        if { [string compare $arg1  $strname]  != 0 } {
                            send "strings different\r"
                        } else {
                            send "strings same\r"
                        }
                 }

       default          abort
}

Thanks in advance.

EDIT:

Edited to show the correct structure.

Upvotes: 3

Views: 4976

Answers (2)

glenn jackman
glenn jackman

Reputation: 246774

Expect is an extension of Tcl, and Tcl is very particular about whitespace:

...
# need a space between the end of the condition and the beginning of the true block
if { [string compare $arg1  $strname]  != 0 } {
  ...
# the else keyword needs to be on the same line as the closing bracket of the true block
} else {
  ...
}

Upvotes: 5

toes_314
toes_314

Reputation: 108

Is your complete script more like this?:

#!/usr/bin/expect -d

set arg1 [lindex $argv 0]
set strname teststring

expect {
       "prompt#"
                        if { [string compare $arg1  $strname]  != 0 }{
                            send "strings different\r";
                        }
                        else{
                            send "strings same\r";
                        }

       default          abort
}

Or are you running expect some other way? How exactly are you running expect, your script, with what args?

Upvotes: 1

Related Questions