Grab value from a url


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:

content=$(curl -L url) 
echo $content

It returns the following string:


I need to extract the value of percent .

asked by Cristian Sanchez 06.04.2016 в 17:04

5 answers


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']"

In your script it would be:

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

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

$ bash

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

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

#! /bin/bash
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:


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
echo $content
percentValue=$(echo $content | jq ".[\"percent\"]")
echo $percentValue

Now a complete example with curl :

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

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

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

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

Open the terminal and execute this:

~ $ curl -I && 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
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 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'/' && chmod a+x $PWD'/'
~ $ echo "open nano to edit the script" && nano $PWD'/'

Add and save this:



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:

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