2016-01-29 29 views
1

我正在Dockerfile中工作,但有一個「安全」的MariaDB服務器腳本,我需要運行哪些是交互式的,我不知道如何處理這個問題。自動化需要用戶交互的流程的最佳策略

基本上,這是我必須遵循在測試環境中的腳本的流程是一樣我想只是回答你下面的流量出現在Dockerfile實現無需用戶交互:

# /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腳本或者其他可以自動處理但不知道的東西,你會如何處理這個問題?

回答

3

我想你可以用兩種不同的方法試試:

  1. 使用expect腳本最終從bash的「評價」。手冊頁here

或者:

  • 使用 「在這裏記錄」 語法從bash腳本(不指望)
  • **編輯 - 例選項2 **

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

    基本上,您將所有答案傳遞給mysql_secure_installation的標準輸入。

    使用expect只是有點複雜。你可以從here

    +0

    優秀,我得到它現在只是一個疑問:如果我把這個名爲'secure.sh'將這個腳本調用'/ usr/bin/mysql_secure_installation'?我的意思是我還沒有得到'here document'的工作方式,因爲在我看來,我應該首先調用secure_installation腳本然後回答,你能清楚這一點嗎? – ReynierPM

    +1

    現在...是的。我以前的版本有一個錯字。 – mauro

    1

    另一種選擇,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