From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: Received: (qmail 24427 invoked from network); 11 Apr 2005 06:58:48 -0000 Received: from news.dotsrc.org (HELO a.mx.sunsite.dk) (130.225.247.88) by ns1.primenet.com.au with SMTP; 11 Apr 2005 06:58:48 -0000 Received: (qmail 38002 invoked from network); 11 Apr 2005 06:58:42 -0000 Received: from sunsite.dk (130.225.247.90) by a.mx.sunsite.dk with SMTP; 11 Apr 2005 06:58:42 -0000 Received: (qmail 13799 invoked by alias); 11 Apr 2005 06:58:33 -0000 Mailing-List: contact zsh-users-help@sunsite.dk; run by ezmlm Precedence: bulk X-No-Archive: yes X-Seq: 8665 Received: (qmail 13780 invoked from network); 11 Apr 2005 06:58:32 -0000 Received: from news.dotsrc.org (HELO a.mx.sunsite.dk) (130.225.247.88) by sunsite.dk with SMTP; 11 Apr 2005 06:58:32 -0000 Received: (qmail 36931 invoked from network); 11 Apr 2005 06:58:32 -0000 Received: from happygiraffe.net (81.6.215.59) by a.mx.sunsite.dk with SMTP; 11 Apr 2005 06:58:29 -0000 Received: from localhost (localhost.happygiraffe.net [127.0.0.1]) by happygiraffe.net (Postfix) with ESMTP id 433D8B88A; Mon, 11 Apr 2005 07:58:28 +0100 (BST) Received: from happygiraffe.net ([127.0.0.1]) by localhost (ppe.happygiraffe.net [127.0.0.1]) (amavisd-new, port 10024) with LMTP id 10323-02; Mon, 11 Apr 2005 07:58:27 +0100 (BST) Received: by happygiraffe.net (Postfix, from userid 1001) id DE869B820; Mon, 11 Apr 2005 07:58:27 +0100 (BST) Date: Mon, 11 Apr 2005 07:58:27 +0100 To: "S. Cowles" Cc: zsh-users@sunsite.dk Subject: Re: problem testing file descriptor 3 Message-ID: <20050411065827.GA1819@ppe.happygiraffe.net> References: <200504102333.36134.scowles@earthlink.net> Mime-Version: 1.0 Content-Type: text/plain; charset=us-ascii Content-Disposition: inline In-Reply-To: <200504102333.36134.scowles@earthlink.net> User-Agent: Mutt/1.5.9i From: dom@happygiraffe.net (Dominic Mitchell) X-Virus-Scanned: amavisd-new at happygiraffe.net X-Spam-Checker-Version: SpamAssassin 3.0.2 on a.mx.sunsite.dk X-Spam-Level: X-Spam-Status: No, score=-2.6 required=6.0 tests=BAYES_00 autolearn=ham version=3.0.2 X-Spam-Hits: -2.6 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