【问题标题】:Not able to add data to a file Tcl using expect statement无法使用期望语句将数据添加到文件 Tcl
【发布时间】:2019-05-29 20:33:54
【问题描述】:

我正在尝试创建一个脚本并运行一个程序,当这个程序运行时它说 DB 已经在运行或端口已经被另一个进程使用(意味着已经在运行)

如果它已经在运行,我希望创建一个 if-else 语句,如果已经运行但如果没有运行,则希望将 1 添加到文件中

这是我的名为 test 的 tcl 文件

    #!/usr/bin/expect -f
    set f [open a2.txt w]
    cd /opt/Uni-SON/
    spawn ./Aprogram start 
    expect {
       "Port is already being used by another process"

        puts $f 1
        puts "sadsadasdsaa"
    }

    puts $f 0

这是我的 test2 文件,它是 bash 文件

#!/bin/bash
./test
value=$(<a2.txt)
rm -rf a2.txt
if [ $value -eq 0 ]
then
        echo Not being used
else
        echo Already Being used
fi

错误:它没有向文件中添加数据,但如果我将完全相同的语句 puts $f 1放在期望括号之外它可以工作

怎么了?

我该怎么做?

【问题讨论】:

    标签: bash shell centos tcl


    【解决方案1】:

    这句话的问题:

    expect {
       "Port is already being used by another process"
    
        puts $f 1
        puts "sadsadasdsaa"
    }
    

    是它触发了你有一系列事情要等待的情况。您可能希望将其更改为:

    expect {
        "Port is already being used by another process" {
            puts $f 1
            puts "sadsadasdsaa"
        }
    }
    

    也就是说,当找到该消息时,执行与之关联的小脚本(两个puts 调用)。您也可以同时等待几件事情,这就是expect 变得非常强大的地方!

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2020-06-05
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2010-12-05
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多