K
K
Kirill Gerasenkov2016-07-08 14:22:55
MySQL
Kirill Gerasenkov, 2016-07-08 14:22:55

How to properly escape quotes in bash?

There is a bash script that contains this variable:

Q='SELECT *
     FROM blabla.users s
     LEFT JOIN auth.users u on s.userId = u.userId
     WHERE
     s.userId is not NULL and
     not( (u.email like "[email protected]%" OR u.email like "[email protected]%") ) and u.deactivated is NULL
     INTO OUTFILE '/home/blalba/$F.csv'
     FIELDS TERMINATED BY ','
     LINES TERMINATED BY '\n';
     '

when executed - swears:
ERROR 1064 (42000) at line 1: You have an error in your SQL syntax; check the manual that corresponds to your MariaDB server version for the right syntax to use near '/home/blabla/Fri_Jul__8_13:20:37_MSK_2016.csv FIELDS
TERMINATED BY ,
' at line
The FIELDS command and, accordingly, the next one will also not be used, which SQL swears at:
+ Q='SELECT *
    FROM blabla.users s
    LEFT JOIN auth.users u on s.userId = u.userId
    WHERE
    s.userId is not NULL and
    not( (u.email like "[email protected]%" OR u.email like "[email protected]%") ) and u.deactivated is NULL
    INTO OUTFILE /home/blabla/Пт_июл__8_13:20:37_MSK_2016.csv
    FIELDS TERMINATED BY ,
    LINES TERMINATED BY \n;
    '

So, how to properly escape ' ?

Answer the question

In order to leave comments, you need to log in

2 answer(s)
A
Andrey Burov, 2016-07-08
@m000dy

Single quote is not escaped, use double quotes

F="asd";
Q="SELECT *
     FROM blabla.users s
     LEFT JOIN auth.users u on s.userId = u.userId
     WHERE
     s.userId is not NULL and
     not( (u.email like \"[email protected]%\" OR u.email like \"[email protected]%\") ) and u.deactivated is NULL
     INTO OUTFILE '/home/blalba/$F.csv'
     FIELDS TERMINATED BY ','
     LINES TERMINATED BY '\n';
     "
echo "$Q"

S
sim3x, 2016-07-08
@sim3x

In such cases it is better to use EOF

cat << EOF | psql ---params
BEGIN;

`pg_dump ----something`

update table .... statement ...;

END;
EOF
stackoverflow.com/questions/2500436/how-does-cat-e...

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question