1

我有一个 shell 脚本,它从文件中获取行,并通过 mysql 将它们发送到数据库。但是,它正在读取的某些行包含双引号。

例子 -This is a "line from a file" with words

这一行最终被放入一个变量中,被输入 mysql 命令以将其发送到数据库。这就是问题出现的地方。变量中的引号导致 mysql 命令出错。

例子:

MySQLString='INSERT INTO MyTable (`id`, `line`,) VALUES (NULL, "'"$LifeFromFile"'")'
mysql -uName -pPass --execute "$MySQLString"
4

1 回答 1

2

您可能想在printf这里使用:

line='This is a "line from a file" with words'

printf -v sqlStr "INSERT INTO MyTable (id, line) VALUES (NULL, '%s')" "$line"
echo "$sqlStr"

INSERT INTO MyTable (`id`, `line`) VALUES (NULL, 'This is a "line from a file" with words')
于 2018-06-25T20:50:06.663 回答