git/list[1] front-page[2] threads[3] people[4] search[5] about
 

Re: [PATCH v2] test_cmp: diagnose incorrect arguments

From
Eric Sunshine <sunshine@sunshineco.com>
Date
Aug 11, 2020, 19:25 UTC
Message-ID
<CAPig+cSroWZEoOL78COmPS4rkvKLE-yCiqh6Part+5gUgVon+A@mail.gmail.com>
In-Reply-To
<20200811183258.GB33865@syl.lan>
On Tue, Aug 11, 2020 at 2:33 PM Taylor Blau <me@ttaylorr.com> wrote:
Show 6 quoted lines
> On Sun, Aug 09, 2020 at 01:42:09PM -0400, Eric Sunshine wrote:
> > +             test "x$2" = x- || test -e "$2" || BUG "test_cmp '$2' missing"
>
> Not related to your patch, but I've seen this style of "x$1" in a few
> places in test-lib-functions.sh. Why can't this be written as 'test "$1"
> = -'?
Short answer: To prevent 'test' from thinking that the argument is a switch.
Longer answer:

'test' can accept both switches (i.e. "-e") and non-switch arguments. Keep in mind, too, that all the quoting is stripped by the shell _before_ 'test' ever sees its arguments. Let's say that the caller has a filename whose name actually is "-e" and passes that in as $1. So, what does 'test' see?

    test -e = -

Rather than comparing literal string "-e" to literal string "-", it's instead (almost) asking if the file named "=" exists; I say "almost" because it's actually an error since switch -e only accepts one argument, but it's being given two arguments, "=" and "-".

You might say that having a file named "-e" (or similar) is unlikely, however, what is not unlikely is a caller passing "-" for standard-input as $1. In this case, 'test' sees:

    test - = -

which may or may not be an error in a particular implementation of 'test'. Some implementations may understand that "-" is not a valid switch, thus infer that you're actually asking for an equality comparison between arguments, but other implementations may complain either that there is no switch named "-" or that those arguments simply make no sense.

This is why it's a very common idiom in shell programming with 'test' to see "x" prepended, thus ensuring that the argument can't be confused with a switch.

Previous: Taylor BlauNext: Taylor Blau
Message 9 of 20 in “test_cmp: diagnose incorrect arguments”
  1. test_cmp: diagnose incorrect argumentsEric Sunshine, Aug 9, 2020
  2. Shourya ShuklaAug 9, 2020
  3. Eric SunshineAug 9, 2020
  4. test_cmp: diagnose incorrect argumentsEric Sunshine, Aug 9, 2020
  5. Junio C HamanoAug 9, 2020
  6. Eric SunshineAug 9, 2020
  7. Junio C HamanoAug 10, 2020
  8. Taylor BlauAug 11, 2020
  9. Eric SunshineAug 11, 2020
  10. Taylor BlauAug 11, 2020
  11. Jeff KingAug 12, 2020
  12. Eric SunshineAug 12, 2020
  13. Eric SunshineAug 12, 2020
  14. Jeff KingAug 12, 2020
  15. Jeff KingOct 16, 2020
  16. Eric SunshineOct 16, 2020
  17. Junio C HamanoOct 16, 2020
  18. Junio C HamanoOct 16, 2020
  19. Junio C HamanoOct 16, 2020
  20. Eric SunshineOct 17, 2020

Read the whole thread, see it on lore, or plain text.

$ cat FOOTERMessages come from the public archive at lore.kernel.org/git, fetched every hour. The front page is chosen and written each morning by an AI editor and can be wrong; the threads themselves are the record. About and API. For agents: an MCP server at https://gitlist.dev/mcp, and any thread, story or person page as Markdown by adding .md to its URL (or sending Accept: text/markdown). Details in /llms.txt.