How to execute create mysql database using shell script if base name has "-"

2

On the server there are some databases named modelo-submodelo (this is the name of the database, created with "-" same).

When I execute a command like:

mysqldump -h"$DBHOST" -u"$DBUSER" -p"$DBPASS" modelo-submodelo --compress=TRUE > $TEMP/$DBTEMPNAME

works normally. However, if I have to create the base again using the command line:

mysql -h"$DBHOST" -u"$DBUSER" -p"$DBPASS" -e "create database modelo-submodelo;"

returns error due to "-":

  

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 '-submodel' at line 1

How do I mount this command so I do not get the error and create the database correctly?

    
asked by anonymous 29.03.2017 / 15:30

2 answers

2

You can easily include the hyphen in the command making use of the severe accent, following example:

mysql -h"$DBHOST" -u"$DBUSER" -p"$DBPASS" -e "create database 'modelo-submodelo';"
    
29.03.2017 / 16:49
0

Whenever you need to use special characters such as pate of the content of command line parameters (bash), use quotation marks (single or double) around the entire value of the parameter. You can even merge them. Remember that double quotation marks subject the content to be expanded (when applicable) while single quotes do not.

In your case, the table creation command is executed by the mysql client (via the "-e" option), not by the shell, by passing the command passed to the application rules. So your command line should look like this:

mysql -h "$ DBHOST" -u "$ DBUSER" -p "$ DBPASS" -e "create database 'template-submodel';"

    
31.03.2017 / 19:50