简体   繁体   中英

special characters in bash script

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

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';"

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 (ie, 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

尝试这个:

mysql -uusername -ppassword -e "UPDATE \'table-name.config\' SET value=1234567890 WHERE action_code=102 AND name=\'last_updated_date\';"

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM