Amandeep kaur
Amandeep kaur

Reputation: 1015

Read response of curl command in shell script

I am writing a shell script in which I need to work on the response of an api. I called the api using curl command and trying to get its response in shell so that I can loop over response and print it. But I am not able to get the response. Here is the script:

#!/bin/bash
result=$(curl "https://example.com/test")
echo "test"
RESP=`echo $result | grep -oP "^[^a-zA-Z0-9]"`

echo "$RESP"

I am getting empty response after test. Can somebody help me over this ?

Upvotes: 0

Views: 16318

Answers (1)

Adhoc
Adhoc

Reputation: 121

Try this syntax:

#!/bin/bash
result="$(curl -s 'https://example.com/test')"
echo "result: '$result'"
RESP=$(echo "$result" | grep -oP "^[^a-zA-Z0-9]")
echo "RESP:'$RESP'"

Explanation:

  • We store in $result the result of the page curled (-s removes the status of download and things that don't belong to the page)
  • We echo the content of $result between quotes, to check what we got.
  • We store the content of that grep command (what are you looking for btw? please leave a comment if you need Regex help)
  • We echo the content of $RESP between quotes, to check what we got.

Upvotes: 3

Related Questions