zsh-users
 help / color / mirror / code / Atom feed
From: dom@happygiraffe.net (Dominic Mitchell)
To: "S. Cowles" <scowles@earthlink.net>
Cc: zsh-users@sunsite.dk
Subject: Re: problem testing file descriptor 3
Date: Mon, 11 Apr 2005 07:58:27 +0100	[thread overview]
Message-ID: <20050411065827.GA1819@ppe.happygiraffe.net> (raw)
In-Reply-To: <200504102333.36134.scowles@earthlink.net>

On Sun, Apr 10, 2005 at 11:33:32PM -0700, S. Cowles wrote:
> 
> I am having trouble testing if file descriptor 3 is already opened in the
> calling environment.  If the file descriptor is not open, I would like to
> open it and redirect it to stderr.  The test script I am using is:
> 
> 	#!/bin/zsh
> 	[[ -t 3 ]] &&
> 		{ echo "fd3 is open."; } ||
> 		{ echo "fd3 is not open."; }
> 	echo no1 >&1
> 	echo no2 >&2
> 	echo no3 >&3
> 	exit 0

The -t flag tests to see whether an fd is open *and attached to a
terminal*.  So it's probably no good for what you need.

> If the script is invoked in this manner:
> 	cp /dev/null fd3.out
> 	exec 3>&-
> 	./test 3> fd3.out
> the script reports that fd3 is not open even though fd3 output does correctly
> redirect to the file fd3.out.
> 
> If the script is invoked like so:
> 	exec 3>&-
> 	exec 3> /dev/tty
> 	./test
> the script reports that fd3 is open and all output correctly appears on the
> terminal session.
> 
> If I use a test like this:
> 	echo -n "" >&3 1> /dev/null 2> /dev/null || {
> 		echo "fd3 is not open."
> 		exec 3>&2
> 	}
> error messages are not hidden.
> 
> So, as a recap, if fd3 is already opened when the script is called, I want
> output to redirect as coded in the calling environment.  If fd3 is not
> already opened, I want to redirect fd3 output to an existing, open fd and
> I want any error messages hidden.
> 
> What is a good test to determine if file descriptor 3 is open in the calling 
> environment?  Thanks.

I think that the only way you could do this would be using the stat
plugin.

    % zmodload -i zsh/stat
    % stat -f 3
    stat: 3: bad file descriptor
    % stat -f 3 3</etc/hosts
    device  365
    inode   8285
    mode    33188
    nlink   1
    uid     0
    gid     0
    rdev    32807
    size    177
    atime   1113201458
    mtime   1100873702
    ctime   1100873702
    blksize 4096
    blocks  4
    link

You should be able to make that do what you need.

-Dom


  reply	other threads:[~2005-04-11  6:58 UTC|newest]

Thread overview: 3+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2005-04-11  6:33 S. Cowles
2005-04-11  6:58 ` Dominic Mitchell [this message]
2005-04-11  7:32   ` S. Cowles

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=20050411065827.GA1819@ppe.happygiraffe.net \
    --to=dom@happygiraffe.net \
    --cc=scowles@earthlink.net \
    --cc=zsh-users@sunsite.dk \
    /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).