Search code examples
bashscriptingpearexpect

Automating pear install with expect


I'm working on a small script to try and automate the installation of pear on a system should it be missing - basically, taking the interactive portions of the installation process and making them non-interactive so all I need to do is execute the script and it does it for me on its own. This is my first time trying to use expect and it's, well, not what I expected.

Sample code below:

 /usr/bin/expect <<EOD
          set timeout 20
          set number [lindex $argv 0]
          set path [lindex $argv 1]

 spawn php -q go-pear.phar
 expect "1-11, 'all' or Enter to continue:"
 send "$number\r"
interact
 expect "Installation base ($prefix) [/usr/local] :"
send "$path\r"
 interact
EOD

This is not quite working at the moment as it's trying to fill in the /usr/local portion in the first menu as follows:

1-11, 'all' or Enter to continue: invalid command name "/usr/local"
    while executing
"/usr/local"
    invoked from within
"expect "Installation base () [/usr/local] :""

Can anyone show me what I'm missing here? Also, I'd like to get this so I don't have to run it as ./script $var0 $var1 but rather just run it as ./script with everything being contained within and filled in, but not sure how to do that.

Thanks in advance for any help you can offer.


Solution

  • I ended up figuring this out. In case anyone else wants to do something similar, here's what worked for me:

     /usr/bin/expect <<EOF
    set send_slow {1 .1}
    set timeout -1   
     spawn php -q go-pear.phar
     expect "1-11, 'all' or Enter to continue:"
       sleep .1
     send -s -- "1\r"
     expect -exact "1\r"
       sleep .1
     send -s -- "/usr/local\r"
     expect -exact "/usr/local\r"
     expect "1-11, 'all' or Enter to continue:"
       sleep .1
     send -s -- "\r"
     expect -exact "\r"
     expect EOF