在expect脚本中写入文件

时间:2015-02-03 10:37:15

标签: tcl expect

tcl函数中的puts命令无法正常工作。基本上这个函数提供了关于验证的反馈(好的或不好的),但问题在于放置。想法是把写入文件“验证确定”或“验证发现错误”,但文件/ tmp / ARNE / bla只有“验证确定”。我有很多验证,所以有些错误有些错误,但“验证发现错误”不在文件中。 如果有人知道可能是什么问题,我会恭喜。

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
        }
}

1 个答案:

答案 0 :(得分:1)

我认为您的代码中缺少}。在您的第一个expect语句中,花括号分组涵盖了两个部分,但我怀疑您打算使用单独的主体有两个单独的expect语句。以下添加了缺少的支撑:

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
    }
}

虽然你可能想要一个带有单独选择器的expect语句。 expect语句的操作类似于switch语句,如:

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
       }
    }
}

也可以正常工作。