1

In the following expect script, you can see that I am trying to match the regex ".*" . That match succeeds, but $expect(0, string) is still not getting set. What am I doing wrong?

First, the script (in a file called /usr/local/bin/py) ...

#!/usr/bin/expect -d

log_user 1
exp_internal 1
match_max 100000

spawn /usr/bin/python3

set prompt       "(>>>|\\.\\.\\.) "
set anythingElse ".+"

expect {
  -re $prompt {
    send_user -- $expect_out(0, string)
    expect_user -re "((>>>|\\.\\.\\.) )?(.*)\n"
    send -- $expect_out(3, string)
    send -- "\n"
    exp_continue
  }
  -re $anythingElse {
    set x $expect_out(0, string)
    send_user -- $x
    exp_continue
  }
  eof {
    exit 0
  }
}

And now, here's the output. Note that the failure is on the "set x ..." line:

expect version 5.45
argv[0] = /usr/bin/expect  argv[1] = -d  argv[2] = /usr/local/bin/py  
set argc 0
set argv0 "/usr/local/bin/py"
set argv ""
executing commands from command file /usr/local/bin/py
spawn /usr/bin/python3
parent: waiting for sync byte
parent: telling child to go ahead
parent: now unsynchronized from child
spawn: returns {1277}
Gate keeper glob pattern for '(>>>|\.\.\.) ' is ''. Not usable, disabling the performance booster.
Gate keeper glob pattern for '.+' is ''. Not usable, disabling the performance booster.

expect: does "" (spawn_id exp6) match regular expression "(>>>|\.\.\.) "? (No Gate, RE only) gate=yes re=no
".+"? (No Gate, RE only) gate=yes re=no
Python 3.4.3 (default, Oct 14 2015, 20:28:29) 
[GCC 4.8.4] on linux
Type "help", "copyright", "credits" or "license" for more information.

expect: does "Python 3.4.3 (default, Oct 14 2015, 20:28:29) \r\n[GCC 4.8.4] on linux\r\nType "help", "copyright", "credits" or "license" for more information.\r\n" (spawn_id exp6) match regular expression "(>>>|\.\.\.) "? (No Gate, RE only) gate=yes re=no
".+"? (No Gate, RE only) gate=yes re=yes
expect: set expect_out(0,string) "Python 3.4.3 (default, Oct 14 2015, 20:28:29) \r\n[GCC 4.8.4] on linux\r\nType "help", "copyright", "credits" or "license" for more information.\r\n"
expect: set expect_out(spawn_id) "exp6"
expect: set expect_out(buffer) "Python 3.4.3 (default, Oct 14 2015, 20:28:29) \r\n[GCC 4.8.4] on linux\r\nType "help", "copyright", "credits" or "license" for more information.\r\n"
can't read "expect_out(0, string)": no such element in array
    while executing
"set x $expect_out(0, string)"
    invoked from within
"expect {
  timeout {
    exp_continue
  }
  -re $prompt {
    send_user -- $expect_out(0, string)
    send_user -- "!!!\r\n"
    expect_user -re "((>>..."
    (file "/usr/local/bin/py" line 14)

Thanks in advance.

1 Answer 1

9

I figured out the answer. There can be no space between the comma and the second argument of expect_out().

In other words, this is wrong: $expect_out(0,[space]string)

... and this is correct: $expect_out(0,string)

3
  • 1
    $expect_out isn't a function. $expect_out(0,string) isn't a function call. 0,string is not two arguments. $expect_out(0,string) is a variable so ``$expect_out(0, string)` is an entirely different variable. Commented Jan 18, 2016 at 16:27
  • 1
    That's because Tcl associative arrays are only one-dimensional, so 0,string and 0, string are two different array keys. Commented Jan 18, 2016 at 17:32
  • Thanks to both of you. I figured that this was due to some sort of Tcl idiosyncracy. Now I see which one.
    – HippoMan
    Commented Jan 18, 2016 at 18:05

Not the answer you're looking for? Browse other questions tagged or ask your own question.