简体   繁体   English

无法通过expect获得shell中的$expect_out(buffer)和$expect_out(0,string)值

[英]unable to get the $expect_out(buffer) and $expect_out(0,string) values in shell with expect

If i code expect into shell script, I am unable to get the values of $expect_out(buffer) and $expect_out(0,string).如果我将expect 编码到shell 脚本中,我将无法获得$expect_out(buffer) 和$expect_out(0,string) 的值。

Below are the sample codes that i coded.以下是我编码的示例代码。

 [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

Please help me to get the $expect_out(buffer) and $expect_out(0,string).请帮我获取 $expect_out(buffer) 和 $expect_out(0,string)。

Shell heredocs are essentially big double-quoted strings. Shell heredocs 本质上是大的双引号字符串。 The shell is substituting the $expect_out variable (with an empty string since the shell session has no such variable) before expect gets launched.在启动expect之前shell正在替换$expect_out变量(由于shell 会话没有这样的变量,所以使用空字符串)。 You need to single-quote the expect script body to protect the expect variables from the shell, or escape any $ :您需要单引号 expect 脚本主体以保护 shell 中的 expect 变量,转义任何$

expect << 'EOF'
# ........^...^
# everything else is the same

http://www.gnu.org/software/bash/manual/bashref.html#Here-Documents http://www.gnu.org/software/bash/manual/bashref.html#Here-Documents

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM