Zsh Mailing List Archive
Messages sorted by:
Reverse Date,
Date,
Thread,
Author
Re: noclobber overzealous with multios and /dev/stdout
- X-seq: zsh-workers 28316
- From: Mikael Magnusson <mikachu@xxxxxxxxx>
- To: zsh workers <zsh-workers@xxxxxxx>
- Subject: Re: noclobber overzealous with multios and /dev/stdout
- Date: Tue, 5 Oct 2010 17:18:06 +0200
- Dkim-signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=gamma; h=domainkey-signature:mime-version:received:received:in-reply-to :references:date:message-id:subject:from:to:content-type; bh=dkkUN6JOXDlUxZnkkhN1Z4RfHEzjrgpFa7lxxPkYeEo=; b=E55RatY0MOS+cTheWKBpYEx3+cZR0YQHpBQRSeBdBztGt2cMxcFdkk3Cmg2eiYgIgY nX2/VGyjGGCwLPMvZjMisXNcStz/5ITWWydM+RNu6rtDZKVkcxmAKVn4kbMGoDBPRB5G aKlg1/FXf9AV4xw6LNcI8dCO/R+TzgIzKRw+0=
- Domainkey-signature: a=rsa-sha1; c=nofws; d=gmail.com; s=gamma; h=mime-version:in-reply-to:references:date:message-id:subject:from:to :content-type; b=Wz/xWd03LnMXgtJDTqxwiUtbWbNUo0TAmwz3yWH5WR8T2BWSHo3vDrIbbfuYScXeh8 UbPs2ht89oLwz0dsi04H208u5GRe9FuVS70yEB/xTYEOKmGbx/gjBiylrqzT6rnfTBQ1 BzuWy2T5/2CnXiXy/oSYvqPzFc4QeAr1dSSwo=
- In-reply-to: <101005080059.ZM4862@xxxxxxxxxxxxxxxxxxxxxx>
- List-help: <mailto:zsh-workers-help@zsh.org>
- List-id: Zsh Workers List <zsh-workers.zsh.org>
- List-post: <mailto:zsh-workers@zsh.org>
- Mailing-list: contact zsh-workers-help@xxxxxxx; run by ezmlm
- References: <AANLkTi=PxKX2x5M_oseccYWsSgztpWY_n8Lz9gTu=3vE@xxxxxxxxxxxxxx> <101005080059.ZM4862@xxxxxxxxxxxxxxxxxxxxxx>
On 5 October 2010 17:00, Bart Schaefer <schaefer@xxxxxxxxxxxxxxxx> wrote:
> On Oct 4, 11:32pm, Mikael Magnusson wrote:
> }
> } I can't really see the logic here, so I'm guessing it's a bug somewhere:
>
> This is not really a zsh issue; it depends on the implementation of the
> device special files that refer to existing descriptors.
>
> Internally zsh always does these steps:
> (1) Attempt to open the file for exclusive write.
> [If this succeeds, we're done, the file didn't exist before.]
> (2) Open the file for write, but without truncation, then fstat the
> descriptor and close/fail if a regular file.
>
> The special files /dev/stdout and /dev/fd/1 etc. are oddballs in that
> they may appear to be (or not) a regular file depending on how the
> related descriptors were previously opened.
>
> So in these two cases ...
>
> } % rm file
> } % echo test > file > /dev/stdout
> } zsh: file exists: /dev/stdout
> }
> } % rm file
> } % echo test > file > /dev/fd/1
> } zsh: file exists: /dev/fd/1
>
> ... what has happened is that zsh has opened "file" as the standard
> output (fd 1), which changes the meaning of /dev/stdout and /dev/fd/1
> to refer to the regular file "file". This is in turn causes noclobber
> to refuse to truncate them; zsh has no way of knowing that the OS has
> magically created a new reference to a file that zsh itself created
> only a fraction of a second before, it knows only that it may not
> truncate an existing file.
Ah, this is sort of half magical stuff, thanks for explaining :).
> } both 0 and 1 are symlinks to /dev/pts/33
>
> No, they aren't. Left-to-right order is important with multios, as
> it is with descriptor duplication using >&DIGITS.
In case this was unclear, i was referring to /proc/<pid>/fd/0 and 1
there, they are both symlinks pointing to the same target
(/dev/pts/33). When i created a symlink in the current dir to the same
place, i also didn't get the error. Oh, i just realized now, does what
the symlink in /proc points to change during the evaluation of the
redirect operators? I guess they must, in that case i understand what
happens :). Ie after "> file", /proc/self/fd/1 points to "file", while
0 still points to the terminal...
What i was actually trying to do when i encountered this was use
multios to write both to the terminal and a file. I guess what i must
do in that case is first duplicate stdout to a new fd with {myfd}>&1
(i always forget the exact syntax for this), and then > file >&$myfd ?
This came up in the irc channel, someone wanted to redirect stdout to
one place, and stderr both to a file and stdout, or something like
that. (I can probably figure out the exact command on my own if he
returns).
--
Mikael Magnusson
Messages sorted by:
Reverse Date,
Date,
Thread,
Author