bash - while with multiply conditions lose one of that

Viewed 30

ill try to make loop with 2 conditionals, x != 0 OR curl = success, curl module working fine, but script lose [[ x -eq 0 ]]

#!/bin/bash
set -x
x=8
while [[ $x -eq 0 ]] || [[ "$(curl -s -o /dev/null -w ''%{http_code}'' http://nexus_url/path/96."$x"/latest/version.sign)" != "200" ]]
do
  ((x--))
done
echo $x

when we try to use script like this

#!/bin/bash
set -x
x=8
while [[ $x -eq 0 ]] 
do
  ((x--))
done
echo $x

its succesfully stop on x=0

url checking also work, but cycle become infinity when we try use it together and no one url checking was succesfull, i think problem is in || between conditions, how i can correctly use OR in bash conditions?

1 Answers

Your conditions should be like this:

#!/bin/bash
set -x
x=8
while [[ $x -gt 0 ]] && [[ "$(curl -s -o /dev/null -w ''%{http_code}'' http://nexus_url/path/96."$x"/latest/version.sign)" != "200" ]]
do
  ((x--))
  sleep .1
done

echo $x

Which is essentially this pseudo code:

x=8
while (x > 0 && curl_cmd != SUCCESS) {
   x--;
   skeep .1
}

This will attempt to run curl 8 times. It will come out of loop when x==0 or curl returns a success.

Related