bash while loop

Wish I knew how to do this on my own but I've wasted too much time on it as it is.... help! No idea what syntax to use.

I need to make the while condition a curl test to see if I get a result 200.
If I don't, then loop again until I do.
Once I do, then continue on with the script.

#!/bin/bash

while "curl -m 5 -k --key sever.key --cert server.crt -u "user:1234" -s -o /dev/null -w '%{http_code}\n' https://google.com)" ;
                if [  $RES -ne 000 ];then echo "AVAILABLE" ; fi
         do
        sleep 2
    done

echo "would continue with the script from this point on..."
projectsAsked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

woolmilkporcCommented:
"while" must be followed by "do". So just exchange "do" and "if ..."

while <operation>
   do
        if <condition> ; then
        <operation>
        fi
        sleep
   done

Your script has to fill the "RES" variable some way. Perhaps like this:

while RES=$(curl -m 5 -k --key sever.key --cert server.crt -u "user:1234" -s -o /dev/null -w '%{http_code}\n' https://google.com))
 do
   if [  $RES -eq 200 ];then 
       echo "AVAILABLE" ; 
       break
   fi
   sleep 2
done

Open in new window


The while loop will run as long as the curl command (which I didn't check for syntax, by the way) gives a zero return code and the outcome is not equal 200.
Should curl fail or should the result be 200 the loop will terminate and the script will continue after "done".
To check whether the loop ended due to "curl" having failed or due to RES being 200 best repeat the test for "$RES -eq 200" after "done" and react accordingly.
0
woolmilkporcCommented:
If you want to run the loop until you're getting a 200 regardless of whether curl fails or not try this:

while :
  do
   RES=$(curl -m 5 -k --key sever.key --cert server.crt -u "user:1234" -s -o /dev/null -w '%{http_code}\n' https://google.com))
   if [  $RES -eq 200 ];then 
       echo "AVAILABLE" ; 
       break
   fi
   sleep 2
done

Open in new window


":" means "no operation" which always returns "true", so the loop will run until the break condition ("200") is met.
0
TintinCommented:
With a while loop, you can do:

while [ $(curl -m 5 -k --key sever.key --cert server.crt -u "user:1234" -s -o /dev/null -w '%{http_code}\n' https://google.com) -ne 200 ]
do
     sleep 2
done

Open in new window


or you can reverse the logic with an until loop:

until [ $(curl -m 5 -k --key sever.key --cert server.crt -u "user:1234" -s -o /dev/null -w '%{http_code}\n' https://google.com) -eq200 ]
do
     sleep 2
done

Open in new window

0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
Cloud Class® Course: MCSA MCSE Windows Server 2012

This course teaches how to install and configure Windows Server 2012 R2.  It is the first step on your path to becoming a Microsoft Certified Solutions Expert (MCSE).

woolmilkporcCommented:
I assumed that the RES variable is needed again later in the script and that issuing the "Available" message is mandatory .
0
projectsAuthor Commented:
Basically, I just don't want the rest of the script to run until I can see the server and get a curl 200 result back.
To just keep trying until I see a 200 then continue on with the script.

Currently, I am using something like this;

#!/bin/bash

    while [[ $(date "+%Y") -lt 2000 ]] ;
         do
        sleep 10
    done

This simply lets me know once my device time was updated, which means I now have internet access.
0
woolmilkporcCommented:
All of our solutions will do what you desire.
0
projectsAuthor Commented:
I'm not sure which one is what I need then.

I do need to know that I am getting a 200 back because that confirms that the script is able to see the target before continuing on.
0
woolmilkporcCommented:
If you don't need to see the "Available" message and if you want the loop to run until curl returns a  numeric 200 value you can use one of Tintin's solutions, they're equivalent (at least in bash).
0
projectsAuthor Commented:
/go: line 3: [: 000: unary operator expected
not seeing some site yet so try again until I see it
./go: line 3: [: 000: unary operator expected
not seeing some site yet so try again until I see it
./go: line 3: [: 000: unary operator expected

#!/bin/bash

until [ $(curl -s -o /dev/null -w '%{http_code}\n' https://google.com) -eq200 ]
do
     sleep 2

echo "not seeing some site yet so try again until I see it"

done

Echo "now would continue with the script"
0
woolmilkporcCommented:
Put a space between "-eq" and "200"

until [ $(curl -s -o /dev/null -w '%{http_code}\n' https://google.com) -eq 200 ]
do
0
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Shell Scripting

From novice to tech pro — start learning today.

Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.