I have a bash script:
#! /bin/bash
someId=$(curl -sk -H -X POST -d "fizzbuzz" "https://someapi.example.com/v1/orders/fire" | jq '.someId')
if [ -z "$someId" ]; then
echo "Order Placement failed; unable to parse someId from the response"
exit 1
fi
echo "...order $someId placed"
When I run this I get the following output:
...order null placed
So somehow $someId is null but then...shouldn't I be seeing the "Order Placement failed; unable to parse someId from the response" echo instead?
How can I modify the if [ -z "$someId" ]; then conditional to execute if $someId is null?
Use the --exit-status option, which makes jq have a non-zero exit status if the last output value is false or null.
#! /bin/bash
if ! someId=$(curl -sk -H -X POST -d "fizzbuzz" "https://someapi.example.com/v1/orders/fire" | jq -r --exit-status '.someId'); then
echo "Order Placement failed; unable to parse someId from the response"
exit 1
fi
echo "...order $someId placed"
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With