regexbashescapingexpect

expect regular expression does not work with [yes/NO]


I'm running easyrsa init-pki in bash script, and use expect to auto answer the questions, but the following expect settings are not working as expected:

expect {
    -re {Confirm removal:} {send "yes\r" ; exp_continue}
    -re {[yes/NO]:} {send "yes\r" ; exp_continue}
}

Here is the console output when running easyrsa init-pki:

WARNING!!!

You are about to remove the EASYRSA_PKI at:
* /home/test

and initialize a fresh PKI here.

Type the word 'yes' to continue, or any other input to abort.
  Confirm removal: yes

         ******************************************
         * SECOND WARNING - STOP - SECOND WARNING *
         ******************************************

  To keep your current 'pki/vars' settings use 'init-pki soft'.
  To keep your current Request files use 'init-pki soft'
  The Requests can then be signed by a new CA (Partial CA renewal)
  To keep your current Easy-RSA TLS Key use 'init-pki soft'
  This private key file is in use by your current VPN.

       ** USE OF   'init-pki soft'   IS RECOMMENDED **


Type the word 'yes' to continue, or any other input to abort.
  
  WARNING: COMPLETELY DESTROY current PKI (NOT recommended) ?

    [yes/NO]: 

The first rule -re {Confirm removal:} {send "yes\r" ; exp_continue} is working as expected, it can auto answer with yes, but the second rule -re {[yes/NO]:} {send "yes\r" ; exp_continue} won't auto answer with yes.

What's wrong in my regular expression?


Solution

  • Since [] has special meaning for RE you should write like this:

    -re {\[yes/NO]:}
    
    #
    # or use exact match
    #
    -ex {[yes/NO]:}