How do I get a string from a curl response for a variable in bash? - bash

The bash script sends a curl. The curl response example is following:
{"code":"2aaea70fdccd7ad11e4ee8e82ec26162","nonce":1541355854942}
I need to get the code "2aaea70fdccd7ad11e4ee8e82ec26162" (without quotes) and use it in the bash script.

Use jq to extract the value from the JSON, and command substitution to capture the output of the command:
code=$(curl ... | jq -r '.code')
The -r (--raw) prints the string directly instead of quoting it as in a JSON.

You can also achieve it by sed command if you don't want to install jq:
json=`curl ...`
code=$(echo "$json" | sed -nE 's/.*"code":"([^\"]*)",".*/\1/p')

Related

(Bash) Pass the variable with jq that includes spaces

Im trying to create a variable using jq, let's say, for example:
firstName=($(curl -s https://www.easports.com/fifa/ultimate-team/api/fut/item | jq -r '.items[].firstName'))
The result I expected is "C. Ronaldo" but it gave me only "C." How can I fix it?
What about using .items[0] and command substitution instead of ($(...)) which is an array and is a subject to word-splitting, hence just the C.:
$ var=$(curl -s 'https://www.easports.com/fifa/ultimate-team/api/fut/item' | jq -r '.items[0].firstName')
$ echo "$var"
C. Ronaldo

Unexpected token error observed while using jq library in shell

I have used the below command, and It will get some substring in attribute value.
skipped=$(echo "$value" | jq -f '.[].output | scan("totalSkipped+: [[:digit:]]+")' | sed 's/"//g' )
I ran this script in shell through Jenkins job. and observed below error message:
/tmp/jenkins7615126817764256878.sh: command substitution: line 30: syntax error near unexpected token `"totalSkipped+: [[:digit:]]+"'
/tmp/jenkins7615126817764256878.sh: command substitution: line 30: `echo "$value" | jq .[].output | scan("totalSkipped+: [[:digit:]]+") | sed 's/"//g' )'
I have the entire json file which is stored in $value variable and echo "$value" returned the content of json file but not sure why its not working in jenkins.
I used the same command in jq online tool but It works as expected.
https://jqplay.org/s/7lBj_kDoB3
I'm using jq-1.6 version.
Can someone help me to resolve this?
skipped=$(jq -r '.[].output | scan("totalSkipped: [[:digit:]]+")' <<<"$value")
The pipeline is jq syntax, so it needs to be inside single quotes so the shell doesn't try to find a separate shell command named scan.
No reason for sed here -- using the -r argument to jq makes it emit raw strings as output, so they don't have syntactic quotes.

parse json using a bash script without external libraries

I have a fresh ubuntu installation and I'm using a command that returns a JSON string. I would like to send this json string to an external api using curl. How do I parse something like {"foo":"bar"} to an url like xxx.com?foo=bar using just the standard ubuntu libraries?
Try this
curl -s 'http://twitter.com/users/username.json' | sed -e 's/[{}]/''/g' | awk -v RS=',"' -F: '/^text/ {print $2}'
You could use tr -d '{}' instead of sed. But leaving them out completely seems to have the desired effect as well.
if you want to strip off the outer quotes, pipe the result of the above through sed 's/(^"\|"$)//g'

sha1sum into the URL of curl using GET method

I'm writing a document for a curl request.
User should enter email and password, and I need to send user's email and sha1sum of the password via GET method.
If that would be right, the command would look like:
curl 'http://example.com/auth?email={EMAIL}&pwd={SHA1SUM(PASSWORD)}'
I know about printf variable | sha1sum, but how should I use it to concatenate to the quoted string of CURL?
Please not that it should stay a ONE line command.
You probably want a command substitution using $(). You should use double quotes on the URL in this case (assuming a $PASSWORD variable):
$ curl "http://example.com/auth?email={EMAIL}&pwd=$(echo "$PASSWORD" | sha1sum | sed -e 's|\w*-$||')"
(Added sed to remove the trailing - from the sha1sum output.)

Using Sed with regular expression to save results into a variable

What I'm trying to do is take user input as a string and parse a section of the string. The results from my regex I want to save into a new variable. Here is what I have so far.
#!/bin/bash
downloadUrl="$1"
pythonFile=echo $downloadUrl | sed '/Python-[\d+.]+tgz/'
echo "$downloadUrl"
echo "$pythonFile"
And here is my result.
sed: 1: "/Python-[\d+.]+tgz/": command expected
You forgot to run the commands in $() to get command substitution. Use:
pythonFile=$(echo $downloadUrl | sed '/Python-[\d+.]+tgz/')
See the manual for more details.
sed '/Python-[\d+.]+tgz/' is incorrect since you need to have a sed command like p and anyway it is only searching not really extracting part of input text.
You can use grep -oP
pythonFile$(grep -oP '/Python-[\d+.]+tgz/' <<< "$downloadUrl")
Or without -P
pythonFile$(grep -o '/Python-[0-9+.]\+tgz/' <<< "$downloadUrl")

Resources