Reputation: 235
I am creating a shell script(.sh) and trying to read a particular value from the JSON response from a REST call. Below is the sample json response which I get by calling a Rest URL(GET)
{"isTestRunning":false,"status":null}
Here is the logic I am trying to code in shell script. I am completely new to the shell scripting and finding it difficult to script for my requirement. How do I have to start on this? If anyone of you have a documentation link will also be very helpful?
response = curl 'http://test-url/checkTestEnable'
for(till response.isTestRunning is true)
{
response = curl 'http://test-url/checkTestEnable'
if(response.isTestRunning is false){
//curl 'another url'
//break the loop.
}
}
Upvotes: 2
Views: 2778
Reputation: 4749
Below script access the browser stack build api to get the build status
// utility method to log along with timestamp
log() {
echo "[$(date +'%Y-%m-%d %H:%M:%S')] $@"
}
Code to curl the browser stack API and parse the build status
BS_CREDENTIALS="${BS_CREDENTIALS:-}"
BS_BUILD_STATUS_API='https://api.browserstack.com/app-automate/builds?limit=1'
if [ -z "$BS_CREDENTIALS" ]; then
log 1>&2 "Error: BSs_CREDENTIALS not set"
exit 1
fi
log "Connecting to browserstack api to check status"
response=`(curl -u $BS_CREDENTIALS $BS_BUILD_STATUS_API)`
log "API response is `jq -n "$response"`"
buildStatus=$(jq -n "$response" | jq --raw-output '.[0].automation_build.status')
// code block to loop until the buildStatus is not "passed"
while [ "$buildStatus" != "passed" ]; do
log "Job is still running, will check after 30 seconds"
sleep 30
buildStatus=`(curl -u $BS_CREDENTIALS $BS_BUILD_STATUS_API) | jq --raw-output '.[0].automation_build.status'`
buildStatus="running"
done
Upvotes: 0
Reputation: 4148
I would go with jq
for that. jq
is like sed for JSON data.
while [ "$(curl -sS -H 'Accept: application/json' 'http://test-url/checkTestEnable' |
jq '.isTestRunning')" = "true" ]; do
sleep 10
done
curl 'ANOTHER_URL'
If jq
isn't available you can use the following, somewhat ugly regular expression with sed
instead.
while [ "$(curl -sS -H 'Accept: application/json' 'http://test-url/checkTestEnable' |
sed -re 's/.*":(false|true),".*/\1/')" = "true" ]; do
sleep 10
done
curl 'ANOTHER_URL'
BTW: If you can create shell scripts, then you can install jq
, it's just a single binary.
Upvotes: 4
Reputation: 235
As suggested by @JGK the below one worked for me like a magic. Thanks a lot @JGK !
while [ "$(curl -sS -H 'Accept: application/json' 'http://test-url/checkTestEnable' |
sed -re 's/.*":(false|true),".*/\1/')" = "true" ]; do
sleep 10
done
curl 'ANOTHER_URL'
Upvotes: 0
Reputation: 95242
It's a shame you can't install jq
, because it really is the best way to deal with JSON from the shell. @JGK's sed
solution works as long as isTestRunning
is the only boolean in the object returned by the curl
; if that changes, you can complicate the sed
expression a little to make sure that you get the right one, but using regexes to parse JSON quickly gets unruly.
There may be other options depending on what's installed on your system. Python usually ships with its JSON module, for example:
isTestRunning() {
curl -sS -H 'Accept: application/json' 'http://test-url/checkTestEnable' |
python -c 'import json,sys;print(json.load(sys.stdin)["isTestRunning"])' |
grep -q True
}
while isTestRunning; do
sleep 10
done
Upvotes: 2