0

I have a while loop that tries to curl a weblink for a term.. I need to add an "or" in to it, so I can count up for a time period, so that if if page never comes up, it just doesn't stall the script indefinitely. Here is what I have, that isn't working.

count=0
while [ $count -lt 5 -o `curl -silent -m 2 10.10.10.10:7001 | grep myterm | wc -l` -lt 1 ]; do 
echo "Waiting for it to start" 
count=$(($count + 1))
sleep 5
done
echo "started"

I know my curl works and returns 1 when the page is up, and the count part works fine, if it loops through 5 times, it ends the while loop and says started(not how I am leaving it, just an example). Yet the first time it runs, it should see that my curl = 1, which is not less than 1, and end the while loop.. What am I doing wrong??

EDIT: To explain why I am doing it this way, so someone can explain why it is bad practice/what to do instead.

I am starting a jboss service. This service can take anywhere from 3 to 6 minutes to start. So I need to do 2 things. Check for the status page to come up(best way to tell when it has successfully started), and, if it takes more than 8 minutes to come up, to stop checking so that it can move on to the next service. Thoughts?

7
  • 1
    You're doing a lot of things "wrong" (understand "not very good practice"). Commented Oct 30, 2013 at 17:07
  • Are you looking for a timeout? stackoverflow.com/questions/687948/… Commented Oct 30, 2013 at 17:09
  • Did you understand that the condition in your while is an or condition? Commented Oct 30, 2013 at 17:12
  • Yes, I need or. I need it to finish the loop if it is able to curl the page and the value returned to be greater than 0 OR if the count reaches 5. I need it so that if either of those conditions is true, the while loop ends. Commented Oct 30, 2013 at 17:15
  • Note that -silent is 4 different curl options, not just requesting silence (which is -s by itself, or --silent with two dashes). The other options you're inadvertently passing are -i, which includes the response headers in the output; -l, which is for getting a directory listing out of an ftp:// URL and makes no sense here; and -e, which specifies a Referer header - in this case, "nt". Commented Oct 30, 2013 at 17:21

1 Answer 1

1

What you're trying to do is keep trying while you haven't yet tried 5 times and it fails. But you didn't write that.

count=0
nb=0
while [ $count -lt 5 ]; do 
  nb=$(curl -silent -m 2 10.10.10.10:7001 | grep myterm | wc -l)
  if [ $nb -ge 1 ]; then
    echo "Started"
    exit 0
  fi
  echo "Waiting for it to start..." 
  count=$(($count + 1))
  sleep 5
done
echo "Didn't start after $count tries"
exit 1
Sign up to request clarification or add additional context in comments.

1 Comment

This worked for me. In the end I just changed my code from -o to -a and it works fine. I guess I was misunderstanding my own conditions. I get it now why and works. Thanks very much.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.