Redirecting output for the entire script


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Redirecting output for the entire script
# 1  
Old 04-25-2013
Redirecting output for the entire script

Hi All,

I am trying to redirect output for every line in a korn shell script that is going to generate output and append it to a log file.

I have been doing this after EACH and every line that is going to produce output:

command 1 >> test.log
command 2 >> test.log
command 3 >> test.log

I would like to have both stderr and stdout appended to the log, and I know there is some easier way doing this than this repeition for every line.
# 2  
Old 04-25-2013
You can use group command { ...; } and append output to file:
Code:
{ command 1; command 2; command 3; } >> test.log

OR
Code:
{
  command 1
  command 2
  command 3 
} >> test.log

This User Gave Thanks to Yoda For This Post:
# 3  
Old 04-25-2013
>> test.log 2>&1 to also redirect stderr.
This User Gave Thanks to MadeInGermany For This Post:
# 4  
Old 04-25-2013
Or, at the top line of the script:

Code:
exec 1>logfile

This will send every line that anything prints into standard ouptut, into the logfile.
# 5  
Old 04-25-2013
There is always the option of simply calling the script with the redirection in place:
Code:
script.sh >>test.log 2>&1

Regards,
Alister
# 6  
Old 04-25-2013
Quote:
Originally Posted by Corona688
Or, at the top line of the script:

Code:
exec 1>logfile

This will send every line that anything prints into standard ouptut, into the logfile.
And to send both stdout and stderr to logfile for all commands in the script that don't otherwise redirect their output, you can use:
Code:
exec >logfile 2>&1

and, of course if you want to append to an existing logfile instead of overwriting it, you can use:
Code:
exec >>logfile 2>&1

Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Redirecting the output

For example, if we run the below command, symcfg list -thin -pool , results in an output most of the times and if the out is generated i'm able to redirect the output to a file. but sometimes it doesnt result any output and even though the output is being redirected, i can see "No Thin Pools "... (2 Replies)
Discussion started by: web2moha
2 Replies

2. Shell Programming and Scripting

Redirecting command output to a file in a shell script

Hello All, I have some unique requirement. I have written a very lengthy script which calls number of resource script to execute a particular task. What I want is output of each command(called from main script and resource scripts) should go to a... (3 Replies)
Discussion started by: anand.shah
3 Replies

3. Shell Programming and Scripting

Redirecting script output to terminal

When ever i started my terminal,Every time I have to change the directory like "cd user/documents/ravi/folder2/folder3" Without typing this entire command every time ,I placed "alias c='cd user/documents/ravi/folder2/folder3'" in .bash_profile file. so that i can able to execute command 'c'... (6 Replies)
Discussion started by: Raviteja saddal
6 Replies

4. UNIX for Dummies Questions & Answers

redirecting script output

Hello, I am interested in taking the output from a script i wrote and using it as input to a different script i wrote. So for example i want to take the output from program2 and use it as a parameter for program1. I didnt think i could use the >> symbols because i think that is just for .txt... (4 Replies)
Discussion started by: GmGeubt
4 Replies

5. UNIX for Dummies Questions & Answers

How to read entire output from a file?

Hello- I am trying to view a file which is quite large. However, whenever I do 'cat (file name)' it shows me just the half.. I am using Putty to access my server. Also, is it possible to edit a file from a unix system on a 'Gedit for Windows" text editor? Thanks (7 Replies)
Discussion started by: DallasT
7 Replies

6. UNIX for Dummies Questions & Answers

redirecting the script output to more than 1 file

Hi, I want to redirect my script output to more than one file without printing the result to the screen. How to do that? ex: echo "hi" >> a.txt b.txt cat a.txt hi b.txt :confused: (2 Replies)
Discussion started by: boopathyvasagam
2 Replies

7. UNIX for Advanced & Expert Users

Read the entire output fired by ps command

Actually I want to display the entire output fired by ps command. My output gets trucated after 80 chars. Thus, i am not able to see the entire command running when i give a ps -eaf .... Does anyone know how do i display the entire output fired by ps command .. (i.e the command along with... (5 Replies)
Discussion started by: vinithepoo
5 Replies

8. Shell Programming and Scripting

redirecting SQL output from within a shell script

Hi all I would like to open a SQL session from within a shell script and then redirect the output of the SQL command to a file. e.g. #!/bin/bash sqlplus "/ as sysdba" <<EOF @$HOME/recovery_space.sql EOF I want to redirect the output of the SQL command to a temp file, because... (2 Replies)
Discussion started by: soliberus
2 Replies

9. Shell Programming and Scripting

Redirecting OUTPUT

Hi, I want to move the output of a command/script to a file as well as to to be displayed on stdout. Can anybody help me in this. Thanks in advace .. -Chanakya M (1 Reply)
Discussion started by: Chanakya.m
1 Replies

10. Shell Programming and Scripting

Redirecting to standard output from within called script

Hi, How to achieve this? Let us assume the following: There are 2 scripts a.ksh and b.ksh $ cat a.ksh sh b.sh 2>&1 >> /work/log/a_log.txt $ cat b.sh echo "abcd" My requirement is, is there a way to display this abcd in standard output also alongside of writing into a_log.txt?... (8 Replies)
Discussion started by: vigneshra
8 Replies
Login or Register to Ask a Question