0

hi i am writings a small shell script. there i use curl command to call to api. what it return is a status of a scan.

{"status":"14"}

i want to get this status and check if it is less than 100; this is what i have done so far

    a=0

    while [ $a -lt 100 ]
    do

        curlout=$(curl "http://localhost:9090/JSON/spider/view/status/?zapapiformat=JSON&scanId=0");
        echo "$curlout";
       a=`expr $a + 1`
    done

what i want to do is assign that status to $a; how to get read this json to get the value in shell script

3
  • sed -r '/status/s/^.*"([0-9]+).*$/\1/' <<< "$curlout" worked for you? Commented Sep 28, 2017 at 7:13
  • echo '{"status":"14"}' | tr -cd '0-9\n'? Commented Sep 28, 2017 at 7:19
  • 1
    post the output of curl "http://localhost:9090/JSON/spider/view/status/?zapapiformat=JSON&scanId=0". And what is the purpose of incrementing the value of status if it's always static? Commented Sep 28, 2017 at 7:20

2 Answers 2

5

If you need to work with JSON, you should obtain jq:

$ echo '{"status": "14"}' | jq '.status|tonumber'
14

or, less rigorously:

$ echo '{"status": "14"}' | jq -r '.status'
14
Sign up to request clarification or add additional context in comments.

1 Comment

How to set it to a variable
0

If you're sure about the format of the curl output, then it's very simple.

echo "$curlout" | tr -cd '[:digit:]'

From manpage of tr,

   -c, -C, --complement
          use the complement of SET1

   -d, --delete
          delete characters in SET1, do not translate

   [:digit:]
          all digits

So this command removes all characters other than digits.

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.