0

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".

5
  • Instead of "$100" should use use 100? Commented Sep 4, 2018 at 20:25
  • @ExplosionPills removing the " " did not work. Commented Sep 4, 2018 at 20:38
  • BTW, all-caps names are used for the names of variables meaningful to the shell itself and POSIX-specified tools, whereas lowercase names are guaranteed safe for application use. See pubs.opengroup.org/onlinepubs/9699919799/basedefs/… Commented Sep 4, 2018 at 22:09
  • Is SAVE_TO supposed to demonstrate why you want to keep TMP? Why not echo ",$i,$time,$status" if you want to reconstruct the items? Commented Sep 5, 2018 at 14:05
  • ...btw, >>anything repeated in your script is a code smell -- much more efficient to just open a file once and reuse that open handle whenever you want to write another line instead of re-opening it over and over. Commented Sep 5, 2018 at 14:06

2 Answers 2

1

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
Sign up to request clarification or add additional context in comments.

8 Comments

@hummingbirdtarry, the above code temporarily sets IFS only for the duration of the read command, to tell it how to distinguish where one field ends and the next one begins. I don't know what you mean referring to a "TMP command". See BashFAQ #1 for a discussion of using read, explaining the meaning of IFS and the importance of -r.
@hummingbirdtarry, ...also, to better understand why we're using the <(...) syntax here, see BashFAQ #24, which describes what happens when trying to pipe directly to read without it.
I've updated my current code in the question. I am trying to apply IFS to the TMP value, but cannot find a way to set the field separator without interfering with the TMP command.
Why are you unwilling to change the assignment to TMP?
...you can certainly run IFS=, read -r time status <<<"$TMP", but the "why" question stands.
|
0

You can evaluate each "element" individually like this

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

1 Comment

I don't condone using string-splitting -- it's responsible for most of the bugs in BashPitfalls -- but if you are going to advise it, at minimum you can use bash-builtin string substitution: for element in ${TMP//,/ }; do is much more efficient than spinning up a subshell, setting up a pipeline in it, and running an external command such as sed inside that pipeline. See wiki.bash-hackers.org/syntax/pe, or BashFAQ #100.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.