grant command syntax error near password area in shell script - shell

I have created a shell script like this.But getting syntax error in grant command, I think near password area.Please help me if anyone could.
ssh -t qbadmin#10.3.2.0 '
su root -c "
echo \"Give db name :\";
read db_name;
echo \"Give password :\";
read db_pass;
host=localhost;
sql1=\"create database \$db_name;\";
sql2=\"grant all on \${db_name}.* to \${db_name}#\${host} identified by \"\${db_pass}\";\";
sql3=\"\${sql1}\${sql2}\";
mysql -u root -p -e \"\${sql3}\";
";
'
The database has created in the remote machine successfully, but the grant command returned error..! My guess is the error might be because of the usage of \" character before and after ${db_pass} in the grant command.Please do help me to solve this.
Thanks.

Nested quoting is always tricky. Why don't you just ssh to the remote host as root?
ssh -t root#10.3.2.0 '
echo "Give db name :"
read db_name;
echo "Give password :"
read db_pass
host=localhost
sql1="create database \$db_name;"
sql2="grant all on ${db_name}.* to ${db_name}#${host} identified by ${db_pass};"
sql3="${sql1}${sql2}"
mysql -p -e "${sql3}"
'

Related

Shell script with mysqldump

I wrote a shell script to automate mysqldump.
I don't want my password to be entered in the script file. Can anyone suggest me an alternative way to do this?
If you are running the script interactively, then you can use read to read the password into an environmental variable, and then echo that password to mysqldump.
read -s -p 'password: ' password
echo "$password" | mysqldump ...
The password will be stored in plain text in memory but not elsewhere.
Alternatively as per the documentation you can use an option file to avoid giving the password on the command line. The file would contain something similar to the below:
[client]
# The following password will be sent to all standard MySQL clients
password="my_password"

How can I capture the error returned from mysqldump and use it to take another action in a shell script?

I'm using mysqldump in a shell script to dump several schemas from a production environment to a local one.
schemas = (one two three)
read -p "Enter Username: " un
read -s -p "Enter Password: " pw
for schema in "${schemas[#]}"
do
:
mysqldump -h SERV -u $un --password=$p > /dev/null 2>&1 | mysql -uroot LOCAL
done
I'm redirecting out and error to /dev/null to prevent warnings and error messages, but I want to be able to catch the error and do something else based on the output (e.g. Access Denied, Not Found).
How can I capture the error returned from mysqldump and use it to take another action in a shell script?
For what it's worth, the $? variable always seems to be 0 after mysqldump completes, even if the STDERR is access denied.
I did a little more research and found the answer here:
http://scratching.psybermonkey.net/2011/01/bash-how-to-check-exit-status-of-pipe.html

shell scripting grant command is not working in mysql

I have created a shell script as given below.The aim is to create a database in a remote machine and give all privileges on that database for a specific user in remote machine.The shell script is as follows.
ssh root#10.3.2.0 'echo "db name :";
read db_name;
echo "db user :";
read db_user;
echo "user password:";
read password;
host=localhost;
sql1="create database $db_name;";
sql2="grant all on ${db_name}.* to ${db_user}#${host} identified by "${password}";";
sql3="${sql1}${sql2}";
mysql -u root -p -e "${sql3}";
'
And the output Iam getting is as foloows
root#10.3.2.0's password:
db name :
amblex
db user :
qbadmin
user password:
xxxx
Enter password: xxxx
ERROR 1064 (42000) at line 1: You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near 'qburst' at line 1
What could be the mistake in my script.PLease help me to resolve this.
Thanks in advance.
Instead of ...
sql2="grant all on ${db_name}.* to ${db_user}#${host} identified by "${password}";";
... try this ...
sql2="grant all on ${db_name}.* to ${db_user}#${host} identified by \"${password}\";";
Also you might want to consider using expect.

grant command is not working in shell script because of syntax error

I am stuck with an issue with shell scripting.
I have already used \ character before double quotes (") because of some reason, as in the following line of my shell script.
sql2=\"grant all on \${db_name}.* to \${db_user}#\${host} identified by \${dbpass};\";
But as in the MySQL "grant" command syntax, I need to put double quotes before and after \${dbpass}, which is the part of the shell script line I given above.When I put like this, \"\${dbpass}\", it is throwing syntax error during execution.What should I need to solve this.
This is an update.
This is the shell script which I want to run.
ssh -t qbadmin#10.3.2.0 '
su root -c "
echo \"Give db name :\";
read db_name;
echo \"Give password :\";
read db_pass;
host=localhost;
sql1=\"create database \$db_name;\";
sql2=\"grant all on \${db_name}.* to \${db_name}#\${host} identified by \"\${db_pass}\";\";
sql3=\"\${sql1}\${sql2}\";
echo \"==============\";
mysql -u root -p -e \"\${sql3}\";
";
'
Please refer this script and please let me know the necessary changes i need to do with this.
Thanks.
Try this
db_name="yourdbname"
db_user="yourdbuser"
host="yourhost"
dbpass="yourdbpass"
sql2="grant all on ${db_name}.* to ${db_user}#${host} identified by ${dbpass};"
echo $sql2 | mysql -u yourusername -pmysql

user created by shell script is not executing proceeding commands

I have created a shell script as follows.
username='root'
sudo -H -u "$username" bash 2<&0 << 'END_COMMAND'
useradd -m -s /bin/bash suhail
passwd suhail
mkdir ~/test
END_COMMAND
But i am getting the out put as follows when i trying to run this script file
user#uvais-desktop:~/ssp$ ./test
sudo: unable to resolve host uvais-desktop
Enter new UNIX password: Retype new UNIX password: Sorry, passwords do not match
passwd: Authentication token manipulation error
passwd: password unchanged
It is not prompting for password for the new user.Everything happened in a second. !!
Please help me if anyone could.
One problem is that you can't run passwd. It asks to write password twice which you can't do in the script. So you enter password once then for the second time you enter mkdir ~test, hence the message "Sorry, passwords do not match"
Instead of passwd suhail try:
echo suhail| passwd $username --stdin

Resources