Reputation: 250
I have a shell script that is taking in lines from a file, and sending them to a database via mysql. However, some of the lines it is reading contain double quotes.
Example - This is a "line from a file" with words
This line winds up getting put into a variable, to be fed into the mysql command to send it to the database. This is where the problem arises. The quotes in the variable, are causing an error in the mysql command.
Example:
MySQLString='INSERT INTO MyTable (`id`, `line`,) VALUES (NULL, "'"$LifeFromFile"'")'
mysql -uName -pPass --execute "$MySQLString"
Upvotes: 2
Views: 341
Reputation: 785008
You may want to use printf
here:
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')
Upvotes: 3