0

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 ?

9
  • Try adding double quote in echo "${result}"... Commented Jan 8, 2020 at 11:10
  • @sungtm I did the above while grep and it has no difference Commented Jan 8, 2020 at 11:14
  • @Amandeepkaur, SSA(greetings), could you please check if your variable is not empty on first place? Do a echo "$result" and let us know then. Commented Jan 8, 2020 at 11:23
  • 1
    Sat Sri Akal @RavinderSingh13, I did echo "$result" just after curl statement and its empty. If I replace curl with wget then i get a file saved in my system which is having correct json response. Do I need to pass some headers with the curl ? Commented Jan 8, 2020 at 11:27
  • @Amandeepkaur, Cool so now we cleared first step of troubleshooting, we know our first variable itself DO NOT have any value so innocent grep can't do anything there, lemme check on something and get back to you here. Commented Jan 8, 2020 at 11:28

1 Answer 1

3

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.
Sign up to request clarification or add additional context in comments.

2 Comments

What's the value to the literal single-quotes? (I worry that their use leads people to assume that content has been quoted/sanitized adequately to make it safe to copy-and-paste into another command line, which isn't generally true -- as "'$foo'" only evaluates to the literal content of variable foo should that content not contain any literal single-quote characters).
I generally use single quotes when I check the contents of variables to differentiate line returns, single space ' ', empty variable '', etc. It's just a quick trick to see if your variable contains something, and by no mean a best practice like testing with -z, indeed! :D

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.