duraja
duraja

Reputation: 49

How to get a string in the output of curl command in Unix shell scripting?

I am looking to cut the task number which is highlighted in the output. However, normal grep or sed commands or redirection to text file for getting the desired output is not working. As the curl is writing on the terminal screen, these operations arent working. Please suggest.

vcap@jumpbox-sagdf-staging:~ $ curl -v -s -k 'https://admin:[email protected]:25555/deployments/concourse/vms?format=full' * Hostname was NOT found in DNS cache

GET /deployments/concourse/vms?format=full HTTP/1.1

Authorization: Basic YWRtaW46OHdId1lEVlIwakJCZ3c=

User-Agent: curl/7.35.0

Host: 10.19.1.1:25555

Accept: /

>

< HTTP/1.1 302 Moved Temporarily

< Server: nginx

< Date: Fri, 30 Jun 2017 04:07:19 GMT

< Content-Type: text/html;charset=utf-8

< Content-Length: 0

< Connection: keep-alive

< WWW-Authenticate: Basic realm="BOSH Director"

< Location: https://10.19.1.1/tasks/1017317

< X-XSS-Protection: 1; mode=block

< X-Content-Type-Options: nosniff

< X-Frame-Options: SAMEORIGIN

<

Upvotes: 1

Views: 1677

Answers (1)

Daniel Stenberg
Daniel Stenberg

Reputation: 58124

Since you want to extract the piece on the right of the right-most slash of the Location: response header, I would use this:

curl -so /dev/null -w "%{redirect_url}\n" $URL | sed 's:.*/::g'

so that with your original URL use case it would look like (I added the -k back in this version):

curl -kso /dev/null -w "%{redirect_url}\n" 'https://admin:[email protected]:25555/deployments/concourse/vms?format=full' | sed 's:.*/::g'

(Do note that you've exposed your user name and password to the world here...)

Upvotes: 0

Related Questions