I'm trying to get a simple while loop working in bash that uses two conditions, but after trying many different syntax from various forums, I can't stop throwing an error. Here is what I have:
while [ $stats -gt 300 ] -o [ $stats -eq 0 ]
I have also tried:
while [[ $stats -gt 300 ] || [ $stats -eq 0 ]]
... as well as several others constructs. I want this loop to continue while $stats is > 300
or if $stats = 0
.
Try:
[
is a call totest
. It is not just for grouping, like parentheses in other languages. Checkman [
orman test
for more information.The extra [ ] on the outside of your second syntax are unnecessary, and possibly confusing. You may use them, but if you must you need to have whitespace between them.
Alternatively:
The correct options are (in increasing order of recommendation):
Some notes:
Quoting the parameter expansions inside
[[ ... ]]
and((...))
is optional; if the variable is not set,-gt
and-eq
will assume a value of 0.Using
$
is optional inside(( ... ))
, but using it can help avoid unintentional errors. Ifstats
isn't set, then(( stats > 300 ))
will assumestats == 0
, but(( $stats > 300 ))
will produce a syntax error.