1

I use the following code as part of a much larger script:

mysql -u root -p << MYSQL
    create user '${DOMAIN}'@'localhost' identified by '${DOMAIN}';
    create database ${DOMAIN};
    GRANT ALL PRIVILEGES ON ${DOMAIN}.* TO ${domain}@localhost;
MYSQL

As you can see it creates an authorized, allprivileged DB user, and a DB instance with the same value (the password will also as the same value).

DB user ====> ${domain}.
DB user password ====> ${domain}.
DB instance ====> ${domain}.

This is problematic because I need the password to be different. Of course, I could change the password manually from `${domain} after the whole script will finish to be executed, but that's not what I want:

What I want is to type/paste the password directly on execution, interactively.

In other words, I want that me being prompted for the DB user's password would be an integral part of running the script.

I've already tried the following code, which failed:

mysql -u root -p << MYSQL
    create user '${DOMAIN}'@'localhost' identified by -p;
    create database ${DOMAIN};
    GRANT ALL PRIVILEGES ON ${DOMAIN}.* TO ${domain}@localhost;
MYSQL

What is the right way to be able to insert a password interactively, by either typing/pasting directly in script execution (instead changing it manually after script execution)?

4
+100

Just have the user store the variable beforehand with read:

echo "Please enter password for user ${domain}: "; read -s psw
mysql -u root -p << MYSQL
  create user '${domain}'@'localhost' identified by '${psw}';
  create database ${domain};
  GRANT ALL PRIVILEGES ON ${domain}.* TO ${domain}@localhost;
MYSQL 

Here, the command read reads user input and stores it in the $psw variable. Note that just after entering the password value, you'll be prompted for the MySQL root password in order to connect to the MySQL database (-p flag = interactive password prompt).

  • Dear @dr01, not that you need this bounty, but I offer bounty for the following expanding after my edit was rejected. I didn't quite understand what read psw does. When I went to my local (WSL) Bash shell I typed read psw and got an empty prompt... Either clicking Enter or doing ^C took me back to the regular primary prompt... So, I don't understand how your example is different. Or maybe the whole point is that I'll type mysql -u root -p << MYSQL in this empty read prompt and I just worry for no good reason. – Arcticooling Nov 26 '17 at 22:10
  • Use 'unset psw' command when password is no longer needed. – Basilevs Nov 27 '17 at 3:39
  • Read command puts user input to a variable given as argument. – Basilevs Nov 27 '17 at 3:42
  • 1
    2 things, @dr01, I think $psw unsetting should be part of the script (to ensure 100% automation). Also, I think the read password value should be hidden so please add -s argument to read there (as you know, StackExchange won't let me edit for 2 letters). – Arcticooling Nov 27 '17 at 11:37
  • 1
    Second point done. For the first point, you don't need to unset the variable as it won't exist anyway once the script ends (it is not an environment variable). – dr01 Nov 27 '17 at 13:23

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.