Reputation: 239
I have a simple shell script where I am doing a cURL and based on the response if empty or not wants to log a flag. I tried null check but it still goes to else part.
Script : test.sh
RESP=`curl -m 600 -X POST -H "Accept: application/json" ......` #curl response
#if [ -z "$RESP" ] && echo "Empty"
if [ ${#RESP[@]} -eq 0 ];
then
echo "APIName=Test1Api, TicketRaised=N"
else
echo "RESPONSE is NOT NULL"
echo -e "APIName=Test1Api, HTTP_STATUS=$HTTP_STATUS, totalTime=$TIME_TAKEN, Response=$RESP, TicketRaised=Y"
fi
Output > (even if Response ie "RESP" if empty it goes into else part)
RESPONSE is NOT NULL
APIName=Test1Api, HTTP_STATUS=[], totalTime=545, Response=[], TicketRaised=Y
Upvotes: 2
Views: 7223
Reputation: 6061
I deduce from your answer that what you call an empty answer is the empty JASON string []
.
So try:
RESP=`curl -m 600 -X POST -H "Accept: application/json" ......` #curl response
if [ $RESP = "[]" ]
then
echo "APIName=Test1Api, TicketRaised=N"
else
echo "RESPONSE is NOT NULL"
echo -e "APIName=Test1Api, HTTP_STATUS=$HTTP_STATUS, totalTime=$TIME_TAKEN, Response=$RESP, TicketRaised=Y"
fi
Be sure that there is no newline char after []
. Otherwise, you will have to adapt the code.
Upvotes: 1