我是新的bash脚本,我想使自动化删除elasticsearch中的旧保存的logging。 我可以通过手动使用curl命令执行此操作,
" curl -XDELETE 'index name/_query' -d ' { "query": { "range": { "eventType_timestamp": { "gte": "2016-05-30T07:00:00.000Z", "lte": "2016-06-15T06:59:59.999Z" } } } }'
而不是手动传递date,我想通过shell脚本自动分析date。 我检索date和存储在一个字段,当我将这些字段传递到脚本我收到一些错误
" curl -XDELETE 'index name/_query' -d ' { "query": { "range": { "eventType_timestamp": { "gte": $from_dataset_date, "lte": $to_dataset_date } } } }'"
错误 :这是我运行上面的curl时得到的错误
{"error":{"root_cause":[{"type":"jsonparse_exception","reason":"jsonparse_exception: Unrecognized token '$fromdatasetdate': was expecting ('true', 'false' or 'null')\n at [Source: [B@ba52ee7; line: 9, column: 33]"}],"type":"search_phase_execution_exception","reason":"all shards failed","phase":"query","grouped":true,"failed_shards":[{"shard":0,"index":"","node":"-oHm1uxQQ6e7RTnCaWtQEw","reason":{"type":"query_parsing_exception","reason":"Failed to parse","index":"-dd.","caused_by":{"type":"json_parse_exception","reason":"json_parse_exception: Unrecognized token '$from_dataset_date': was expecting ('true', 'false' or 'null')\n at [Source: [B@ba52ee7; line: 9, column: 33]"}}}]},"status":400}
看来这是一个报价问题 – 与单引号,双引号和转义组合。 卷发发送json数据。 Bash变量应该用双引号,但json数据应该以单引号开头。
例:
data="'"'{"json": "finish string after single quote ->", '"\"$BASH_VAR\""',"json1": "finish data"}'"'"
在上面的例子中,我在单引号和双引号中使用连接字符串( "'"'{..'
代表'
add to {..
)。
在bash脚本中尝试这样的事情
curl -XDELETE 'index name/_query' -d "'"' { "query": { "range": { "eventType_timestamp": { "gte": '"\"$from_dataset_date\""', "lte": $to_dataset_date } } } }'"'"