期待 运行 shell 命令的脚本

Expect script to run shell command

如何在 expect 脚本中 运行 shell 命令并检查特定字符串?

#!/usr/bin/expect

set timeout 20

send "ps -aef | grep P1"

expect "string"

Blah 
Blah

exit;

我尝试用 spawnexecsystem 命令代替 send,但它总是超时或以某些错误结束。

模式是你spawn程序,expect它产生一些输出,send它一些输入,(必要时重复最后两个),close; wait 完成。如果程序没有产生预期的输出,您将等到它完成或超时。

幸运的是,您可以一次等待多个事情:

spawn ps -aef
expect {
    "P1" { ... got it ... }
    eof { ... not got it ... }
    timeout { ... ps hung? ... }
}
close
wait