> On 18 Apr 2020, at 01:56, Rosen Penev <ros...@gmail.com> wrote:
> 
> On Fri, Apr 17, 2020 at 1:50 AM <m...@adrianschmutzler.de> wrote:
>> 
>>> 
>>> -     [ $log_buffer_size -eq 0 -a $log_size -gt 0 ] &&
>>> log_buffer_size=$log_size
>>> -     [ $log_buffer_size -eq 0 ] && log_buffer_size=64
>>> +     [ "$log_buffer_size" -eq 0 ] && [ "$log_size" -gt 0 ] &&
>> 
>> I'm never sure whether a comparison [ "$string" -eq 0 ], i.e. one with 
>> quotes and one without using -eq works as expected in all cases.
>> I typically use [ "$string" = "0" ] instead, but I'm not sure whether that's 
>> effectively just the same.
> Sounds bogus. log_buffer_size and log_size are stated to be uintegers above.
>> 
>> Rest seems fine, despite some similar cases with -eq/-ne below.
> -eq/-ne vs = is a stylistic difference.

I disagree.  ‘= != < >’ are string comparisons, -eq/-ne/gt/lt/ge/le are 
numeric/arithmetic comparisons.

Consider this slightly contrived case where to emphasise the difference between 
string and numeric comparison I compare to ’00’ which is arithmetically zero, 
but not just a simple, lone ‘0’ string.

#!/bin/sh

set -x

[ "$foo" -eq 00 ] && echo Z
[ "$foo" = 00 ] && echo Z
[ $foo -eq 00 ] && echo Z
[ $foo = 00 ] && echo Z

foo=“0"
[ "$foo" -eq 00 ] && echo Z
[ "$foo" = 00 ] && echo Z
[ $foo -eq 00 ] && echo Z
[ $foo = 00 ] && echo Z

foo=0
[ "$foo" -eq 00 ] && echo Z
[ "$foo" = 00 ] && echo Z
[ $foo -eq 00 ] && echo Z
[ $foo = 00 ] && echo Z

The unquoted expansions of $foo in the first block will lead to unknown operand 
errors since $foo expands to nothing.  The quoted $foo in the first block will 
lead to ’sh: out of range’ because “” is not an integer suitable for the 
integer -eq comparison.  A solution:

[ "$foo" ] && [ "$foo" -eq 00 ] && echo Z

In later blocks, because $foo is now set it always expands to something so 
there’s no difference between quoted vs unquoted behaviour (in this example!)  
we’re just into the differences between string vs numeric value comparisons, 
ie. string ‘0’ is not equal to ’00’ but value ‘0’ is equal to ’00'

Maybe that helps.


Attachment: signature.asc
Description: Message signed with OpenPGP

_______________________________________________
openwrt-devel mailing list
openwrt-devel@lists.openwrt.org
https://lists.openwrt.org/mailman/listinfo/openwrt-devel

Reply via email to