hummingbirdtarry
hummingbirdtarry

Reputation: 1

How to add an if then statement to a value with multiple arguments using bash

I am storing 2 values in my TMP location: {time_total} and {http_code}

I want to add an if-then condition that checks for the status. If it is anything other than 100, I want it to print out a line saying "something is wrong". But display nothing if the value is equal to 100.

echo getInfo  >> $SAVE_TO
for i in "${LB[@]}"
   do
        TMP=$(curl  -X GET -sS -w "%{time_total},%{http_code}\n" -H "UNAME:  
$USER" -H "UPASS: $PWD" -H "Content-Type: application/json"  -d '{"propertytosearch":"systemUserName", "systemUserName":"XXXXXXX"}' https://$i/ws/rest/v2/getInfo -o /dev/null)
        echo ,$i,$TMP >> $SAVE_TO
   done

if [[ $http_code != $200 ]]
then
 echo "something wrong with $i"
fi

TMP Output:

1.207,100

If I remove %{time} and only use %{status}, the if-then command works. How would I do it for 2 input values?

I don't necessarily need to check for {time}, but if required, I can have an if condition for time that checks for anything greater than 4.000. It can have the same echo "something is wrong".

Upvotes: 0

Views: 65

Answers (2)

Ardent Technologist
Ardent Technologist

Reputation: 1

You can evaluate each "element" individually like this

for element in ${TMP//,/ }
do
  if [ "$element" -ne 100 ]
  then
    echo Something is wrong.
  fi
done

Upvotes: 0

Charles Duffy
Charles Duffy

Reputation: 295463

To read time and status into two separate variables, you can do the following:

IFS=, read -r time status < <(curl  -X GET -sS -w "%{time},%{status}\n" -H)

...thereafter, you can test them individually:

if [[ $status != 100 ]]; then  # note that $status is safe unquoted only in [[ ]], not [ ]
  echo "Something is wrong"
fi

Upvotes: 1

Related Questions