0

I am creating a script to setup a series of computers. One of the apps to install is a mysql database. The bash script will call

sudo mysql_secure_installation

The expect script will have to respond to the questions that are output in sequence from this command, answers will be hardcoded and will be the same for all.

Questions and responses are

Enter current password for root (enter for none): Just press the Enter
Set root password? [Y/n]: Y
New password: Enter password
Re-enter new password: Repeat password
Remove anonymous users? [Y/n]: Y
Disallow root login remotely? [Y/n]: Y
Remove test database and access to it? [Y/n]:  Y
Reload privilege tables now? [Y/n]:  Y
Cyrus
  • 84,225
  • 14
  • 89
  • 153
Rhys Clarke
  • 77
  • 12
  • 1
    [Stack Overflow](http://stackoverflow.com/tour) is a question and answer site for professional and enthusiast programmers. Please show your coding efforts. – Cyrus Apr 01 '18 at 09:54
  • If this is a Debian-based platform, the proper solution is a debconf preseed file with the replies to the prompts. Other professional environments offer something similar. – tripleee Apr 01 '18 at 10:06
  • Ubuntu 16.04 lts – Rhys Clarke Apr 01 '18 at 10:19
  • Possible duplicate of https://stackoverflow.com/questions/7739645/install-mysql-on-ubuntu-without-a-password-prompt – tripleee Apr 01 '18 at 12:02
  • take a look at [*sexpect*](https://github.com/clarkwang/sexpect). – pynexj Apr 23 '18 at 15:23

1 Answers1

2

I recommend to try it first without expect:

echo -e '\nY\nfoobar\nfoobar\nY\nY\nY\nY' | sudo mysql_secure_installation
Cyrus
  • 84,225
  • 14
  • 89
  • 153
  • Thanks, was overwhelmed by expect, up until last night i had never used bash either but this worked great! – Rhys Clarke Apr 01 '18 at 17:18
  • 1
    As [glenn jackman](https://stackoverflow.com/questions/49596694/bash-to-expect-and-back-again/49597187#comment86203054_49597187) suggested, it becomes more readable with a [here-doc](https://en.wikipedia.org/wiki/Here_document). – Cyrus Apr 01 '18 at 17:24