'How to handle commands that require user intervention when writing shell script? [duplicate]

I am crating a script to deploy an application which requires me to install mysql. now first command that i need to run is

apt install mysql-server

This has a y/n prompt which i can handle with -y flag but after this i need to run the following command

mysql_secure_installation 

This command asks user for multiple inputs like

validate password Y/n prompt, 
password strength 0/1/2, 
password, 
repeat password, 
continue with password y/n, 
delete anonymous users y/n, 
disallow remote root login y/n, 
remove test database y/n, 
reload previlidge tables y/n

Is there a way that i can specify all the options in the script



Solution 1:[1]

You can create a txt file with responses and pass it to your interactive command, like this:

/path/to/my/command << commands.txt

You can also use expect command: https://linux.die.net/man/1/expect

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1