Grab value from a url

7

I'm trying to make a script in Bash with which I can get a value from a URL. For example, in this url there is part that says "percent", what I need is to grab that number.

I honestly do not have much experience in bash, so any help is welcome.

This is what I have in my script:

#!/bin/bash 
content=$(curl -L url) 
echo $content

It returns the following string:

{"percent":"13.29471501108489","count":1859}

I need to extract the value of percent .

    
asked by Cristian Sanchez 06.04.2016 в 17:04
source

5 answers

2

How about a little bit of Python?

From the terminal:

$ echo '{"percent": "13.29471501108489", "count": 1859}' | python -c "import json, sys; print json.load(sys.stdin)['percent']"
13.29471501108489

In your script it would be:

#!/bin/bash 
content=$(curl -L url) 
echo $content | python -c "import json, sys; print json.load(sys.stdin)['percent']"

If the script is called, for example, percent.sh the result would be:

$ bash percent.sh
13.29471501108489

You do not need anything else, the -c option in Python allows you to execute code from text. Then, I'm just using the json library to parse the string and convert it into a dictionary and the sys library to read from the STDIN:

import json, sys;

Finally I only print the key percent of the dictionary that has been created, all this in one sentence:

print json.load(sys.stdin)['percent']
    
answered by 06.04.2016 в 18:52
1

You can use jq , in your example it would be something like this:

#! /bin/bash
content="{\"percent\":\"13.29471501108489\",\"count\":1859}"
echo $content

This has only been to generate the same response that generates the curl , now we extract the value:

percentValue=$(echo $content | jq ".[\"percent\"]")
echo $percentValue

With this you will get the result you want:

"13.29471501108489"

If you wanted the value without quotes you can add the sentence 'tr:

percentValue=$(echo $content | jq .[\"percent\"] | tr -d '"')

Here is the complete example:

#! /bin/bash
content="{\"percent\":\"13.29471501108489\",\"count\":1859}"
echo $content
percentValue=$(echo $content | jq ".[\"percent\"]")
echo $percentValue

Now a complete example with curl :

#!/bin/bash 
content=$(curl -L url) 
echo $content
percentValue=$(echo $content | jq ".[\"percent\"]")
echo $percentValue

To install jq you can guide here , but if you're on OSX it's as easy as:

brew install jq

In Ubuntu it would be like this:

sudo apt-get install jq
    
answered by 06.04.2016 в 18:48
1

Perl includes the JSON :: PP module (from Perl 5.14) to deal with JSON:

echo $content | perl -MJSON::PP -0777 -e 'print decode_json(<>)->{"percent"}'

JSON :: PP is a JSON implementation in Perl. In CPAN, the JSON :: XS module is available, which implements the same interface as JSON :: PP, but focusing on the correction and speed (it is written in C).

    
answered by 06.04.2016 в 21:16
1

Well, I told you a little, in the end I did it this way:

#!/usr/bin/python
import requests
from sys import exit

threshold = 8
r = requests.get('url') 
rj = r.json()
print rj['data']['percent']
if rj['data']['percent'] > threshold:  print 1
else:  print 0

Well, it works well for me, if it exceeds the threshold, it rolls 1, otherwise, 0, now I just have to see how to integrate it into nagios!

Thanks for everyone's help!

    
answered by 11.04.2016 в 16:02
0

Open the terminal and execute this:

~ $ curl -I https://google.com && echo " Show document info only "

With an exit:

HTTP/1.1 302 Found
Cache-Control: private
Content-Type: text/html; charset=UTF-8
Referrer-Policy: no-referrer
Location: https://www.google.co.ve/?gfe_rd=cr&dcr=0&ei=gIauWsiRGu2FX9_yp6AM
Content-Length: 270
Date: Sun, 18 Mar 2018 15:32:16 GMT
Alt-Svc: hq=":443"; ma=2592000; quic=51303431; quic=51303339; quic=51303335,quic=":443"; ma=2592000; v="41,39,35"

If you want to obtain a specific output data for example the Content-Type executes the following:

echo $(curl -I https://google.com 2>/dev/null | grep "Content-Type" | head -1 | cut -d":" -f2)

And you'll get an exit:

text/html; charset=UTF-8

Now creating a script for this;

~ $ echo '#!/bin/bash'>$PWD'/script.sh' && chmod a+x $PWD'/script.sh'
~ $ echo "open nano to edit the script" && nano $PWD'/script.sh'

Add and save this:

#!/bin/bash 

URL_BASE='https://google.com'
REMOVE=':'

result=$(curl -I $URL_BASE 2>/dev/null | grep "[email protected]" | head -1 | cut -d"$REMOVE" -f2)

echo $result

Well we have the script, we just have to run and see the result:

~ $ ./script.sh Content-Type
~ $ $PWD/./script.sh Location
~ $ sh $PWD/./script.sh Date
~ $ bash $PWD/./script.sh Cache-Control
    
answered by 26.03.2018 в 19:42