cashman04
cashman04

Reputation: 1173

While loop help bash

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?

Upvotes: 0

Views: 283

Answers (1)

remram
remram

Reputation: 5203

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

Upvotes: 1

Related Questions