2
response=$(curl -sL -w \\n%{http_code} "http://<ip_addr>/api/1/app" -X DELETE)
echo response
if [ "$response" -eq 200 ]
then 
    echo "Got 200 OK"
else
    echo "not getting the result"
fi
  • What i'm trying to do is to get the http response code.
  • I'm positive that the response should be 200 OK
  • When I run the script I'm getting

    {
        "result":true
    }
    200
    tst.sh: line 302: [: {
    200: integer expression expected
    
  • I even don't want to display

    {
        "result":true
    }
    

    I just want to print 200 and compare 200.

1
  • I'm using bash shell. Commented Aug 8, 2014 at 8:03

3 Answers 3

4

Just with curl command:

curl -sL -w '%{http_code}' "http://<ip_addr>/api/1/app" -X DELETE -o /dev/null
Sign up to request clarification or add additional context in comments.

1 Comment

Just confirmed the idea. You got it first :)
2

Get the last line of the output.

response=$(curl -sL -w \\n%{http_code} "http://<ip_addr>/api/1/app" -X DELETE | tail -1)

Besides tail -n you can also use:

awk 'END { print }'
sed -n '$p'

Another way if you're using bash is to remove everything before the last line:

shopt -s extglob
response=${response##*[[:space:]]}

1 Comment

@Adarsh klashxx's answer (response=$(curl -sL -w '%{http_code}' "http://<ip_addr>/api/1/app" -X DELETE -o /dev/null)) is better. I suggest you accept it instead.
0

In your above, you are missing a $ in your first echo, but no matter. You can strip the {"result":true} from the $response string

before the if statement:

response=${response#*\}\ }

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.