How to run mysql database using shell script if the base name has "-"

Asked

Viewed 1,262 times

2

On the server there are some databases with the name 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 command line:

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

returns the 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 should I mount this command so it doesn’t error and create the database correctly?

  • Try "create database" template-submodel";"

  • 1

    Also try with the grave accent, mysql -h"$DBHOST" -u"$DBUSER" -p"$DBPASS" -e "create database \submodel model`;"`

  • @lazyFox worked, inside the script had to put mysql -h"$DBHOST" -u"$DBUSER" -p"$DBPASS" -e "create database \model-submodel`"` thank you very much.

2 answers

2


You can easily include the hyphen in the command using the severe accent, as follows::

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

0

Whenever you need to use special characters such as the contents of command line parameters (bash), use quotes (single or double) around the entire value of the parameter. You can also insert them. Remember that double quotes subject the content to be expanded (when it fits) while single quotes do not.

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

mysql -h"$DBHOST" -u"$DBUSER" -p"$DBPASS" -and "create database `template-submodel`;"

Browser other questions tagged

You are not signed in. Login or sign up in order to post.