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

Re: [PATCH] Supplant the "while case ... break ;; esac" idiom

From
EREygene Ryabinkin <rea-git@codelabs.ru>
Date
Sep 24, 2007, 11:35 UTC
Message-ID
<20070924113556.GI8111@void.codelabs.ru>
In-Reply-To
<87ps08s3zt.fsf@catnip.gol.com>
Miles,
Mon, Sep 24, 2007 at 08:21:42PM +0900, Miles Bader wrote:
Show 10 quoted lines
> > The reason we used "case" is that this has always been a builtin (has to 
> > be, because it changes workflow).
> >
> > Therefore I am somewhat uneasy that the patch went in so easily, 
> > especially given a message that flies in the face of our endeavours to 
> > make git less dependent on any given shell (as long as it is not broken to 
> > begin with).
> 
> The comment "... holds only for a shell where [ is a builtin" doesn't
> make any sense to me

The 'while case ...' construct does not invoke any external commands. The 'while test ...' too, but only when 'test' is builtin. When 'test' is the external binary you get one additional fork/exec per each cycle.

I believe that this trick comes from the old days where people were generally much more eager to save CPU cycles than now ;)) But if you tried to run Cygwin on the moderately new machines (like PIII), you still can notice that commands like 'man' are a bit slow just because they require a bunch of preprocessors (and processes in pipe) to run. So, "save the process, kill the fork/exec" ;))

> though:  "-ne" is a standard operator even if "["
> isn't a builtin.  It's useful if you are testing numbers in potentially
> non-canonical form, e.g., with leading zeroes or something, and AFAIK is
> quite portable.

In general -- yes, it is portable and good for leading zeros. But in the case of $#, there will hardly be leading zeros or something else. And using '!=' typically saves you one atoi() + some other checks, since it typically translates to a bare strcmp().

-- 
Eygene
Previous: Miles BaderNext: Miles Bader
Message 37 of 45 in “Allow shell scripts to run with non-Bash /bin/sh”
  1. Allow shell scripts to run with non-Bash /bin/shEygene Ryabinkin, Sep 21, 2007
  2. Junio C HamanoSep 21, 2007
  3. David KastrupSep 22, 2007
  4. Junio C HamanoSep 22, 2007
  5. Junio C HamanoSep 22, 2007
  6. David KastrupSep 22, 2007
  7. Junio C HamanoSep 22, 2007
  8. Eygene RyabinkinSep 22, 2007
  9. David KastrupSep 22, 2007
  10. Junio C HamanoSep 22, 2007
  11. Vineet KumarSep 22, 2007
  12. David KastrupSep 22, 2007
  13. Junio C HamanoSep 22, 2007
  14. Adam FlottSep 22, 2007
  15. Junio C HamanoSep 22, 2007
  16. Eygene RyabinkinSep 23, 2007
  17. David KastrupSep 23, 2007
  18. Junio C HamanoSep 23, 2007
  19. David KastrupSep 23, 2007
  20. Supplant the "while case ... break ;; esac" idiomDavid Kastrup, Sep 23, 2007
  21. Junio C HamanoSep 23, 2007
  22. David KastrupSep 24, 2007
  23. David KastrupSep 24, 2007
  24. Junio C HamanoSep 24, 2007
  25. David KastrupSep 25, 2007
  26. Junio C HamanoSep 25, 2007
  27. Johannes SchindelinSep 25, 2007
  28. Avi KivitySep 25, 2007
  29. Mike HommeySep 24, 2007
  30. David KastrupSep 24, 2007
  31. David SymondsSep 24, 2007
  32. David KastrupSep 24, 2007
  33. Pierre HabouzitSep 24, 2007
  34. Pierre HabouzitSep 24, 2007
  35. Johannes SchindelinSep 24, 2007
  36. Miles BaderSep 24, 2007
  37. Eygene RyabinkinSep 24, 2007
  38. Miles BaderSep 24, 2007
  39. David KastrupSep 24, 2007
  40. Johannes SchindelinSep 24, 2007
  41. David KastrupSep 24, 2007
  42. Miles BaderSep 24, 2007
  43. Junio C HamanoSep 24, 2007
  44. David KastrupSep 24, 2007
  45. David KastrupSep 24, 2007

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.