如果我希望将代码编入shell脚本,我将无法获得$ expect_out(缓冲区)和$ 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(缓冲区)和$ expect_out(0,字符串)。
答案 0 :(得分:0)
Shell heredocs本质上是一个很大的双引号字符串。在 expect启动之前, shell 替换$expect_out
变量(由于shell会话没有这样的变量,所以使用空字符串)。您需要单引用expect脚本体来保护shell中的expect变量:
expect << 'EOF'
# ........^...^
# everything else is the same
http://www.gnu.org/software/bash/manual/bashref.html#Here-Documents