[Top][All Lists]

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Re: Bug, or am I confused?

From: Chet Ramey
Subject: Re: Bug, or am I confused?
Date: Sun, 10 Jul 2011 21:28:54 -0400
User-agent: Mozilla/5.0 (Macintosh; U; Intel Mac OS X 10.6; en-US; rv: Gecko/20110414 Lightning/1.0b2 Thunderbird/3.1.10

On 7/10/11 6:33 PM, Linda Walsh wrote:
> Andreas Schwab wrote:
>> Linda Walsh <address@hidden> writes:
>>>  if [[ -z "" && ((LINES < 80 )) ]]; then echo foo; fi
>>> (prints nothing)....
>>> What am I missing?
>> "LINES" does not sort before "80".
>> Andreas.
> But it compares == to 66?

As always, Andreas is the soul of brevity.  What he means is that the
conditional expression operators </=/> perform string comparisons.  If
you want arithmetic comparisons, you still have to use -le, -eq, and -gt.

> Normally, in the shell, if you type in something in double parens
> it does a mathematical evaluation.

When you're in a place where you can enter a command name, (( ... )) will
perform arithmetic evaluation equivalent to let "...".  A conditional
expression is not such a place.

> i.e.
> if you are in a while loop

...and entering a command.

> so I'd expect the above to eval my shell-var, 'lines'...
> Ah....I see....I need another set of parens!...
> (ARG....!!....
> What rule should I have remembered to 'know' that?

The rule you need is in the manual.  In a conditional expression parens
only serve to  override normal operator precedence.  Adding extra pairs
of parens doesn't do anything special.

> 'if in [[ ]], then you need an extra level of parens around a double-paren
> expression to get mathematical evaluation'...

This is pretty much nonsense.  The operators and their precedence are in
the manual under CONDITIONAL EXPRESSIONS.  If you want an arithmetic
comparison, use one of the arithmetic comparison operators.  If you want
the operands to those operators to undergo arithmetic expansion, use the
standard $((...)) word expansion.

> I guess many of us are still learning about these things as they unfold --
> and of course Chet keeps ahead of us by continually throwing in more
> benefits...

This hasn't changed in years.  The -lt/-eq/-gt operators were inherited
from test and are archaic.

Since you want to mix conditional and arithmetic comparisons, you might
consider using something like

if [[ -z "" ]] && (( LINES < 80 )); then

``The lyf so short, the craft so long to lerne.'' - Chaucer
                 ``Ars longa, vita brevis'' - Hippocrates
Chet Ramey, ITS, CWRU    address@hidden    http://cnswww.cns.cwru.edu/~chet/

reply via email to

[Prev in Thread] Current Thread [Next in Thread]