Re: [PATCH 01/16] check-non-portable-shell: complain about "test" a/-o instead of &&/||

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

 



On Mon, Apr 12, 2021 at 7:09 AM Ævar Arnfjörð Bjarmason
<avarab@xxxxxxxxx> wrote:
> Expand the t/check-non-portable-shell.pl checks to complain about the
> use of "-a" and "-o" to the "test" shell built-in to to mean "and" and
> "or", as opposed to using two "test" invocations with "&&" or "||".
>
> There aren't any portability issues with using that construct that I
> know of, but since Junio expressed a dislike of it in [1] and we've
> currently got no such constructs let's add it to the lint checking. I
> had various in-flight and WIP patches that used this construct.

It's not only Junio's dislike of `-a` and `-o` but also that they have
long been considered obsolescent by POSIX[1]. GNU has likewise warned
against it[2] for a few decades.

[1]: https://pubs.opengroup.org/onlinepubs/9699919799/utilities/test.html#tag_20_128_16
[2]: https://www.gnu.org/savannah-checkouts/gnu/autoconf/manual/autoconf-2.70/html_node/Limitations-of-Builtins.html

> Signed-off-by: Ævar Arnfjörð Bjarmason <avarab@xxxxxxxxx>
> ---
> diff --git a/t/check-non-portable-shell.pl b/t/check-non-portable-shell.pl
> @@ -35,6 +35,7 @@ sub err {
> +       # Portability issues
> @@ -48,6 +49,9 @@ sub err {
> +       # Coding style preferences

Given that these flags are considered obsolescent, thus may someday be
dropped from some implementations, these seem a better fit for the
"portability issues" section than the "coding style preferences"
section. (I'd probably just drop those section titles, though.)

> +       /\btest\s+-[a-z]\s+.*?\s+-a\s+/ and err '"test A && test B" preferred to "test A -a B"';
> +       /\btest\s+-[a-z]\s+.*?\s+-o\s+/ and err '"test A || test B" preferred to "test A -o B"';

These will only match the simplistic forms of `test -X blah` (where
"-X" is some single letter option), but will miss expressions such as
`test "$foo" = bar`. Crafting a regex to match more generally would be
non-trivial, so this simpler match is a reasonable start. Okay.



[Index of Archives]     [Linux Kernel Development]     [Gcc Help]     [IETF Annouce]     [DCCP]     [Netdev]     [Networking]     [Security]     [V4L]     [Bugtraq]     [Yosemite]     [MIPS Linux]     [ARM Linux]     [Linux Security]     [Linux RAID]     [Linux SCSI]     [Fedora Users]

  Powered by Linux