gpt4 book ai didi

mysql - Shell - 使用包含双引号的字符串变量

转载 作者:行者123 更新时间:2023-11-29 05:54:28 26 4
gpt4 key购买 nike

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

示例 - 这是一个带有单词的“文件行”

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

例子:

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

最佳答案

你可能想在这里使用 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')

关于mysql - Shell - 使用包含双引号的字符串变量,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/51031355/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com