【问题标题】:unable to get the $expect_out(buffer) and $expect_out(0,string) values in shell with expect无法使用期望在 shell 中获取 $expect_out(buffer) 和 $expect_out(0,string) 值
【发布时间】:2014-04-21 12:33:02
【问题描述】:

如果我将期望编码到 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)。

【问题讨论】:

    标签: linux bash shell unix expect


    【解决方案1】:

    Shell heredocs 本质上是大的双引号字符串。 shell 替换了 $expect_out 变量(用一个空字符串,因为 shell 会话没有这样的变量)before expect 被启动。您需要单引号引用期望脚本主体以保护期望变量免受 shell 的影响,转义任何$

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

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

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2013-06-21
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多