Reputation: 2072
I would like to run a command and then check the results for the string "200 OK".
So far, I have the following bash script:
for i in $(seq 1 50)
do
printf "i is: $i\n"
RESULTS="$(curl -i -H "Accept: application/json" -X GET http://localhost/test/store/widget/123 | grep -ne "200 OK")"
if [ $RESULTS -eq 0 ]; then
echo "GET failed with $RESULTS"
break
else
echo "we're good"
fi
done
When I run the above script, I get the following results:
i is: 1
% Total % Received % Xferd Average Speed Time Time Time Current
Dload Upload Total Spent Left Speed
100 85 100 85 0 0 3944 0 --:--:-- --:--:-- --:--:-- 4047
gettest.sh: 8: [: 1:HTTP/1.1: unexpected operator
we're good
i is: 2
% Total % Received % Xferd Average Speed Time Time Time Current
Dload Upload Total Spent Left Speed
100 85 100 85 0 0 3293 0 --:--:-- --:--:-- --:--:-- 3400
gettest.sh: 8: [: 1:HTTP/1.1: unexpected operator
we're good
The output that I get back from running the curl command once looks like this:
HTTP/1.1 200 OK
Date: Tue, 10 Feb 2015 13:21:18 GMT
Server: Apache/2.4.7 (Ubuntu)
X-Powered-By: PHP/5.5.9-1ubuntu4.5
Content-Length: 85
Content-Type: application/json; charset=utf-8
{"status":pass,"widgetinfo":"{\"widgetname\":\"the best widget\",\"price\":\"100.00\"}"}
Based on the above results, I have a few questions:
gettest.sh: 8: [: 1:HTTP/1.1: unexpected operator?
Any tips? Thanks!
Upvotes: 1
Views: 1345
Reputation: 7959
If you don't want curl to print stats use -s
, by the way you can use -f
to exit with non zero in case of error 4xx and 5xx.
Valid request:
curl -sf example.com -o /dev/null ; echo $?
0
404 not found:
curl -sf example.com/test ; echo $?
22
And if you want the error message -S
:
curl -sfS example.com/test ; echo $?
curl: (22) The requested URL returned error: 404 Not Found
22
From the manual:
man curl | grep '\-[Ssf],' -A 3
-f, --fail
(HTTP) Fail silently (no output at all) on server errors. This is mostly done to better enable scripts etc to better deal with failed
attempts. In normal cases when a HTTP server fails to deliver a document, it returns an HTML document stating so (which often also
describes why and more). This flag will prevent curl from outputting that and return error 22.
--
-s, --silent
Silent or quiet mode. Don't show progress meter or error messages. Makes Curl mute.
-S, --show-error
When used with -s it makes curl show an error message if it fails.
So your script could look like this:
RESULTS="$(curl -sSfi -H "Accept: application/json" -X GET http://localhost/test/store/widget/123 -o /dev/null 2>&1)"
if [ $RESULTS -eq 0 ]; then
echo "GET failed with $RESULTS"
break
else
echo "we're good"
fi
Upvotes: 1
Reputation: 3243
You could get the http_code using this command
curl -s -o /dev/null -w "%{http_code}" http://www.wikipedia.org
This command prints in the stdout the http code
Upvotes: 1
Reputation: 212178
RESULTS=$(cmd)
assigns the output of the command to RESULTS, not the value returned by the command. If you want to check that a particular string appears in the output, it is common to use grep (which will consume the output of command, so it will be unavailable):
if cmd | grep -q "some string"; then ...
If you want to check the result, just do:
if cmd; then echo cmd succeeded ...
else echo cmd failed ...
fi
Note that in the latter case, the output of cmd will still be written to the stdout of the script, so you may want to redirect.
Upvotes: 0