Solved

bash while loop

Posted on 2014-11-17
10
364 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
What Should I Do With This Threat Intelligence?

Are you wondering if you actually need threat intelligence? The answer is yes. We explain the basics for creating useful threat intelligence.

 
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

6 Surprising Benefits of Threat Intelligence

All sorts of threat intelligence is available on the web. Intelligence you can learn from, and use to anticipate and prepare for future attacks.

Join & Write a Comment

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 …
How to remove superseded packages in windows w60 or w61 installation media (.wim) or online system to prevent unnecessary space. w60 means Windows Vista or Windows Server 2008. w61 means Windows 7 or Windows Server 2008 R2. There are various …
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…
This video discusses moving either the default database or any database to a new volume.

708 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

17 Experts available now in Live!

Get 1:1 Help Now