gpt4 book ai didi

Linux shell : my `expect` script doesn't work as expected

转载 作者:太空宇宙 更新时间:2023-11-04 04:44:37 28 4
gpt4 key购买 nike

我有一个如下所示的简单脚本,从命令行读取 2 个数字并将它们加在一起:

$cat runexp.sh
#!/bin/bash
echo "read 1st number"
read n1
echo "read 2nd number"
read n2
expr $n1 + $n2

可以运行,没问题。然后我编写了一个预期脚本,如下所示:

$cat autorun.sh
#!/usr/bin/expect
spawn ./runexp.sh
expect 'read 1st number' {send "1"}
expect 'read 2nd number' {send "2"}
interact

好像还是提示从命令行读取,等了好久才结束。

$./autorun.sh
spawn ./runexp.sh
read 1st number
5
4
3
5
4
3
read 2nd number
9

我哪里出错了?谢谢。

最佳答案

您还必须发送换行符,否则它只会等待(至少直到默认超时,我认为是十秒)。

此外,expect 不喜欢用单引号来括住字符串。与 Tcl(它的来源)一样,它需要双引号或大括号。这工作得很好:

#!/usr/bin/expect

spawn ./runexp.sh

expect "read 1st number" {send "1\n"}
expect {read 2nd number} {send "2\n"}

interact

关于Linux shell : my `expect` script doesn't work as expected,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/44319579/

28 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com