linux - 无法使用 expect 在 shell 中获取 $expect_out(buffer) 和 $expect_out(0,string) 值

标签 linux bash shell unix expect

如果我将 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/

相关文章:

ruby-on-rails - 为什么我的 Cron 作业不能正常工作?

linux - 为 node.js 安装 NPM 时出错

linux - 使用 awk 在 Shell 中过滤掉 [..] 中的子字符串列

bash - EOFD 是什么意思?

shell - 自动化期间在Expect Shell脚本中无法建立主机的真实性时如何忽略或传递 'Yes'

linux - 计时器检查文件末尾 bash shell

linux - 缓冲区溢出不运行

c++ - 如何解决对自己类的构造函数和析构函数的 undefined reference 的问题?

linux - linux下如何仅通过 "R"获取进程状态?

bash - 如何在 bash 命令前添加自动前缀 ([prefix] docker exec)