Visit The New, Modern Unix Linux Community


Trouble with tee command to capture script outputs


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Trouble with tee command to capture script outputs
# 1  
Bug 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 Smilie ===> Smilie
# 2  
Quote:
Originally Posted by muthubharadwaj
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

# 3  
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  
Quote:
Originally Posted by muthubharadwaj
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.
# 5  
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.
# 6  
Quote:
Originally Posted by muthubharadwaj
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.

Previous Thread | Next Thread
Thread Tools Search this Thread
Search this Thread:
Advanced Search

Test Your Knowledge in Computers #691
Difficulty: Medium
UnixWare is a Unix operating system originally released by Univel.
True or False?

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Need command to capture word from shell script and send email

Hello Experts, Greeting to all of you. I have a requirement, that we have a shell script status.sh that check the status of server and server status shows as status.sh Enterprise Server - Running Admin Server - Shutdown Requirement is like whenever the output shows shutdown it should... (2 Replies)
Discussion started by: aks_1902
2 Replies

2. Shell Programming and Scripting

Question about tee command

I have the following script as shown below where I cat a file and then also tee the output to a file as I have to email the execution of the process to users at the end of the script: cat incoming.dat | tee -a execution.log if then echo "Issue with incoming.dat file, file not... (5 Replies)
Discussion started by: calredd
5 Replies

3. Shell Programming and Scripting

Help with tee command

In the current directory , I have seven files . But when I use the following command , it lists eight files ( 7 files + file_list.xtx) ls -1 | tee file_list.xtx | while read line; do echo $line ; done Does the tee command create the file_list.xtx file first and then executes the ls -1... (1 Reply)
Discussion started by: kumarjt
1 Replies

4. UNIX for Dummies Questions & Answers

Problem using tee to capture activity - nothing happens

Hi, I'm sure this is real simple but in the looking I've done I haven't seen it addressed (or didn't realize it). I've started up a PuTTY connection to our UNIX box and started screen and script - no problems. Then I typed "tee teeKH18.txt" expecting it to start tee and dump all activity into... (2 Replies)
Discussion started by: Mike Welch
2 Replies

5. Shell Programming and Scripting

Capture IP and command type in linux script

hi guys, is there any way to capture the ip address of users who log-in to linux then capture the command executed together with the time and date? example output 192.1.1.1 : ls -ltr Aug 6 16:38:40thanks in advance. (2 Replies)
Discussion started by: d3xt3r
2 Replies

6. UNIX for Advanced & Expert Users

Equivalents of tee command to find exit status of command

Hi, Want to log the output of command & check the exit status to find whether it succeeded or failed. > ls abc ls: abc: No such file or directory > echo $? 1 > ls abc 2>&1 | tee log ls: abc: No such file or directory > echo $? 0 Tee commands changes my exit status to be always... (7 Replies)
Discussion started by: vibhor_agarwali
7 Replies

7. Shell Programming and Scripting

tee + more command

script1: #!/bin/ksh more test.txt script2: calling the script1 #!/bin/ksh /tmp/script1.sh 2>&1 | tee tee.log where test.txt contains ~1200 lines. When I execute the script2 the more command does not print pagewise it goes to the end of the line, when I remove the tee command it... (4 Replies)
Discussion started by: prasad111
4 Replies

8. Shell Programming and Scripting

create outputs from other command outputs

hi friends, The code: i=1 while do filename=`/usr/bin/ls -l| awk '{ print $9}'` echo $filename>>summary.csv #Gives the name of the file stored at column 9 count=`wc -l $filename | awk '{print $1}'` echo $count>>summary.csv #Gives just the count of lines of file "filename" i=`expr... (1 Reply)
Discussion started by: rajsharma
1 Replies

9. UNIX and Linux Applications

Tee with pipe command.

cat work.txt M|324324|32424|3431 M|324324|32424|3431 N|324324|32426|3432 N|324324|32424|3434 M|324324|32424|3435 cat work.txt | tee $( grep '^M' > m.txt ) | $( grep '^N' > n.txt ) cehpny00:/home01/sr38632 $ cat m.txt M|324324|32424|3431 M|324324|32424|3431 M|324324|32424|3435 ... (2 Replies)
Discussion started by: rsampathy
2 Replies

10. Cybersecurity

What command or script to capture a system snapshot?

Some background on what I am trying to accomplish - Accreditation/Certification for DoD (Unix/Linux) system: I am trying to improve the process for capturing key system information in preparation for performing a formal security review of a Unix or Linux system. This is in addition to the SRR... (1 Reply)
Discussion started by: SecureMe
1 Replies

Featured Tech Videos