zsh-users
 help / color / mirror / code / Atom feed
From: Bart Schaefer <schaefer@brasslantern.com>
To: zsh-users@zsh.org
Subject: Re: set -F kills read -t
Date: Tue, 18 Mar 2014 10:45:05 -0700	[thread overview]
Message-ID: <140318104505.ZM15560@torch.brasslantern.com> (raw)
In-Reply-To: <532872BE.1020408@eastlink.ca>

To add to what PWS just said ...

On Mar 18,  9:22am, Ray Andrews wrote:
}
} ... And the same run of tests is fine. I have no idea how this 'race
} condition' stuff works

"Race condition" is a term referring to what happens when two (or more)
tasks both wish to use the same resource at the same time, but the
resource is only available to one task at a time.  Think of restroom
stalls at a sports stadium at half time; someone ends up waiting, but
you can't tell in advance who will get there first.

In your case the writer is a janitor arriving to refill the paper towel
dispenser, and the reader has just used the sink and need to dry his
hands.  The -t option tells the reader that if the towel dispenser is
empty, he should immediately go away with his hands still wet.  If you
instead use -t 1, he waits 1 second to give the janitor a chance to do
his job.

}    $ echo $s | func0
}    lowercase s to a summer's day?
} 
}    $ echo $S | func0
}    lowercase s to a summer's day?   << WRONG!

The "problem" here is that $input is a global variable, so it remained
set across runs of func0.  In the second case "read -t" failed (if we
had bothered to test $? it would have been 1) so nothing new was put
into $input and the old value persisted.

You could fix this by declaring "local input;" which would cause input
to be reset every time the function is called, but that depends on
whether you want to be able to refer to $input after the function is
finished.

(One could argue that "read" should always erase the parameter to which
it was told to write, no matter whether the action of reading succeeds;
but that's a different conversation.)

} Nothing is more important than predictability.

Not always true.  The point of the -t option is to tell "read" that it
is in fact more important not to wait than it is to be predictable.

I suspect that what you really want is the answer to the question "is
my standard input a pipe?" and to go do something else if it is not.
Using "read -t" gives you an unpredictable answer to that question.

Without more context of what your function is meant to do when the
input is NOT a pipe, we can't tell you the best way to answer that
question, or even whether it's the right question in the first place.

-- 
Barton E. Schaefer


  parent reply	other threads:[~2014-03-18 17:45 UTC|newest]

Thread overview: 29+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2013-12-02 14:26 implementing a control for completing filenames with a defined list of tokens Eric Smith
2013-12-02 15:58 ` Bart Schaefer
2014-03-16 14:13   ` Eric Smith
2014-03-16 19:27     ` Bart Schaefer
2014-03-16 20:13       ` Bart Schaefer
2014-03-18  3:10         ` set -F kills read -t Ray Andrews
2014-03-18  6:50           ` Bart Schaefer
2014-03-18 16:22             ` Ray Andrews
2014-03-18 16:47               ` Peter Stephenson
2014-03-18 17:45               ` Bart Schaefer [this message]
2014-03-18 22:08                 ` Ray Andrews
2014-03-18 23:12                   ` Jan Larres
2014-03-19  4:06                     ` Ray Andrews
2014-03-19  5:30                       ` Jan Larres
2014-03-19 15:23                         ` Ray Andrews
2014-03-19 20:00                           ` Bart Schaefer
2014-03-20  1:47                             ` Ray Andrews
2014-03-19  1:17                   ` Bart Schaefer
2014-03-19  5:00                     ` Ray Andrews
2014-03-19  6:37                       ` Bart Schaefer
2014-03-19 17:08                         ` Ray Andrews
2014-03-19 17:22                           ` Roman Neuhauser
2014-03-19 22:21                           ` Bart Schaefer
2014-03-20  1:46                             ` Ray Andrews
2014-03-20  4:21                               ` Bart Schaefer
2014-03-20 15:49                                 ` Ray Andrews
2014-03-20 16:08                                   ` Bart Schaefer
2014-03-20 21:27                                     ` Ray Andrews
2014-03-19 10:00                       ` Roman Neuhauser

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=140318104505.ZM15560@torch.brasslantern.com \
    --to=schaefer@brasslantern.com \
    --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).