git@vger.kernel.org mailing list mirror (one of many)
 help / color / mirror / code / Atom feed
From: David Kastrup <dak@gnu.org>
To: git@vger.kernel.org
Subject: Re: [PATCH] Supplant the "while case ... break ;; esac" idiom
Date: Mon, 24 Sep 2007 10:29:57 +0200	[thread overview]
Message-ID: <867imgwjne.fsf@lola.quinscape.zz> (raw)
In-Reply-To: 20070924080134.GA9112@artemis.corp

Pierre Habouzit <madcoder@debian.org> writes:

> On Mon, Sep 24, 2007 at 07:57:31AM +0000, David Kastrup wrote:
>> "David Symonds" <dsymonds@gmail.com> writes:
>> 
>> > On 24/09/2007, David Kastrup <dak@gnu.org> wrote:
>> >> Mike Hommey <mh@glandium.org> writes:
>> >>
>> >> > On Sun, Sep 23, 2007 at 10:42:08PM +0200, David Kastrup wrote:
>> >> >> -while case $# in 0) break ;; esac
>> >> >> +while test $# != 0
>> >> >
>> >> > Wouldn't -ne be better ?
>> >>
>> >> Why?
>> >
>> > Because -ne does a numeric comparison, != does a string comparison,
>> > and it's a numeric comparison happening, semantically speaking.
>> 
>> I don't see the point in converting $# and 0 into numbers before
>> comparing them.  "!=" is quite more readable, and the old code also
>> compared the strings.
>
>   Fwiw $# already is a number.

It isn't.

> Hence test $# -ne 0 is definitely a better test.

/* TEST/[ builtin. */
int
test_builtin (list)
     WORD_LIST *list;
{
  char **argv;
  int argc, result;

  /* We let Matthew Bradburn and Kevin Braunsdorf's code do the
     actual test command.  So turn the list of args into an array
     of strings, since that is what their code wants. */
  if (list == 0)
    {
      if (this_command_name[0] == '[' && !this_command_name[1])
	{
	  builtin_error ("missing `]'");
	  return (EX_BADUSAGE);
	}

      return (EXECUTION_FAILURE);
    }

  argv = make_builtin_argv  (list, &argc);
  result = test_command (argc, argv);
  free ((char *)argv);

  return (result);
}

>   $# != 0 would yield sth like (strcmp(sprintf("%d", argc), "0"))
>   $# -ne 0 would yield sth like (argc != atoi("0")).
>
>   Not that it matters much, but the latter looks better to me.

Not to me.  The code does not support your argument, and all $x
expansions certainly are strings, according to manual and usage.  I
will rework the patch this evening in order to get a commit message
more placable to Junio, and I will at his request remove all of the
(redundant) quoting.  But removing quoting from $# does not turn it
into a number: it remains the same string '0'.  If someone else feels
he should replace all "=" and "!=" tests for "numeric" comparisons
with the unreadable numeric tests, he can go ahead proposing a
separate patch that should not just cover $#.

You can have bash declare numeric variables, but even they are strings
(they just auto-evaluate on assignment):

declare -i nonsense
nonsense="3+$(echo 4)"
echo "$nonsense"

gives 7, even though everything has been "strings" here.

-- 
David Kastrup

  parent reply	other threads:[~2007-09-24  8:30 UTC|newest]

Thread overview: 45+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2007-09-21 21:43 [PATCH] Allow shell scripts to run with non-Bash /bin/sh Eygene Ryabinkin
2007-09-21 23:52 ` Junio C Hamano
2007-09-22  0:05   ` David Kastrup
2007-09-22  0:18     ` Junio C Hamano
2007-09-22  0:21       ` Junio C Hamano
2007-09-22  0:26       ` David Kastrup
2007-09-22  3:54   ` Eygene Ryabinkin
2007-09-22  4:06     ` David Kastrup
2007-09-22  6:58       ` Junio C Hamano
2007-09-22  7:34         ` Vineet Kumar
2007-09-22 11:07         ` David Kastrup
2007-09-22  6:54     ` Junio C Hamano
2007-09-22 21:37       ` Adam Flott
2007-09-22  8:32     ` Junio C Hamano
2007-09-23  8:31       ` Eygene Ryabinkin
2007-09-23  8:59       ` David Kastrup
2007-09-23 19:20         ` Junio C Hamano
2007-09-23 19:33           ` David Kastrup
2007-09-23 20:42             ` [PATCH] Supplant the "while case ... break ;; esac" idiom David Kastrup
2007-09-23 22:20               ` Junio C Hamano
2007-09-24  6:22                 ` David Kastrup
2007-09-24 14:24                   ` David Kastrup
2007-09-24 23:31                     ` Junio C Hamano
2007-09-25  6:13                       ` David Kastrup
2007-09-25  6:29                         ` Junio C Hamano
2007-09-25 10:33                           ` Johannes Schindelin
2007-09-25 10:46                           ` Avi Kivity
2007-09-24  6:05               ` Mike Hommey
2007-09-24  6:26                 ` David Kastrup
2007-09-24  6:30                   ` David Symonds
2007-09-24  7:57                     ` David Kastrup
2007-09-24  8:01                       ` Pierre Habouzit
2007-09-24  8:04                         ` Pierre Habouzit
2007-09-24 10:33                           ` Johannes Schindelin
2007-09-24 11:21                             ` Miles Bader
2007-09-24 11:35                               ` Eygene Ryabinkin
2007-09-24 13:45                                 ` Miles Bader
2007-09-24 13:58                                   ` David Kastrup
2007-09-24 14:04                                   ` Johannes Schindelin
2007-09-24 14:10                                     ` David Kastrup
2007-09-24 14:58                                     ` Miles Bader
2007-09-24 16:24                                     ` Junio C Hamano
2007-09-24 11:39                             ` David Kastrup
2007-09-24  8:29                         ` David Kastrup [this message]
2007-09-22  2:33 ` [PATCH] Allow shell scripts to run with non-Bash /bin/sh Junio C Hamano

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

  List information: http://vger.kernel.org/majordomo-info.html

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=867imgwjne.fsf@lola.quinscape.zz \
    --to=dak@gnu.org \
    --cc=git@vger.kernel.org \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
Code repositories for project(s) associated with this public inbox

	https://80x24.org/mirrors/git.git

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for read-only IMAP folder(s) and NNTP newsgroup(s).