zsh-users
 help / color / mirror / code / Atom feed
From: Bart Schaefer <schaefer@brasslantern.com>
To: Zsh Users <zsh-users@zsh.org>
Cc: Marc Chantreux <eiro@phear.org>
Subject: Re: dangerous behavior of while in zsh 5.6 ?
Date: Wed, 3 Oct 2018 08:28:29 -0700	[thread overview]
Message-ID: <CAH+w=7ZuFKDMBvL7XdSqYXcXy5hrc_zQPK0KsLrTZ1Szrfu6Ow@mail.gmail.com> (raw)
In-Reply-To: <20181003143800.GA9162@prometheus.u-strasbg.fr>

On Wed, Oct 3, 2018 at 7:47 AM Marc Chantreux <eiro@phear.org> wrote:
>
> a end of line after a condition is interpreted as an empty
> true block so:
>
>     while (( i-- ))
>         print $i

This is just a syntax error for me (or prints the PS2 prompt if
interactive), so I'm not sure how you're getting it to do anything.

This would certainly not be something new or different in 5.6.2.

In shell syntax, a newline is the same as a semicolon, so what you've
written should be the same as:

  while (( i-- )); print $i

To make that a complete loop statement you have to follow it with "do
... done" or something in braces or parens.

> should   mean:
>
>     while (( i-- )) {print $i}

No, it should mean

  while { (( i-- )); print $i; }

which will be an infiinte loop (if you ever supply the "do" block,
otherwise it's just a syntax error) because the exit status of the
loop condition { ... } will be that of the print statement, which is
never false.

> so it doesn't behave like the other loops:
>
>     repeat 5 print $[i++]
>     while (( i-- )) print $i

It's actually "repeat" that doesn't behave the same here, because
"repeat" has no test condition to follow the number of repetitions.
Thus

  repeat 5 ; print $[i++]

is

  repeat 5 { ; print $[i++]; }

Conversely the test condition for "while" is defined to be the whole
sequence of commands up to the keyword "do" or (with shortloops) one
of "{" or "(" or "((".  Commands in that sequence may be separated &&
or || or | or semicolon or newline.  This is not a bug, it's the way
the syntax is designed to work.

  reply	other threads:[~2018-10-03 15:29 UTC|newest]

Thread overview: 11+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2018-10-03 14:38 Marc Chantreux
2018-10-03 15:28 ` Bart Schaefer [this message]
2018-10-03 16:25   ` Marc Chantreux
2018-10-03 17:47     ` Daniel Shahaf
2018-10-03 20:47       ` nat() in pure shell Marc Chantreux
2018-10-03 22:08         ` Daniel Shahaf
2018-10-03 23:23           ` Marc Chantreux
2018-10-08 10:38           ` Vincent Lefevre
2018-10-04 15:50         ` Bart Schaefer
2018-10-03 20:54     ` dangerous behavior of while in zsh 5.6 ? Bart Schaefer
2018-10-03 21:06       ` Marc Chantreux

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

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

  git send-email \
    --in-reply-to='CAH+w=7ZuFKDMBvL7XdSqYXcXy5hrc_zQPK0KsLrTZ1Szrfu6Ow@mail.gmail.com' \
    --to=schaefer@brasslantern.com \
    --cc=eiro@phear.org \
    --cc=zsh-users@zsh.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://git.vuxu.org/mirror/zsh/

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 NNTP newsgroup(s).