I am working on a project in which I need to make a url call to one of my server from the bash shell script..


After hitting the above url, I will be getting the below response which I need to parse it and extract value of syncs and syncs_behind

state: READY num_retries_allowed: 3 syncs: 30 syncs_behind: 100 num_rounds: 60 hour_col: 2 day_col: 0 oldest_day_col: 0

Now I need to hit the above url every 10 seconds for a period of 10 minutes and extract the value of syncs and syncs_behind from it and use to validate it with below condition -

syncs > 8
syncs_behind = 0

if the syncs is greater than 8 and syncs_behind = 0, then I will end my shell script with some message saying - "Data has been validated", otherwise I would keep on trying for 10 minute window.. If in that 10 minute window, this doesn't happen I will end the shell script anyway meaning I won't retry again.

So I started with the below code and I saved the file as beat.sh with the below contents –


COUNT=60   #number of 10 second timeouts in 10 minutes

while [[ $COUNT -ge "0" ]]; do

#send the request, put response in variable
DATA=$(wget -O - -q -t 1 http://hostname.domain.com:8080/beat)

#grep $DATA for syncs and syncs_behind
SYNCS=$(echo $DATA | grep -o 'syncs: [0-9]+' | awk '{print $2}')
SYNCS_BEHIND=$(echo $DATA | grep -o 'syncs_behind: [0-9]+' | awk '{print $2}')
echo $SYNCS

#add new values to the sum totals

#verify conditionals
if [[ $SYNCS -gt "8" -a $SYNCS_BEHIND -eq "0" ]]; then exit -1; fi

#decrement the counter
let COUNT-=1

#wait another 10 seconds
sleep 10


And when I am running it as ./beat.sh, I got below error -

./beat.sh: line 23: syntax error in conditional expression
./beat.sh: line 23: syntax error near `-a'
./beat.sh: line 23: `if [[ $SYNCS -gt "8" -a $SYNCS_BEHIND -eq "0" ]]; then exit -1; fi'

Any thoughts what wrong I am doing here?

  • Is the double colon grep -o 'syncs:: [0-9]+' correct? Mar 7, 2014 at 1:44
  • @Hauke:no its not correct.. that's by mistake I have updated that.. Thanks for pointing out..
    – arsenal
    Mar 7, 2014 at 2:18
  • And somehow my echo statements are not getting printed out at all.. Not sure what could be wrong?
    – arsenal
    Mar 7, 2014 at 2:18

1 Answer 1


Bash's [[ does not support -a. One approach is to use [ and otherwise keep things the same:

[ $SYNCS -gt "8" -a $SYNCS_BEHIND -eq "0" ]

For best results with [, though, one should double-quote variables:

[ "$SYNCS" -gt 8 -a "$SYNCS_BEHIND" -eq 0 ]

In the general case though -a and -o should be avoided as the [ command cannot parse its arguments reliably when it receives more than 4 (beside [ and ]). So

[ "$SYNCS" -gt 8 ] && [ "$SYNCS_BEHIND" -eq 0 ]

Alternatively, keep the [[ but use && for -a:

[[ $SYNCS -gt "8" && $SYNCS_BEHIND -eq "0" ]]

Many people prefer [[ over [ because the double-quotes around variables are unnecessary.

But here, if you're going to use ksh/bash/zsh specific syntax, for arithmetic comparison, you might as well write it:

(( SYNCS > 8 && SYNCS_BEHIND == 0 ))

MORE: The grep lines have two issues. As Hauke pointed out, there is a superfluous colon in one. Second, to use the [0-9]+ syntax, both lines need the -E option for extended regex:

SYNCS=$(echo "$DATA" | grep -oE 'syncs: [0-9]+' | awk '{print $2}')
SYNCS_BEHIND=$(echo "$DATA" | grep -oE 'syncs_behind: [0-9]+' | awk '{print $2}')
  • Thanks John.. This worked now.. I am able to run it atleast.. But somehow my echo statements are not printing out anything. echo $SYNCS echo $SYNCS_BEHIND not sure what could be wrong.. Any thoughts? I can see DATA getting printed out..
    – arsenal
    Mar 7, 2014 at 2:11
  • @Webby That the echo commands didn't print anything was a sign that the greps failed to match. See updated answer.
    – John1024
    Mar 7, 2014 at 2:23
  • Yeah now it works fine.. Can you explain what does E do here?
    – arsenal
    Mar 7, 2014 at 2:24
  • @Webby it tells grep to use ERE instead of BRE regex.
    – jordanm
    Mar 7, 2014 at 2:41
  • -E is obviously not needed for [0-9]. It is needed for +. Without -E \+ would be necessary instead. Mar 7, 2014 at 4:48

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .