Take the 2-minute tour ×
Ask Ubuntu is a question and answer site for Ubuntu users and developers. It's 100% free, no registration required.

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 \${db_pass};\";

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 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.

share|improve this question

1 Answer 1

up vote 0 down vote accepted

You need to double escape, surrounding the \${db_pass} bit with \\\" (that's the escaping sequence for \").

Like this:

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}\";
";
'
share|improve this answer

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

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