使用bash脚本超过硬盘大小时自动激活Elasticsearch索引

时间:2016-07-13 06:45:51

标签: json linux bash curl elasticsearch

我是bash脚本的新手,我想自动删除elasticsearch中保存的旧记录。我可以通过手动使用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"
          }
        }
      }
    }'

我想通过shell脚本自动解析日期,而不是手动传递日期。当我将这些字段传递给脚本时,我检索了日期并存储在字段中我收到了一些错误

   " curl -XDELETE 'index name/_query' -d '
    {
      "query": 
      {
        "range": 
                    {
          "eventType_timestamp": 
                      { 
                      "gte": $from_dataset_date,
                      "lte": $to_dataset_date
          }
        }
      }
    }'"

错误:这是我在运行上述卷曲时遇到的错误

{"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}

1 个答案:

答案 0 :(得分:2)

似乎引用有问题 - 单引号,双引号和转义组合。 Curl发送json数据。 Bash变量应该是双引号,但json数据应该以单引号开头。

示例:

data="'"'{"json": "finish string after single quote ->", '"\"$BASH_VAR\""',"json1": "finish data"}'"'"

在上面的例子中,我在单引号和双引号中使用了连接字符串("'"'{..' '添加到{..)。

在bash脚本中尝试这样的事情

curl -XDELETE 'index name/_query' -d "'"'
    {
      "query": 
      {
        "range": 
                    {
          "eventType_timestamp": 
                      { 
                      "gte": '"\"$from_dataset_date\""',
                      "lte": $to_dataset_date
          }
        }
      }
    }'"'"