[英]How to insert schema file into mysql under bash
I want to insert schema file content into the MySQL database as a single field via mysql command under bash. 我想通过bash下的mysql命令将模式文件内容作为单个字段插入MySQL数据库。 It is easy to do under mysql command line. 在mysql命令行下很容易做到。 But my case is that I want to use it together with several dynamic variables. 但是我的情况是我想将其与几个动态变量一起使用。 Code is looks like below: (i is the index of services) 代码如下所示:(i是服务的索引)
WSDL=`cat ${WSDL_FILE}`
WADL=`cat ${WADL_FILE}`
XSD=`cat ${XSD_FILE}`
ADD_SERVICE=$(eval echo "insert into Services set Name=\'\${SRV_LIST_${i}}\', Description=\'\${SRV_LIST_${i}_Description}\', Version=\'\${SRV_LIST_${i}_Version}\', Status=\'Active\', WSDL=\'\'\'${WSDL}\'\'\', WADL=\'\'\'${WADL}\'\'\', XSD=\'\'\'${XSD}\'\'\', CreatedBy=\'${USER}\', CreatedOn=NOW\(\), CreatedAt=\'${HOSTNAME}\';")
Result=$(mysql -umysql -pmysqlpasswd -D service_reg -e "${ADD_SERVICE}")
The result would be like below: 结果如下所示:
./ServiceRegistry.sh: eval: line 279: syntax error near unexpected token `newline'
./ServiceRegistry.sh: eval: line 279: `echo insert into Services set Name=\'${SRV_LIST_1}\', Description=\'${SRV_LIST_1_Description}\', Version=\'${SRV_LIST_1_Version}\', Status=\'Active\', WSDL=\'\', WADL=\'<?xml version="1.0" encoding="UTF-8"?>'
But if I set WSDL, WADL and XSD to empty string, it works fine. 但是,如果我将WSDL,WADL和XSD设置为空字符串,则可以正常工作。 Any ideas? 有任何想法吗? Thanks. 谢谢。
This seems to be the culprit 这似乎是元凶
WADL=\'<?xml version="1.0" encoding="UTF-8"?>'
The double quotes aren't escaped and the string prematurely terminated and reopened. 双引号不会转义,字符串会过早终止并重新打开。 Maybe, there are even more quotes down the road. 也许,将来还有更多报价。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.