expectstty

How to control echo of a spawned process in expect(1)?


How to properly set and get echo status of the terminal associated with a spawned process in expect(1)?

stty -echo executed immediately prior to spawn does not seem to affect the spawned terminal. While set stty_init -echo does, I found no equivalent for querying echo status. The only hack I found to work on my machine was to use stty(1) as follows and then parse its output:

spawn sh -c "$main_command && stty -a"

However, I'm sceptical of robustness of that hack.

There's also tty_spawn_id, which I thought I could use as an argument to the stty expect command, but its contents make no sense to me: it's e.g. exp5 and not /dev/ttys5, which I would expect.


Solution

  • After spawn, the pty name is saved to $spawn_out(slave,name). You can then use stty(1):

    $ expect -c 'spawn sleep 1; set pty $spawn_out(slave,name); puts $pty; puts [exec stty -a --file $pty]; expect eof'
    spawn sleep 1
    /dev/pts/10
    speed 38400 baud; rows 57; columns 168; line = 0;
    intr = ^C; quit = ^\; erase = ^?; kill = ^U; eof = ^D; eol = <undef>;
    eol2 = <undef>; swtch = <undef>; start = ^Q; stop = ^S; susp = ^Z; rprnt = ^R;
    werase = ^W; lnext = ^V; discard = ^O; min = 1; time = 0;
    -parenb -parodd -cmspar cs8 -hupcl -cstopb cread -clocal -crtscts
    -ignbrk brkint -ignpar -parmrk -inpck -istrip -inlcr -igncr icrnl ixon -ixoff
    -iuclc -ixany imaxbel -iutf8
    opost -olcuc -ocrnl onlcr -onocr -onlret -ofill -ofdel nl0 cr0 tab0 bs0 vt0 ff0
    isig icanon iexten echo echoe echok -echonl -noflsh -xcase -tostop -echoprt
    echoctl echoke -flusho -extproc