Trouble with tee command to capture script outputs

function GetInput
{
print -n "Input"
read input
export INPUT=$input
}
export COMMAND="GetInput"

$COMMAND
echo "$INPUT"

$COMMAND | tee -a Log.log
echo "$INPUT"

The first one without "tee" works fine. echo "$INPUT" displays the values I type in for input. The second one always shows $INPUT as empty string. Why? Is there any other way to execute a shell function and capture its log output and at the same time display its output on the stdout device too without impacting its actual behavior? As you can see here, although the fucntion exports the variable INPUT, it is not available when the function call returns to the calling script code.

Thanks for any help
Hoping to :confused: ===> :slight_smile:

When posting non-standard scripts, you should indicate which shell they are for.

Even better is to use standard syntax:

GetInput()
{
    printf "Input: "
    read INPUT
}

Where have you output the value of $INPUT? Not in $COMMAND, which is what you are redirecting.

[quote]

 Is there any other way to execute a shell function and capture its log output and at the same time display its output on the stdout device too without impacting its actual behavior? As you can see here, although the fucntion exports the variable INPUT, it is not available when the function call returns to the calling script code.

export does nothing there; export makes variables visible to child processes, not parent processes.

You are executing $COMMAND in a pipeline, and all elements of a pipeline (all but the last in ksh) are executed in a subshell.


GetInput()
{
    printf "Input: "
    read INPUT
}
COMMAND="GetInput"

$COMMAND
printf "%s\n" "$INPUT" | tee -a Log.log

[/indent]

Your comment "all commands except the last are executed in a sub-shell" explains the behavior, which is what I though though was not sure. What exactly is the "last one", the tee command itself, the rightmost one I guess? In my context, the command which I run and pipe the output to using tee is a "shell function" defined within the script - not a script different from the one that calls it. Usually functions run in the same shell as the one in which the script calling them runs. Including pipe seems to make a difference ot the bahavior. Thanks anyway for your time and effort in responding.

It doesn't matter whether it is a function or any other command; if it is in a pipeline, it is in a subshell.

Is there an alternative to "tee" with "pipeline" to capture outputs and at the same time view on the stdout device? Thanks again for the clear explanation, helped me a lot.

Yes; I posted it earlier in the thread.