Function output to log file


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Function output to log file
# 1  
Old 05-22-2013
Function output to log file

Hi

I have a shell script that does a whole bunch of things. For the sake of simplicity, assume it runs commands one two and three. What i ultimately need is the ability to display the output of the entire script on the screen as well as capture the output in a file. When I try to do it in the following way, it only captures the output of command three:

command one
command two
command three 2>&1 | tee /tmp/log.txt'

This does not resolve my issue. So I thought of including all commands in a function and then trying to capture the output in a file, like the example given below. This seems to be erroneous

Since I have multiple commands to execute, if I

Func()
{
command one
command two
command three
}

#main
Func 2>&1 | tee /tmp/log.txt'


Is there a way/better way of doing this? Please advice.

Thanks

---------- Post updated at 11:10 AM ---------- Previous update was at 09:49 AM ----------

I found a working solution. After each command that needs to be logged, add 2>&1 | tee -a /tmp/log.txt. It will log on the console and also append it to the log file. Any improvements/more efficient ways of doing it are welcome.Smilie
# 2  
Old 05-22-2013
Code:
 script_name.sh 2>&1 | tee -a log.txt

Outside the script...
# 3  
Old 05-22-2013
If you want to redirect the output for all of the commands in the script, you can use Roozo's suggestion.

If you only want to redirect the output from some of the commands in a script that also does other "stuff", you don't need a function to group the commands and redirect the output for the group. You could use:
Code:
{ command one
command two
command three } 2>&1 | tee /tmp/log.txt

to run the commands in the current shell execution environment or:
Code:
( command one
command two
command three ) 2>&1 | tee /tmp/log.txt

to run the commands in a sub-shell execution environment.
# 4  
Old 05-22-2013
Also:
Code:
( command1 command2 command3 ) > stdout.log 2> stderr.log

Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Shell Script function to use script name for log file output

Hi Team - I"m very new to Shell Scripting so I have a rather novice question. My forte is Windows Batch Scripting so I was just wondering what the Shell Script equivalent is to the DOS command %~n? %~n is a DOS variable that dispayed the script name. For instance (in DOS): REM... (11 Replies)
Discussion started by: SIMMS7400
11 Replies

2. Shell Programming and Scripting

How to take output to log file aswell?

Hi guys, A part of my script involves checking for the md5sum of a file. When I run the script, it displays me the output but I want the same output to be written to a log file aswell. #!/bin/bash LOG=/tmp/deploy.log MD5SUM_ADP=`md5sum /opt/code/ADPWebApp.war | awk '{print $1}'`... (4 Replies)
Discussion started by: Junaid Subhani
4 Replies

3. Shell Programming and Scripting

Displaying log file pattern output in tabular form output

Hi All, I have result log file which looks like this (below): from the content need to consolidate the result and put it in tabular form 1). Intercomponents Checking Passed: All Server are passed. ====================================================================== 2). OS version Checking... (9 Replies)
Discussion started by: Optimus81
9 Replies

4. Shell Programming and Scripting

script to mail monitoring output if required or redirect output to log file

Below script perfectly works, giving below mail output. BUT, I want to make the script mail only if there are any D-Defined/T-Transition/B-Broken State WPARs and also to copy the output generated during monitoring to a temporary log file, which gets cleaned up every week. Need suggestions. ... (4 Replies)
Discussion started by: aix_admin_007
4 Replies

5. Shell Programming and Scripting

To log binary file output to a txt file

Hi, I wrote a small script whose function is to execute the postemsg provided if the threshold breaches. I want to log this postemsg messages to a log file. But I am not able to do. Can someone throw some light on how to log the output of this. I am pasting a snippet of that code. ... (2 Replies)
Discussion started by: dbashyam
2 Replies

6. Shell Programming and Scripting

How to capture output to log file

Hi I have a script that will run multiple unix & sql commands. I want to see the output as well as capture it to a log file for further analysis. Is there an easy way to do that instead of adding "tee -a logfile" on everyline or even on the execute line (i.e. script | tee -s logfile). Thanks (1 Reply)
Discussion started by: nimo
1 Replies

7. UNIX for Dummies Questions & Answers

Redirection of output to a log file

Apologies for the trivial nature of this question but I cannot seem to get a simple re direct to a log file to work Step 1 touch log.txt at -f batch.sh now >> log.txt I am trying to get the batch.sh contents into the log file Manny Thanks (8 Replies)
Discussion started by: JohnCrump
8 Replies

8. UNIX for Dummies Questions & Answers

Output function into file

I have this shell: function AAAA { echo $* > Log1 } # main AAA "TEST" > Log2 ..... I not see the same output in the 2 files ; only Log1 result with the strings $* Log2 is without strings .... Why? (1 Reply)
Discussion started by: ZINGARO
1 Replies

9. Shell Programming and Scripting

redirect output to log file

hello all, I'm invoking the program generate-report using backticks from my perl program and redirecting the output to the log file sge-stderr.log. But when i check the process using ps command it is spawing two processes where the below code is parent process and the program generate-report as... (2 Replies)
Discussion started by: kalyanraj
2 Replies

10. Shell Programming and Scripting

Function output

I am working on a script that will recycle processes in an application identified by entry ids. I am reading a file, and using the first field in the records in file to store the entry ids. The second field is a description. I have two records in this file to test my script. My code to do this... (2 Replies)
Discussion started by: Skyybugg
2 Replies
Login or Register to Ask a Question