[OpenWrt-Devel] [PATCHv3] ubox: run init script through shellcheck

Rosen Penev rosenp at gmail.com
Sat Apr 18 22:07:51 EDT 2020


On Sat, Apr 18, 2020 at 2:22 AM Kevin 'ldir' Darbyshire-Bryant
<ldir at darbyshire-bryant.me.uk> wrote:
>
>
>
> > On 18 Apr 2020, at 01:56, Rosen Penev <rosenp at gmail.com> wrote:
> >
> > On Fri, Apr 17, 2020 at 1:50 AM <mail at 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.
These are not strings. These are integers.

I did as was asked and shellcheck now complains:

SC2071: > is for string comparisons. Use -gt instead.
>
> 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.
>
>
> _______________________________________________
> openwrt-devel mailing list
> openwrt-devel at lists.openwrt.org
> https://lists.openwrt.org/mailman/listinfo/openwrt-devel

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


More information about the openwrt-devel mailing list