Re: Possible bug with tee or variable scope ??



2006-06-27, 17:16(-07), Chris:
It seems that tee can influence the scope of a variable. This script
below shows a variable not changing when tee is used and changing (as
expected) when tee is not used:

#!/bin/ksh

Name="Init"

change_name ()
{
echo " Fn before: $Name."
Name="Changed"
echo " Fn after : $Name."
}

echo "Before tee : $Name."
change_name | tee /dev/null
echo "After tee : $Name!"
change_name
echo "After no tee: $Name."
[...]

What you could do is:

{
{
echo "Before tee : $Name." # goes to stdout
change_name >&3 # goes to tee
echo "After tee : $Name!"
change_name
echo "After no tee: $Name."

} 3>&1 >&4 4>&- | tee some files
} 4>&1

Note that zsh has built in support for the tee functionality:

echo foo >&1 > some-file

outputs foo\n both to stdout and to some-file (setopt nomultios
to disable it). And "echo foo" is not started in a subshell (zsh
spawns a background process that reads the output of "echo foo"
via a pipe and tees it to both stdout and some-file, just as |tee
does)

--
Stéphane
.



Relevant Pages

  • Re: Filesystem at 100% capacity, what files are large?
    ... echo "now finding any directories that are world writable" | tee ... echo making all files under /export/home/davek owned by davek ...
    (comp.unix.solaris)
  • Re: print screen and file
    ... echo "ble message" | tee /tmp/new_file ... I recommend `man tee' ... It appends "root log " to /var/log/mylog.log, ... If you want to echo ...
    (comp.unix.shell)
  • Re: Possible bug with tee or variable scope ??
    ... We see exactly the same behavour with a borne parent as well on ... (the sub shells may be ksh on hp-ux, bash on Linux, borne on Solaris ... And if the tee is spawned off in a subshell ... echo " Fn before: $Name." ...
    (comp.unix.shell)
  • Re: Problem with periodically done scripts
    ... + echo Checking for uids of 0: ... tee /dev/stderr ... echo Checking for passwordless accounts: ...
    (freebsd-questions)