我试图编写一个Bash脚本,其中在本地服务器上托管的应用程序上触发了下面给出的curl,我需要从curl响应中提取以下值并将它们存储在单独的变量中:< / p>
1)HTTP响应代码即。 201 2)ETag值:即。 292a74f6-6999-403c-bc3c-d78af9d98a2a 3)&#34; sub&#34;的值来自json的回应即。 11deadd1-ca08-4995-aa4b-a980307f2b1d
`curl -X POST -v -sS http://localhost:8087/idp-backend/profile/ -H 'accept: application/json' -H 'content-type: application/json' -H 'x-int-opco-id: NV' -d '{"user_details": {
"name": "myname",
"given_name": "Diego",
"family_name": "Maradona",
"middle_name": "Armando",
"email": "test@gmail.com",
"email_verified": "true",
"gender": "male",
"phone_number": "491234569999",
"phone_number_verified": true,
"address": {
"street_address": "6767 Collins Ave",
"locality": "Miami Beach",
"region": "FL",
"postal_code": "33141",
"country": "DEU"
}
},
"device_details": {
"user_agent": "iOS",
"device_id": "123123123123123123",
"ip_address": "1.2.3.4"
},
"security_details": {
"password": "India@1234",
"security_questions": [{
"question": "Who is God?",
"answer": "me"
}]
}
}'
Response:
* About to connect() to localhost port 8087 (#0)
* Trying 127.0.0.1... connected
* Connected to localhost (127.0.0.1) port 8087 (#0)
> POST /idp-backend/profile/ HTTP/1.1
> User-Agent: curl/7.19.7 (x86_64-redhat-linux-gnu) libcurl/7.19.7 NSS/3.27.1 zlib/1.2.3 libidn/1.18 libssh2/1.4.2
> Host: localhost:8087
> accept: application/json
> content-type: application/json
> x-int-opco-id: NV
> Content-Length: 1471
> Expect: 100-continue
>
< HTTP/1.1 100
< HTTP/1.1 201
< Set-Cookie: JSESSIONID=AFB7012E00510A9627F5A6B49C0CB3E1; Path=/idp-backend; HttpOnly
< X-Application-Context: preprod-idp-crm
< ETag: 292a74f6-6999-403c-bc3c-d78af9d98a2a
< Content-Type: application/json;charset=UTF-8
< Transfer-Encoding: chunked
< Date: Mon, 09 Apr 2018 13:24:35 GMT
<
{"user_details":{"sub":"11deadd1-ca08-4995-aa4b-a980307f2b1d","name":"myname","given_name":"Diego","family_name":"Maradona","middle_name":"Armando","email":"shiv.mishra@vodafone.com","email_verified":true,"gender":"male","zoneinfo":"CET","locale":"AR","phone_number":"491234567893","phone_number_verified":true,"is_blocked":false,"updated_at":"2018-04-09T13:24:35Z","status":"active","market":"NV","address":{"street_address":"6767 Collins Ave","locality":"Miami Beach","region":"FL","postal_code":"33141","country":"de"}},"kyc_details":{"status":"not-started","last_updated":"2018-04-09T13:24:35Z","history":[{"status":"not-started","timestamp":"2018-04-09T13:24:35Z","comment":"User updated status with: not-started"}],"completeness":"partial","completeness_last_update":"2018-04-09T13:24:35Z"},"links":{"self":{"href":"http://localhost:8087/idp-backend/profile/11deadd1-ca08-4995-aa4b-a980307f2b1d"},"urn:vodafoneid:profile:update":{"href":"http://localhost:8087/idp-backend/profile/11deadd1-ca08-4995-aa4b-a980307f2b1d","method":"PATCH","data":{"name":"{name}","given_name":"{given_name}","family_name":"{family_name}","middle_name":"{middle_name}","preferred_username":"{preferred_username}","nickname":"{nickname}","profile":"{profile_url}","picture":"{picture_url}","website":"{website_url}","email":"{email_address}","email_verified":false,"gender":"{gender}","birthdate":"{birthdate}","birthplace":"{birthplace}","zoneinfo":"{zoneinfo}","locale":"{locale}","phone_number":"{phone_number}","phone_number_verified":true,"is_blocked":false,"status":"<active|inactive>","address":{"street_address":"{street_address}","locality":"{locality}","region":"{region}","postal_code":"{postal_code}","country":"{country}"},"vodafone_phone_number":false},"headers":[{"x-int-opco-id":"NV","Content-Type":"application/json"}]},"urn:vodafoneid:profile:delete":{"href":"http://localho* Connection #0 to host localhost left intact
* Closing connection #0
st:8087/idp-backend/profile/11deadd1-ca08-4995-aa4b-a980307f2b1d","method":"DELETE","headers":[{"x-int-opco-id":"NV"}]}}}
` 我试图将curl响应存储在名为content的变量中并尝试执行echo $ content | jq -r&#39;。[] .sub。
然而,这不起作用,而且甚至HTTP响应和Etag值也不是Json Body的一部分所以,我如何编写bash脚本以便我能够存储所有上述3个值(http ,Etag&amp; sub)一次性使用不同的变量。
提前致谢。
答案 0 :(得分:0)
您可以将输出显示为标准输出,并使用类似-P和-o标志的内容来获取所需的部件。
-P(或--perl-regexp)允许您为与正则表达式匹配的任何内容进行grep,并且当与-o(或--only-matching)一起使用时,您可以非常具体地了解要回显的内容。
因此,例如,在curl的末尾添加| grep -Po '(?i)(?<http>^< http.*$)'
将返回以不区分大小写< http
开头的行
blake.payne tmp $ cat test | grep -Po'(?i)(?^&lt; http。* $)'
这给了我以下例子:
< HTTP/1.1 100
< HTTP/1.1 201