Alex Rieker Alex Rieker - 4 months ago 15
Linux Question

Escape double quote inside a single quote

for x in $(cat raw_tables.txt)
do
echo '{
"type" : "jdbc",
"jdbc" : {
"url" : "jdbc:mysql://localhost:3306/test",
"user" : "root",
"password" : "<pass>",
"sql" : "select * from "'$x'"",
"elasticsearch" {
"cluster" : "Search",
"host": "<ip>",
"port": 9300
},
"index" : ""'$x'"",
"type": ""'$x'""
}
}' | java \
-cp "/etc/elasticsearch/elasticsearch-jdbc-2.3.3.1/lib/*" \
-Dlog4j.configurationFile=/etc/elasticsearch/elasticsearch-jdbc-2.3.3.1/bin/log4j2.xml \
org.xbib.tools.Runner \
org.xbib.tools.JDBCImporter


cat raw_tables.txt
table1
table2
table3


When i run that it comes out as

"index" : ""$x"",

I need it to come out as "index" : "$x",
I can't bypass the double quote it's producing and if I try escaping the entire thing with a single quote the script thinks it's stopped.

I've tried everything.. Anything would be appreciated

Thank you!

Answer

Use a here-document, this way you don't have to care about quoting:

while read x; do
  java ... lots of options \
    more options for java \
    and more options for java  <<END_DOC
{
    "type" : "jdbc",
    "jdbc" : {
        "url" : "jdbc:mysql://localhost:3306/test",
        "user" : "root",
        "password" : "<pass>",
        "sql" : "select * from $x",
        "elasticsearch" {
                "cluster" : "Search",
                "host": "<ip>",
                "port": 9300
        },
        "index" : "$x",
        "type": "$x" 
    }
}
END_DOC
done <raw_tables.txt

The end marker (END_DOC in this case) needs to be flush to the left, with no indentation.

Comments