Question

If i code expect into shell script, I am unable to get the values of $expect_out(buffer) and $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).

Was it helpful?

Solution

Shell heredocs are essentially big double-quoted strings. The shell is substituting the $expect_out variable (with an empty string since the shell session has no such variable) before expect gets launched. You need to single-quote the expect script body to protect the expect variables from the shell, or escape any $:

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

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

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top