user3319356
user3319356

Reputation: 173

puts writing to file in expect script

my puts command in tcl function is not working. Bascily this function is giving feedback about validation(good, or not), but the problem is with puts. Idea is that puts write to file "Validation ok" or "Validation found error", but file /tmp/ARNE/bla has only "Validation ok". I have lots of validation, so some of them have error an some not, but the "Validation found error" is not in the file. If anybody knows what could be the problem, I would appriciate.

proc functionm {} {
        set path /tmp/ARNE/bla
        set var [open $path w]
        expect {
                "There were 0 errors reported during validation" {
                 puts $var "Validation succeeded\r"

               }
        expect -re {There were [0-9] errors reported during validation} {
                  puts "Found $expect_out(0,string) errors in validation"
                  puts $var "Validation found error\r"
                  send "exit\r"
                  expect -re {janko.*}
                  #after 100
                  close
                  exit
        }
}

Upvotes: 0

Views: 2522

Answers (1)

patthoyts
patthoyts

Reputation: 33193

I think you have a missing } in your code. In your first expect statement, the curly-braced grouping covers both sections but I suspect you meant to have two separate expect statements with separate bodies. The following adds the missing bracing:

proc functionm {} {
    set path /tmp/ARNE/bla
    set var [open $path w]
    expect "There were 0 errors reported during validation" {
        puts $var "Validation succeeded\r"
    }
    expect -re {There were [0-9] errors reported during validation} {
        puts "Found $expect_out(0,string) errors in validation"
        puts $var "Validation found error\r"
        send "exit\r"
        expect -re {janko.*}
        #after 100
        close
        exit
    }
}

Although you might have wanted one expect statement with separate selectors. The expect statement operates like a switch statement so something like:

proc functionm {} {
    set path /tmp/ARNE/bla
    set var [open $path w]
    expect {
       "There were 0 errors reported during validation" {
          puts $var "Validation succeeded\r"
       }
       -re {There were [0-9] errors reported during validation} {
           puts "Found $expect_out(0,string) errors in validation"
           puts $var "Validation found error\r"
           send "exit\r"
           expect -re {janko.*}
           #after 100
           close
           exit
       }
    }
}

may also work ok.

Upvotes: 1

Related Questions