🚀 go-pugleaf

RetroBBS NetNews Server

Inspired by RockSolid Light RIP Retro Guy

Article View: comp.unix.shell
Article #3984

Re: [tricky sh] how to log the stdout/stderr of a function ?

#3984
From: Stephane CHAZELAS
Date: Sat, 17 Nov 2001 10:08
34 lines
2138 bytes
Le 16 Nov 2001 11:04:47 -0800, Gael Trinquart <gaelt@yahoo.com> �crivait :
> I try to log all the outputs of a sh function [ my_func | tee
> my_logfile ].
> 
> my_func is defined and called from my_script and modifies variables
> defined in my_script.
> 
> The problem with 'my_func | tee my_logfile' is that it creates a
> sub-shell where my_func has its own copy of my_script's variables.
> When my_func returns, the subshell exits and everything is logged fine
> BUT the modified variables are, of course, not taken into account.
> 
> How can I log everything from my_func AND be able to modify
> my_script's variables ?

One solution:

f() {
  var=yyyy
  echo bla $1
}
exec 3>&1
{
  exec 4>&1 >&3 3>&- 
  var=zzzz
  f bli >&4
  echo var was set to $var
} | tee logfile
exec 3>&-

The strings displayed may not appear in sequence.

-- 
St�phane
Technical Details
Message-ID: <slrn9vcan7.9h.stephane.chazelas@pcchazelas.free.fr>
Path: arc1-ca.pugleaf.net!archiver1.google.com!news1.google.com!newsfeed.stanford.edu!skynet.be!skynet.be!fr.clara.net!heighliner.fr.clara.net!proxad.net!feeder2-1.proxad.net!news1-2.free.fr!not-for-mail
References: <50c0d466.0111161104.55629336@posting.google.com>

Show Full Headers
Complete Article Headers
X-Google-Language: ENGLISH,ASCII
X-Google-Thread: fc89c,7f27c6718083ef6e
X-Google-Attributes: gidfc89c,public
X-Google-Thread: 1012a6,7f27c6718083ef6e
X-Google-Attributes: gid1012a6,public
X-Google-Thread: 110f2c,7f27c6718083ef6e
X-Google-Attributes: gid110f2c,public
X-Google-ArrivalTime: 2001-11-17 02:08:44 PST
Path: arc1-ca.pugleaf.net!archiver1.google.com!news1.google.com!newsfeed.stanford.edu!skynet.be!skynet.be!fr.clara.net!heighliner.fr.clara.net!proxad.net!feeder2-1.proxad.net!news1-2.free.fr!not-for-mail
From: stephane.chazelas@free.fr (Stephane CHAZELAS)
Newsgroups: comp.unix.shell,comp.unix.programmer,fr.comp.os.unix
Subject: Re: [tricky sh] how to log the stdout/stderr of a function ?
References: <50c0d466.0111161104.55629336@posting.google.com>
Reply-To: Stephane_CHAZELAS@yahoo.fr
Message-ID: <slrn9vcan7.9h.stephane.chazelas@pcchazelas.free.fr>
Mime-Version: 1.0
Content-Type: text/plain; charset=iso-8859-1
Content-Transfer-Encoding: 8bit
User-Agent: slrn/0.9.6.3 (Linux)
Date: 17 Nov 2001 10:08:43 GMT
Lines: 34
Organization: Guest of ProXad - France
NNTP-Posting-Date: 17 Nov 2001 11:08:43 MET
NNTP-Posting-Host: 62.147.72.220
X-Trace: 1005991723 news1-2.free.fr 32069 62.147.72.220
X-Complaints-To: abuse@proxad.net