Still celebrating National IT Professionals Day with 3 months of free Premium Membership. Use Code ITDAY17

x
?
Solved

bash while loop

Posted on 2014-11-17
10
Medium Priority
?
740 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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 1000 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
New benefit for Premium Members - Upgrade now!

Ready to get started with anonymous questions today? It's easy! Learn more.

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

[Webinar] Lessons on Recovering from Petya

Skyport is working hard to help customers recover from recent attacks, like the Petya worm. This work has brought to light some important lessons. New malware attacks like this can take down your entire environment. Learn from others mistakes on how to prevent Petya like worms.

Question has a verified solution.

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

This Windows batch file is useful for organizing image files from a digital camera or other source, but can have many other uses.  It simply renames the file(s) to match their create date.  For example, if you took a picture today at 1:40pm and the …
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…
In a recent question (https://www.experts-exchange.com/questions/29004105/Run-AutoHotkey-script-directly-from-Notepad.html) here at Experts Exchange, a member asked how to run an AutoHotkey script (.AHK) directly from Notepad++ (aka NPP). This video…
Suggested Courses

704 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