Solved

bash while loop

Posted on 2014-11-17
10
442 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
ScreenConnect 6.0 Free Trial

Explore all the enhancements in one game-changing release, ScreenConnect 6.0, based on partner feedback. New features include a redesigned UI, app configurations and chat acknowledgement to improve customer engagement!

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

PRTG Network Monitor: Intuitive Network Monitoring

Network Monitoring is essential to ensure that computer systems and network devices are running. Use PRTG to monitor LANs, servers, websites, applications and devices, bandwidth, virtual environments, remote systems, IoT, and many more. PRTG is easy to set up & use.

Question has a verified solution.

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

Suggested Solutions

The following is a collection of cases for strange behaviour when using advanced techniques in DOS batch files. You should have some basic experience in batch "programming", as I'm assuming some knowledge and not further explain the basics. For some…
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…
In a recent question (https://www.experts-exchange.com/questions/28997919/Pagination-in-Adobe-Acrobat.html) here at Experts Exchange, a member asked how to add page numbers to a PDF file using Adobe Acrobat XI Pro. This short video Micro Tutorial sh…

777 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