A
A
Alexander Kryuchkov2013-08-16 11:26:20
linux
Alexander Kryuchkov, 2013-08-16 11:26:20

Escaping quotes. Bash to mysql query

Good afternoon, I'm just tormented, I don't know what to do.
There is a simple request (for example)
UPDATE `my_db`.`Vlan` SET `number`='11' WHERE `id`='11';

I'm trying to do it like

#!/bin/bash
query="UPDATE `my_db`.`Vlan` SET `number`='11' WHERE `id`='11';"
mysql -u root -e "$query";


I only get the answer
/etc/script: line 2: my_db: command not found
/etc/tdma.sh: line 2: Vlan: command not found
/etc/tdma.sh: line 2: number: command not found


etc.

How do I properly screen everything?
I think I've gone through all the options, nothing helps.

I took the line to update the table from Mysql WorkBench, the tables are not mine, I just need to update them with a bash script.
Thank you.

Answer the question

In order to leave comments, you need to log in

5 answer(s)
A
Alexey Zhurbitsky, 2013-08-16
@kruchkov-alexandr

Backticks (`) in bash execute the command enclosed in them. Try without those quotes.

A
Alexey Akulovich, 2013-08-16
@AterCattus

It is not always possible to simply remove the quotes (for example, if '-' is used in the names of the database, tables, fields). Here it is better to replace double quotes " with single quotes '. Within them, ` is not treated as exec.

E
egorinsk, 2013-08-16
@egorinsk

Bash has an absolutely insane quote escaping system, but maybe this will help you: wiki.bash-hackers.org/syntax/quoting#ansi_c_like_strings
In general, I would put queries in separate files so as not to suffer.

S
stavinsky, 2013-08-16
@stavinsky

I risk getting a bunch of minuses, but! Use Python)

A
Anton Dyshkant, 2014-01-11
@vyshkant

Faced a similar problem and solved this issue with normal escaping:

query="CREATE DATABASE \`${site_name}\`;"
mysql -u ${mysql_user} -p${mysql_password} -e "${query}"

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question