Shell script to execute the MySQL statement problems, ask the group of Daniel to answer, is a single quotation mark problem, changed to double quotation marks can be
#小提示 (Nothing to do with this topic): Ubuntu is currently using dash, causing some for (() function not to perform properly,
sudo dpkg-reconfigure Dash
If you choose No, you'll bash.
One. The script reads as follows:
#/bin/bashhostname= ' 192.168.2.52 ' port=3306username= ' root ' password= ' 123456 ' #dbname = ' #-----SQL CMD---------------------------sql_cmd= ' ALTER TABLE ' mysql '. ' User ' ADD COLUMN ' Test ' VARCHAR (1) NULL COMMENT ' test ' after ' T3 '; ' Sql_cmd_02= ' Show tables; ' sql_cmd_03= ' ALTER TABLE ' mysql '. ' User ' ADD COLUMN ' test3 ' VARCHAR (1) NULL COMMENT ' operating system name test_03 '; Sql_cmd_04= ' select * from ' MySQL '. ' User '; ' sql_cmd_05= ' ALTER TABLE ' mysql '. ' User ' ADD COLUMN ' test3 ' VARCHAR (1) NULL COMMENT ' operating system name test_03 '; Mysql-h${hostname}-p${port}-u${username}-p${password}-E "${sql_cmd_03}"
Two. Error content:
T
[Email protected]:~$ sh mysql-tb.sh ERROR 1064 (42000) at line 1:you has an error in your SQL syntax; Check the manual-corresponds to your MariaDB server version for the right syntax-use-near ' operating system name test_03 ' at line 1
Three. How to resolve:
The contents of the comment in the SQL statement require double quotation marks:
ALTER TABLE ' mysql '. ' User ' ADD COLUMN ' test3 ' VARCHAR (1) NULL COMMENT ' os name test_03 '; ' Change to: ALTER TABLE ' mysql '. ' User ' ADD COLUMN ' test3 ' VARCHAR (1) NULL COMMENT "Operating system name test_03";
Problems with shell scripts executing MySQL statements