如果我将 expect 编码到 shell 脚本中,我将无法获得 $expect_out(buffer) 和 $expect_out(0,string) 的值。
下面是我编写的示例代码。
[Linux Dev:upncommn ~]$ cat expshl.sh
#!/bin/bash
expect << EOF
spawn "/home/upncommn/expectecho.sh"
expect {
"hi" { send_user "You said_hi $expect_out(buffer)\n";
send_user "Sting $expect_out(0,string)\n"
exit 1
}
"*bd*" { send_user "You said $expect_out(buffer)\n"
send_user "Sting $expect_out(0,string)\n"
exit 2
}
timeout { send_user "timeout\n"; exit 3 }
}
EOF
[Linux Dev:upncommn ~]$ cat expectecho.sh
echo "hello"
echo "abduls"
echo "theos"
echo "this is abdul"
[Linux Dev:upncommn ~]$ ./expshl.sh
spawn /home/upncommn/expectecho.sh
hello
abduls
theos
You said (buffer)
Sting (0,string)
[Linux Dev:upncommn ~]$ echo $?
2
请帮助我获取 $expect_out(buffer) 和 $expect_out(0,string)。
最佳答案
Shell heredocs 本质上是大的双引号字符串。 shell 替换 $expect_out
变量(使用空字符串,因为 shell session 没有这样的变量) before expect 启动。您需要对 expect 脚本主体进行单引号以保护 shell 中的 expect 变量,或转义任何 $
:
expect << 'EOF'
# ........^...^
# everything else is the same
http://www.gnu.org/software/bash/manual/bashref.html#Here-Documents
关于linux - 无法使用 expect 在 shell 中获取 $expect_out(buffer) 和 $expect_out(0,string) 值,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/23197518/