[英]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"", “ 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. 我需要它以“ index”:“ $ x”的形式出现,我不能绕过它产生的双引号,如果我尝试用单引号将整个内容转义,脚本会认为它已停止。
I've tried everything.. Anything would be appreciated 我已经尝试了一切..任何事情将不胜感激
Thank you! 谢谢!
Use a here-document, this way you don't have to care about quoting (in this case, since the document doesn't need to contain any backticks or dollar signs): 使用此处文档,这样就不必担心引用(在这种情况下,因为文档不需要包含任何反引号或美元符号):
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. 结束标记(在这种情况下为END_DOC
)需要向左齐平,没有缩进。
You shouldn't try to generate dynamic JSON like this, because you don't know if anything in the contents of x
needs to be escaped (although in this case, a valid SQL table name is unlikely to need any special treatement). 您不应该尝试像这样生成动态JSON,因为您不知道是否需要转义x
内容中的任何内容(尽管在这种情况下,有效的SQL表名称不太可能需要任何特殊处理)。 You should use something like jq
to generate the JSON instead. 您应该使用类似jq
东西来生成JSON。
# This is not JSON itself; it is a filter to be used by jq
# to *generate* JSON.
template='{
"type" : "jdbc",
"jdbc" : {
"url" : "jdbc:mysql://localhost:3306/test",
"user" : "root",
"password" : "<pass>",
"sql" : "select * from \($table)",
"elasticsearch": {
"cluster" : "Search",
"host": "<ip>",
"port": 9300
},
"index" : $table,
"type": $table
}
}'
while IFS= read -r x; do
jq -n --arg table "$x" "$template" | java ...
done < raw_tables.txt
Using jq
solves many of the same quoting problems that a here document does as well. 使用jq
解决了许多本文文档中同样的引用问题。
You can also read the table names from standard input, instead of passing them as a JSON variable. 您还可以从标准输入中读取表名,而不是将它们作为JSON变量传递。 You replace $table
with .
您将$table
替换$table
.
in the template, then call jq
with the -R
option instead of the --arg
option. 在模板中,然后使用-R
选项而不是--arg
选项调用jq
。
template='{
"type" : "jdbc",
"jdbc" : {
"url" : "jdbc:mysql://localhost:3306/test",
"user" : "root",
"password" : "<pass>",
"sql" : "select * from \(.)",
"elasticsearch": {
"cluster" : "Search",
"host": "<ip>",
"port": 9300
},
"index" : .,
"type": .
}
}'
while IFS= read -r x; do
echo "$x" | jq -R "$template" | java ...
done < raw_tables.txt
here you go, remove one of the double quotes in each pair around $x
在这里,请删除$x
中每对中的双引号之一
$ for x in table1 table2 table2; do
> echo 'bla bla bla
> "index" : "'$x'"
> bla bla bla'
> done
bla bla bla
"index" : "table1"
bla bla bla
bla bla bla
"index" : "table2"
bla bla bla
bla bla bla
"index" : "table2"
bla bla bla
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.