Reputation: 1440
I have a bash script that has the following line in it
mysql -uusername -ppassword -e 'UPDATE \'table-name.config\' SET value=1234567890 WHERE action_code=102 AND name=\'last_updated_date\';'
but I get
./hybrid_telepath/install: line 856: unexpected EOF while looking for matching `''
./hybrid_telepath/install: line 872: syntax error: unexpected end of file
clearly I have a problem with my characters and dont close the line well.
Any idea how to do it in bash?
If I change it to
mysql -uusername -ppassword -e "UPDATE `table-name`.`config` SET value=1312626266 WHERE action_code=102 AND name='last_updated_date';"
I get
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 'SET value=1312626266 WHERE action_code=102 AND name=last_updated_date' at line 1
Thanks
Upvotes: 1
Views: 2694
Reputation: 1231
I have a similar question about -e "" of mysql cmd.
This looks normal:
$ mysql -h vip0 -u root -pqwerty -e "show databases;"
+--------------------+
| Database |
+--------------------+
| information_schema |
| mysql |
| performance_schema |
+--------------------+
But if I want to execute it via ssh, it does not work:
$ ssh g1 -- mysql -hvip0 -uroot -pqwerty -e "show databases;"
ERROR 1049 (42000): Unknown database 'databases'
As tested, it seems I need to escape the space:
$ ssh g1 -- mysql -hvip0 -uroot -pqwerty -e "show\ databases;"
Database
information_schema
mysql
performance_schema
But I don't understand why...
Upvotes: 1
Reputation: 1277
try this:
mysql -uusername -ppassword -e "UPDATE \'table-name.config\' SET value=1234567890 WHERE action_code=102 AND name=\'last_updated_date\';"
Upvotes: 1
Reputation: 3908
Escape sequences don't work inside single quotes. Use double quotes for your outer-most quotes, and then you don't need to escape the single quotes within:
mysql -uusername -ppassword -e "UPDATE 'table-name.config' SET value=1234567890 WHERE action_code=102 AND name='last_updated_date';"
Upvotes: 2
Reputation: 485
The previous poster is correct. In addition to the suggestion that you use double quotes, you might want to start building your queryString separately. This practice will serve you well as you get better at scripting (and, hopefully, move-on-to something like Python). It's also a bad practice to hard-code your database credentials in your script. Please note that you can define these values elsewhere (i.e., in another script) and then just source that script. This makes your code modular. Even better, write functions, source 'em, and reuse. Here's an example:
#!/bin/bash
. /etc/myDbCreds
myQuery="select * from foo where bar ='snafu';"
mysql -u $myUser -p${myPass} -e $myQuery
Upvotes: 1