简体   繁体   English

用于调用curl的Shell / bash脚本

[英]Shell/bash script for invoking curl

I need to invoke a Rest api call if i encounter a fail in a field in CSV file: 如果我在CSV文件中的字段中遇到失败,我需要调用Rest api调用:

I have a CSV file, which looks as follows: 我有一个CSV文件,如下所示:

project api_name api_result response 项目 api_name api_result 响应

a xyz PASS 200,Date: Mon, 16 Nov 2015 20:52:26 xyz PASS 200,日期:2015年11月16日星期一20:52:26

b abc FAIL 501,Date: Mon, 17 Nov 2015 20:52:26 b abc失败501,日期:2015年11月17日星期一20:52:26

c mas PASS 200,Date: Mon, 18 Nov 2015 20:52:26 c mas PASS 200,Date:Mon,18 Nov 2015 20:52:26

I need a shellbash script to do the following 我需要一个shellbash脚本来执行以下操作

1) invoke a curl everytime there is a FAIL in api_result. 1)每次在api_result中出现FAIL时调用curl。

2) Pass values from CSV to my curl command. 2)将值从CSV传递到我的curl命令。 Example i need to pass the value of api_name from CSV as summary in my data as shown below: 示例我需要将CSV中api_name的值作为摘要传递给我的数据,如下所示:

Request: 请求:

curl POST --data {see below} -H "Content-Type: application/json" http://localhost:8090/rest/api/2/issue/

Data 数据

{
    "fields": {

       "summary": **$api_name**

   }
}

I am open to alternate options as well instead of shell/bash scripts. 我也开放替代选项,而不是shell / bash脚本。 The reason I am trying to do this using shell is because I'm using Jenkins. 我尝试使用Shell进行此操作的原因是因为我正在使用Jenkins。

The input you describe is not really in CSV format, but assuming the input is as you showed and is in a file named input.txt, the following outlines one approach you could take (using bash): 您描述的输入并不是真正的CSV格式,但是假设输入与您所显示的相同,并且位于名为input.txt的文件中,则以下概述了您可以采用的一种方法(使用bash):

while read project api_name api_result etc
do
    if [ "$api_result" = "FAIL" ] ; then
      echo curl ... $'\'{"fields": { "summary": "' "$2" $'" }}\''
    fi
done < <(cat input.txt)

This assumes that the project and api_name appear as the first two tokens on the input line. 假设project和api_name作为输入行上的前两个标记出现。 If that is not the case, then you might like to consider first modifying input.txt so that it's in TSV (tab-separated values) format. 如果不是这种情况,那么您可能需要考虑首先修改input.txt,使其采用TSV(制表符分隔的值)格式。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM