How can I assign a variable using $ pect_out in TCL / EXPECT?

If I want to match DEF_23 using the following regular expression:

 expect { -re "DEF_\[0-9]*" set result $expect_out(1,string) } 

why say no such element in array ? How does $expect_out and how can I capture a DEF with a regular expression and assign it to the result variable?

+8
source share
3 answers

You are looking for expect_out(0,string) - the element of array 1,string will be filled if you copied the brackets in your regular expression.

expect manpage documents the use of expect_out in the documentation for the expect command:

When matching a template (either eof or full_buffer), any matching and previously unsurpassed output is stored in the variable expect_out (buffer) . Up to 9 matches, the regexp substrings are stored in the variables expect_out (1, line) through expect_out (9, line) . If the -indices flag is used before the template, the starting and ending indices (in a form suitable for lrange ) of 10 lines are stored in the variables expect_out (X, start) and expect_out (X, end) , where X is a digit that corresponds to the position of the substring in the buffer. 0 refers to strings that match the entire pattern and are generated for globe patterns as well as regular expression patterns.

The manpage has an illustrative example.

+12
source

It seems that the above explication is inaccurate! Check out this example:

 $ cat test.exp #!/usr/bin/expect set timeout 5 log_user 0 spawn bash send "ls -1 db*\r" expect { -re "^db.*$" { set bkpfile $expect_out(0,string) } } send_user "The filename is: $bkpfile\n" close $ ls -1 db* dbupgrade.log $ ./test.exp can't read "bkpfile": no such variable while executing "send_user "The filename is: $bkpfile\n"" (file "./test.exp" line 15) $ 

The test result is the same as when using $ expect_out (1, string) or $ expect_out (buffer). Am I missing something or is this the expected behavior?

+1
source

Aleksandar - it should work if you change the match to "\ ndb. * $".

If you enable exp_internal 1, you will see that the buffer contains something like this: "ls -1 db * \ r \ ndbupgrade.log \ r \ n08: 46: 09"

So the carriage (^) will throw out your pattern.

0
source

Source: https://habr.com/ru/post/918180/


All Articles