Issue
I want to know if it is possible to reduce this line to a single one
if [[ $(curl --silent --output /dev/null --write-out "%{http_code}" "https://httpbin.org/status/200") -ne 200 ]]; then
exit 1
else
exit 0
fi
If the status code of the request was different from 200, exit with error, if it was successfully, exit with 0
Solution
Since you are exiting in both branches, you might as well just do:
status=$( curl ... )
test "$status" -eq 200
These must be the last lines of the script. If for some reason you have more code in the script that you want to ignore but not delete, just add an exit
. If you want, you can be explicit and write exit $?
but it's not necessary, since exit
with no argument is equivalent.
The script will exit with the status of the last executed command, so if "$status"
is 200, the script exits 0. If it is not 200, it will exit 1. Note that this also generates a comprehensible error message if the curl is somehow munged and status
is set to a non-integer value, while that error message is not visible if you use [[
.
Answered By - William Pursell Answer Checked By - Mary Flores (WPSolving Volunteer)