'How to ask user for input for mysql
I have the following problem that if I can resolve I will have fully automated all sections of required input. I have correctly injected sql statements into mysql via bash script, but this relies on an _EOT and the correct password already included in the script. If the password is different than that then you have to go to the line in the script and enter the correct password you require then run the script. This is counter-intuitive to wanting full automation of the script. So, here is the section where I inject the sql statements:
echo "This section of the script will configure mysql users and databases as per requirements:"
password=$(grep "temporary password" /var/log/mysqld.log | awk '{print $NF}') << "_EOT"
echo "DEBUG: The temporary password from the log is: $password"
output=$(mysql --connect-expired-password -uroot -p"$password")
ALTER USER `root`@`localhost` IDENTIFIED BY '<password>',`root`@`localhost` PASSWORD EXPIRE NEVER;
SHOW VARIABLES LIKE 'validate_password%';
SET GLOBAL validate_password_policy=LOW;
ALTER USER `root`@`localhost` IDENTIFIED BY '<password1>',`root`@`localhost` PASSWORD EXPIRE NEVER;
quit
_EOT
Is there a way where I can ask the user to supply the password into a variable and then inject the variable into mysql in place of having to type into the bash-script the passwords?
Solution 1:[1]
In Bash you can use read for user inputs.
Example:
echo "Enter User Name:"
read user_name
echo "Enter the password:"
read -sp user_pw
echo "The current User is $user_name"
echo "The password is $user_pw"
You can also use read -s for the password input to be "silent" or read -p to input on a new command prompt.
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 | SikorskyS60 |
