How to write the expect script to install mariadb?

Not only are square brackets used for command substitution, but they are also special for glob patterns.

You can either use the -exact switch while escaping the square brackets in quotes:

spawn mysql_secure_installation
expect {
    ...
    -exact "Set root password? \[Y/n\] " {send "y\r";exp_continue}
    ...
}

Or use braces instead of quotes:

spawn mysql_secure_installation
expect {
    ...
    {Set root password? \[Y/n\] } {send "y\r";exp_continue}
    ...
}

FYI you can have the expect script generated for you by using autoexpect:

autoexpect ./mysql_secure_installation

This will generate an expect script called script.exp in your present working directory.


These scripts wait to receive optional output (timeout -1 means "no timeout") and they can tell apart different responses, as it is required by yum install and mysql_secure_installation. With #!/bin/expect -f as shebang, the scripts can be executed, when they were set to chmod +x.

A) To begin with, mariadb_yum.exp (requires su or sudo):

#!/bin/expect -f
set timeout 30
if {[llength $argv] == 0} {
    send_user "Usage: mariadb_yum.exp \[linux sudo password\]\n"
    exit 1
}
set USERNAME "[exec whoami]"
set PASSWORD [lindex $argv 0];

# optionally, redirect output to log file (silent install)
# log_user 0
# log_file -a "/home/$USERNAME/mariadb_install.log"

spawn sudo yum -y install MariaDB-server
set yum_spawn_id $spawn_id

# On GCE it will never ask for a sudo password:
expect -ex "\[sudo\] password for $USERNAME: " {
   exp_send "$PASSWORD\r"
}

expect {
    # when the package was already installed
    -ex "Nothing to do" {
        send_user "package was already installed\n"
    }
    # when the package had been installed
    -ex "Complete!" {
        send_user "package had been installed\n"
    }
}

expect eof
close $yum_spawn_id
exit 0

B) And then mariadb_sec.exp (doesn't require sudo):

#!/bin/expect -f
set timeout 1
if {[llength $argv] == 0} {
    send_user "Usage: mariadb_sec.exp \[mysql root password\]\n"
    exit 1
}
set PASSWORD [lindex $argv 0];

spawn mysql_secure_installation
set mysql_spawn_id $spawn_id

# optionally, redirect output to log file (silent install)
# log_user 0
# log_file -a "/home/[exec whoami]/mariadb_install.log"

# when there is no password set, this probably should be "\r"
expect -ex "Enter current password for root (enter for none): "
exp_send "$PASSWORD\r"

expect {
    # await an eventual error message
    -ex "ERROR 1045" {
        send_user "\nMariaDB > An invalid root password had been provided.\n"
        close $mysql_spawn_id
        exit 1
    }
    # when there is a root password set
    -ex "Change the root password? \[Y/n\] " {
        exp_send "n\r"
    }
    # when there is no root password set (could not test this branch).
    -ex "Set root password? \[Y/n\] " {
        exp_send "Y\r"
        expect -ex "New password: "
        exp_send "$PASSWORD\r"
        expect -ex "Re-enter new password: "
        exp_send "$PASSWORD\r"
    }
}
expect -ex "Remove anonymous users? \[Y/n\] "
exp_send "Y\r"
expect -ex "Disallow root login remotely? \[Y/n\] "
exp_send "Y\r"
expect -ex "Remove test database and access to it? \[Y/n\] "
exp_send "Y\r"
expect -ex "Reload privilege tables now? \[Y/n\] "
exp_send "Y\r"

expect eof
close $mysql_spawn_id
exit 0

For debugging purposes - or to validate the answer, one can run expect with log-level strace 4. This is probably as reputable as a source can get, when it comes to writing expect scripts, as it nicely displays what is going on and most importantly, in which order the things happen:

expect -c "strace 4" ./mariadb_yum.exp [linux sudo password]
expect -c "strace 4" ./mariadb_sec.exp [mysql root password]

Instruction set exp_internal 1 can be used to get output for the regex-matching.


A possible source of confusion might be, where one spawns the processes - as one can spawn several process on various hosts, eg. ssh locally and then yum and mysql_secure_installation remotely. Added $spawn_id to the script; the bottom one close call might be redundant, since it is already EOF (just to show how to spawn & close processes):

Thanks for using MariaDB!
 1  close $mysql_spawn_id
 1  exit 0
 2  rename _close.pre_expect close

Conclusion: The mariadb_sec.exp script probably could be improved further, eg. when at first sending no password and seeing what happens - then sending the password on ERROR 1045 (when a password had already been set previously). It may be save to assume, that one has to set the password when the server just has been installed (except that yum reinstall delivers the same result). Just had no blank CentOS container to test all the cases. Unless running in a root shell, passing both kinds of passwords into one script would be required to automate this from installation until post-installation.

Probably worth noting is that on GCE, sudo would not ask for a password; there are indeed minor differences based upon the environment, as these CentOS container images behave differently. In such case (since there is no su or container-image detection in place), the mariadb_yum.exp script might get stuck for 30 seconds and then continue.


The most reputable sources I can offer are the expect manual, written by Don Libes @ NIST and the TCL/TK manual for expect, along with it's SourceForge project coincidentally called expect.