Solved

bash while loop

Posted on 2014-11-17
10
406 Views
Last Modified: 2014-11-18
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..."
0
Comment
Question by:projects
  • 6
  • 3
10 Comments
 
LVL 68

Expert Comment

by:woolmilkporc
ID: 40448761
"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
 
LVL 68

Expert Comment

by:woolmilkporc
ID: 40448784
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
 
LVL 48

Accepted Solution

by:
Tintin earned 250 total points
ID: 40448815
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
 
LVL 68

Expert Comment

by:woolmilkporc
ID: 40448822
I assumed that the RES variable is needed again later in the script and that issuing the "Available" message is mandatory .
0
 

Author Comment

by:projects
ID: 40448901
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
Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

 
LVL 68

Expert Comment

by:woolmilkporc
ID: 40449239
All of our solutions will do what you desire.
0
 

Author Comment

by:projects
ID: 40450702
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
 
LVL 68

Assisted Solution

by:woolmilkporc
woolmilkporc earned 250 total points
ID: 40450726
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
 

Author Comment

by:projects
ID: 40450853
/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
 
LVL 68

Expert Comment

by:woolmilkporc
ID: 40450880
Put a space between "-eq" and "200"

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

Featured Post

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Background Still having to process all these year-end "csv" files received from all these sources (including Government entities), sometimes we have the need to examine the contents due to data error, etc... As a "Unix" shop, our only readily …
Recently, an awarded photographer, Selina De Maeyer (http://www.selinademaeyer.com/), completed a photo shoot of a beautiful event (http://www.sintjacobantwerpen.be/verslag-en-fotoreportage-van-de-sacramentsprocessie-door-antwerpen#thumbnails) in An…
Learn several ways to interact with files and get file information from the bash shell. ls lists the contents of a directory: Using the -a flag displays hidden files: Using the -l flag formats the output in a long list: The file command gives us mor…
Both in life and business – not all partnerships are created equal. As the demand for cloud services increases, so do the number of self-proclaimed cloud partners. Asking the right questions up front in the partnership, will enable both parties …

911 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

23 Experts available now in Live!

Get 1:1 Help Now