自动化需要用户交互的stream程的最佳策略

我在Dockerfile中工作,但有一个“安全的”MariaDB服务器脚本,我需要运行这是交互式的,我不知道如何处理这个。

基本上这是我在testing环境下的脚本stream程,并且是我想在没有用户交互的Dockerfile中实现的stream程,只需回答如下面的stream程所示:

# /usr/bin/mysql_secure_installation Enter current password for root (enter for none): [ENTER] // because there is no password OK, successfully used password, moving on... Set root password? [Y/n] n ... skipping. Remove anonymous users? [Y/n] Y ... Success! Disallow root login remotely? [Y/n] n ... skipping. Remove test database and access to it? [Y/n] Y - Dropping test database... ... Success! - Removing privileges on test database... ... Success! Reloading the privilege tables will ensure that all changes made so far will take effect immediately. Reload privilege tables now? [Y/n] Y ... Success! Cleaning up... 

所以我需要写一个bash脚本或其他可以自动处理这个但不知道的东西,你会怎么处理这个?

我想你可以尝试两种不同的方法:

  1. 使用expect脚本最终从bash“评估”。 手册页在这里

要么:

  1. 使用bash脚本中的“here document”语法(不要指望)

**编辑 – 选项2的例子**

 #!/bin/bash ... /usr/bin/mysql_secure_installation <<EOF n Y n Y Y EOF ... 

基本上你把所有的答案都传递给mysql_secure_installation的标准input。

使用expect只是一点点复杂。 你可以从这里开始

另一种select,mysql_secure_installation是一个简单的bash脚本,它执行几个mysql命令,只是使用sql命令。

 DELETE FROM mysql.user WHERE User=''; DROP DATABASE test; DELETE FROM mysql.db WHERE Db='test' OR Db='test\\_%'; FLUSH PRIVILEGES; 

将这个命令保存在一个文件中,并将其传递给mysql

 mysql < my_file.sql