Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Expect - Expecting while interacting

Tags:

expect

I want to "listen" for a string outputted by the shell, while being in "interact" mode. Or i want to emulate interact mode somehow, that still allows me to listen for a specific string from the shell.

It seems interact only listens to the users input (the keys I press) and not what is returned by the shell.

How would I go about having Expect executing something everytime it sees a specific string, but otherwise let me use the shell interactively an unhindered?.

Example:

proc yay {} {
        send_user "Yay\n"
}

trap { # trap sigwinch and pass it to the child we spawned
  set rows [stty rows]
  set cols [stty columns]
  stty rows $rows columns $cols < $spawn_out(slave,name)
} WINCH

spawn bash

interact {
    interact -re "HOT" {
         yay
    }

    expect {
        fuzz yay
   }

}

If i run this and type "HOT" it responds with "Yay". As expected, it read my keys. But if i type

echo fuzz

The "expect" clause doesnt get triggered. Also "echo HOT" wont trigger anything either.

So is this possible or am I missing somthing. Perhaps I'd need to emulate interact in some kind of "expect, continue"-loop. Its just important that everything works normally in the shell..

Suggestions anyone?

like image 913
thelogix Avatar asked Jul 04 '14 13:07

thelogix


People also ask

What is Interact in Expect?

Interact is an Expect command which gives control of the current process to the user, so that keystrokes are sent to the current process, and the stdout and stderr of the current process are returned.

What is Tcl expect?

Expect is a Tcl extension designed for scripting applications. It's similar to the scripting languages used in telecommunications packages, but much more general. By default, Expect is built on plain (not Extended) Tcl. In a sense, Expect shouldn't be necessary.


1 Answers

You can use the expect_background command. From the man page:

takes the same arguments as expect, however it returns immediately. Patterns are tested whenever new input arrives.

You can modify your initial script like this:

#!/usr/bin/expect

proc yay {} {
 send_user "Yay\n"
}

trap { # trap sigwinch and pass it to the child we spawned
  set rows [stty rows]
  set cols [stty columns]
  stty rows $rows columns $cols < $spawn_out(slave,name)
} WINCH

spawn bash

expect_background {
 fuzz yay
}
interact -re "HOT" {
  yay
}
like image 144
Ortomala Lokni Avatar answered Sep 20 '22 16:09

Ortomala Lokni