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?

cashman04
  • 1,134
  • 2
  • 13
  • 27
  • 1
    You're doing a lot of things "wrong" (understand "not very good practice"). – gniourf_gniourf Oct 30 '13 at 17:07
  • Are you looking for a timeout? http://stackoverflow.com/questions/687948/timeout-a-command-in-bash-without-unnecessary-delay – freitass Oct 30 '13 at 17:09
  • Did you understand that the condition in your while is an _or_ condition? – gniourf_gniourf Oct 30 '13 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. – cashman04 Oct 30 '13 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". – Mark Reed Oct 30 '13 at 17:21
  • this can be done in a single curl op http://linux.die.net/man/1/curl and piped to grep -q myterm && echo success || echo failed – technosaurus Oct 30 '13 at 17:24
  • In any case, `-o` is deprecated and you should use `[ … ] || [ … ]` instead of `[ … -o … ]`. Better yet, use `[[ … || … ]]` in `bash`. – chepner Oct 30 '13 at 17:25

1 Answers1

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
remram
  • 4,805
  • 1
  • 29
  • 42
  • 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. – cashman04 Oct 30 '13 at 17:22