Linux.com

Regular Expressions

Posted by: Anonymous Coward on August 07, 2006 11:29 AM
You might look at the password example and say 'So what, I can do that in Perl too.' One of the things I like best about Expect is that you can use regular expressions. Using a '-re' Expect will evaluate as a regular expression instead. The next is that the Expect statement can handle different output from the command. Take another look at the password example:
<tt>set password [lindex $argv 1]
spawn passwd [lindex $argv 0]
expect {
    -re "\[N|n]ew \[P|p]assword:" {
        send "$password\r"
        exp_continue
    }
    -re  "[R|r]e-enter new \[P|p]assword\r"
        send "$password\r"
        exp_continue
    }
    "passwd: Password too short - must be at least 6 characters." {
        puts stdout "Bad Password"
        exit 1
    }
    eof {
       exit
    }
}</tt>
This is just a short example of what Expect can do, it isn't a bullet proof password change utility, but hopefully shows you a bit better what expect can do. First it does regular expressions, because not all passwd programs are the same, some use upper case some use lower case, and here I am expecting both. The 'exp_continue statement tells expect to continue evaluating output, where it picks up the 'Re-enter' statement.

Expect can actually respond to all situations a normally interactive program such as password might respond back to the user with. Perl can do this, but not as easy as it can be done with expect.

#

Return to Automate interactive transactions with Expect