how to print script output to screen and file


 
Thread Tools Search this Thread
Top Forums UNIX for Dummies Questions & Answers how to print script output to screen and file
# 1  
Old 04-16-2008
how to print script output to screen and file

Hi all,

I have a script that bulk loads thousands of lines of data. I need to log the output during the execution of the script.

I know I can redirect (">") the output to a file; however, I want the output going to both the screen and the log file.

I thought I could use pipe to pipe the output to a new file but its not working.

For example:
ksh LoadAll.sh > Load.log --> writes only to log
ksh LoadAll.sh | Load.log --> command error


anyone know how this can be done?


thanks
# 2  
Old 04-16-2008
Code:
LoadAll.sh 2>&1 | tee -a Load.log

# 3  
Old 04-16-2008
thanks...it worked great! Smilie
# 4  
Old 04-21-2008
vgersh99

I'm trying to understand the syntax of the command you posted. Can you explain the purpose of the 2>&1?


thank you
# 5  
Old 04-21-2008
The 2>&1 is redirecting stdErr to stdOut. This lets you also record any errors to your logfile as well.
# 6  
Old 01-15-2009
If I run the script from outside then it is working fine
like this, LoadAll.sh 2>&1 | tee -a Load.log

Now I am facing another problem here. Suppose I have a script test.sh
Code:
#!/bin/sh

exec 2>&1 | tee -a test.log
pwd

Now when I run the script like ./test.sh, output is coming to terminal but test.log file is getting created with 0 size.

Please reply if anybody has any suggestion for it.

Last edited by Yogesh Sawant; 05-26-2010 at 03:03 AM.. Reason: added code tags
# 7  
Old 01-15-2009
Outside of the script, all the script's io is piped to 'tee' and so it is only done the one time.

Inside the script you would have to pipe all your commands that might produce output to 'tee' to get your desired result.

Basically in the script you wrote, only the 'exec' command was piped to 'tee' and as a result only it's ouput was "written" to test.log, and therefore you have an empty file.

If you had piped 'pwd' to 'tee' instead you would have gotten output in your log file.
 
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Need output of script on screen and file with correct return status of the called script.

Hi, I am trying to capture logs of the script in the file as well as on the screen. I have used exec and tee command for this. While using exec command I am getting the correct output in the file but, script output is not getting displayed on the screen as it get executed. Below is my sample... (14 Replies)
Discussion started by: Prathmesh
14 Replies

2. Shell Programming and Scripting

How to print and append output of nawk script in commandline and as well into a file?

Hi All, I am working on nawk script, has the small function which prints the output on the screen.Am trying to print/append the same output in a file. Basically nawk script should print the output on the console/screen and as well it should write/append the same result to a file. script :... (3 Replies)
Discussion started by: Optimus81
3 Replies

3. Shell Programming and Scripting

Awk script to run a sql and print the output to an output file

Hi All, I have around 900 Select Sql's which I would like to run in an awk script and print the output of those sql's in an txt file. Can you anyone pls let me know how do I do it and execute the awk script? Thanks. (4 Replies)
Discussion started by: adept
4 Replies

4. Programming

std::cout and gfortran print*, don't output to the screen

I am not sure where to post this other than here. I am trying to figure out why an app gives different output when compiled under Ubuntu 10.10 and CentOS 5.5. I am pretty sure that the issue is that the Cent version has gcc 4.1 installed, while Ubuntu has gcc 4.4. I am trying to print from some... (20 Replies)
Discussion started by: LMHmedchem
20 Replies

5. UNIX for Advanced & Expert Users

Shell Script to compare xml files and print output to a file

All, PLease can you help me with a shell script which can compare two xml files and print the difference to a output file. I have attached one such file for you reference. <Group> <Member ID=":Year_Quad:41501" childCount="4" fullPath="PEPSICO Year-Quad-Wk : FOLDER.52 Weeks Ending Dec... (2 Replies)
Discussion started by: kanthrajgowda
2 Replies

6. Shell Programming and Scripting

awk to compare lines of two files and print output on screen

hey guys, I have two files both with two columns, I have already created an awk code to ignore certain lines (e.g lines that start with 963) as they wou ld begin with a certain string, however, the rest I have added together and calculated the average. At the moment the code also displays... (3 Replies)
Discussion started by: chlfc
3 Replies

7. Shell Programming and Scripting

To parse through the file and print output using awk or sed script

suppose if u have a file like that Hen ABCCSGSGSGJJJJK 15 Cock ABCCSGGGSGIJJJL 15 * * * * * * : * * * . * * * : Hen CFCDFCSDFCDERTF 30 Cock CHCDFCSDHCDEGFI 30 * . * * * * * * * : * * :* : : . The output shud be where there is : and . It shud... (4 Replies)
Discussion started by: cdfd123
4 Replies

8. Shell Programming and Scripting

perl - print to a log file and screen

as the title suggests, i need to print a user message to a log file and the screen using perl. in unix i set up a function using 'tee' like so function Display_Message { echo "$*" | tee -ai $LOGFILE } the following command then obviously displays to the screen and prints to a log... (6 Replies)
Discussion started by: mjays
6 Replies

9. Shell Programming and Scripting

print to screen and to file using awk?!

Hello all!.. does anyone know the syntax to print to the screen and to a file? Im using something like AWK .... print header |tee -a invalid_csv_file ; END {..} ' invalid_csv_file="$invalid_csv_dir_file" but no joy? I get sh:... (2 Replies)
Discussion started by: satnamx
2 Replies

10. UNIX for Dummies Questions & Answers

echo or print to screen and file

I did a search for this topic but I couldn't find it and I was sure I have seen something similar before (hard because I am not sure of the criteria for the keywords) What I was looking for was to be able to echo a message to the screen from a bash.sh script at the same time logging it to a... (2 Replies)
Discussion started by: Shakey21
2 Replies
Login or Register to Ask a Question