Go Back   The UNIX and Linux Forums > Top Forums > Shell Programming and Scripting
Search Forums:



Shell Programming and Scripting Post questions about KSH, CSH, SH, BASH, PERL, PHP, SED, AWK and OTHER shell scripts and shell scripting languages here.

Closed Thread    
 
Thread Tools Search this Thread Display Modes
    #1  
Old 02-16-2009
Registered User
 

Join Date: Oct 2008
Location: Chicagoland
Posts: 4
Thanks: 0
Thanked 0 Times in 0 Posts
Smile 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 ===>
Sponsored Links
    #2  
Old 02-16-2009
cfajohnson's Avatar
Shell programmer, author
 

Join Date: Mar 2007
Location: Toronto, Canada
Posts: 2,829
Thanks: 0
Thanked 82 Times in 76 Posts
Quote:
Originally Posted by muthubharadwaj View Post

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


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

Even better is to use standard syntax:


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

Quote:

Code:
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?[/code]

Where have you output the value of $INPUT? Not in $COMMAND, which is what you are redirecting.
Quote:
[code] 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.


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

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

Sponsored Links
    #3  
Old 02-16-2009
Registered User
 

Join Date: Oct 2008
Location: Chicagoland
Posts: 4
Thanks: 0
Thanked 0 Times in 0 Posts
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.
    #4  
Old 02-16-2009
cfajohnson's Avatar
Shell programmer, author
 

Join Date: Mar 2007
Location: Toronto, Canada
Posts: 2,829
Thanks: 0
Thanked 82 Times in 76 Posts
Quote:
Originally Posted by muthubharadwaj View Post
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.

It doesn't matter whether it is a function or any other command; if it is in a pipeline, it is in a subshell.
Sponsored Links
    #5  
Old 02-16-2009
Registered User
 

Join Date: Oct 2008
Location: Chicagoland
Posts: 4
Thanks: 0
Thanked 0 Times in 0 Posts
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.
Sponsored Links
    #6  
Old 02-16-2009
cfajohnson's Avatar
Shell programmer, author
 

Join Date: Mar 2007
Location: Toronto, Canada
Posts: 2,829
Thanks: 0
Thanked 82 Times in 76 Posts
Quote:
Originally Posted by muthubharadwaj View Post
Is there an alternative to "tee" with "pipeline" to capture outputs and at the same time view on the stdout device?.

Yes; I posted it earlier in the thread.
Sponsored Links
Closed Thread

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

More UNIX and Linux Forum Topics You Might Find Helpful
Thread Thread Starter Forum Replies Last Post
recording command outputs in background jehrome_rando Shell Programming and Scripting 2 02-16-2009 04:13 PM
Shell script outputs different results when commands are entered directly on the CL atilano UNIX for Dummies Questions & Answers 3 02-01-2009 03:46 PM
What command or script to capture a system snapshot? SecureMe Security 1 12-29-2008 05:40 PM
problems with a script that outputs data to a file joeribut Shell Programming and Scripting 1 10-30-2008 12:54 PM
How to store the outputs of a shell script inside a log file??? sunitachoudhury Shell Programming and Scripting 2 03-20-2008 05:45 AM



All times are GMT -4. The time now is 12:41 AM.