bash - 无法使用期望语句将数据添加到文件 Tcl

标签 bash shell centos tcl

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

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

这是我的名为 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在期望括号之外它可以工作

怎么了 ?

我该怎么做?

最佳答案

此语句的问题:

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变得非常强大!

关于bash - 无法使用期望语句将数据添加到文件 Tcl,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/56357086/

相关文章:

linux - 重定向 stdout, stderr without) grep

linux -/usr/lib/libpthread.so : No such file or directory &&/usr/lib/libm. so: No such file or directory 已解决

docker - 在 OpenVZ VPS 上安装 Docker

linux - Systemd 重新加载进程被杀死

linux - 我可以在 Windows 中用什么程序编写 .sh 文件

bash - 与此批处理等效的 Bash 命令

linux - 从文件 Bash 添加整数

bash - 无法使用 shell 脚本中的标志运行 go build

macos - 将 Shell 脚本转换为 *.app 文件

linux - 是否可以在 sed 中使用匹配的字符串作为 shell date 命令的输入?