Unix & Linux Stack Exchange is a question and answer site for users of Linux, FreeBSD and other Un*x-like operating systems. It's 100% free, no registration required.

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

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?

share|improve this question
Is the double colon grep -o 'syncs:: [0-9]+' correct? – Hauke Laging Mar 7 '14 at 1:44
@Hauke:no its not correct.. that's by mistake I have updated that.. Thanks for pointing out.. – lining Mar 7 '14 at 2:18
And somehow my echo statements are not getting printed out at all.. Not sure what could be wrong? – lining Mar 7 '14 at 2:18
up vote 5 down vote accepted

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}')
share|improve this answer
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.. – lining Mar 7 '14 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 '14 at 2:23
Yeah now it works fine.. Can you explain what does E do here? – lining Mar 7 '14 at 2:24
@Webby it tells grep to use ERE instead of BRE regex. – jordanm Mar 7 '14 at 2:41
-E is obviously not needed for [0-9]. It is needed for +. Without -E \+ would be necessary instead. – Hauke Laging Mar 7 '14 at 4:48

Your Answer


By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.