简体   繁体   中英

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:

I have a CSV file, which looks as follows:

project api_name api_result response

a xyz PASS 200,Date: Mon, 16 Nov 2015 20:52:26

b abc FAIL 501,Date: Mon, 17 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

1) invoke a curl everytime there is a FAIL in api_result.

2) Pass values from CSV to my curl command. Example i need to pass the value of api_name from CSV as summary in my data as shown below:

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. The reason I am trying to do this using shell is because I'm using 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):

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. 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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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