Redirecting command output to a file in a shell script


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Redirecting command output to a file in a shell script
# 1  
Old 06-26-2013
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 particular file and I should be able to access that file within my main script (Just want to attach it to mail.). Only output of "echo" command should appear on console.
Is there any way to do this ? I thought of
Code:
"Script"

command but it prints output to console as well.
Another way is to redirect output of each command to file but I have more than 2000 commands in total so that looks tough and something that I want to avoid.
Thank you very much.

Regards,
Anand Shah
# 2  
Old 06-26-2013
Not sure what you are up to... Have you considered using tee command?
# 3  
Old 06-26-2013
Quote:

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 particular file and I should be able to access that file within my main script (Just want to attach it to mail.). Only output of "echo" command should appear on console.
Is there any way to do this ? I thought of

Code:
"Script"

command but it prints output to console as well.
Another way is to redirect output of each command to file but I have more than 2000 commands in total so that looks tough and something that I want to avoid.
Thank you very much.

Regards,
Anand Shah

Hello,

ok I think we have to keep some few things in mind here like file should STOP updating before sending to it mail as attachment, we can put a sleep process for 1 or 2 mins after your script completion. Then we can fetch the file and snd it to mail as an attachment.

For scripts which you want to execute in Bacground or you don't want their output to be shown at screen please use nohup command for same.



Thanks,
R. Singh
# 4  
Old 06-26-2013
try this and let us know if it works ...

script > /dir/log 2>&1
in your script, do
Code:
#! /bin/ksh

admin=admin@some.com

some_command 
some_script 
...
some_command
some_script

if [ -f /dir/log ]
then
      cat /dir/log | mailx -s 'logfile for script is ready' $admin 
else
     echo "script has no entries in /dir/log up to this point" >> /dir/log
fi

exit 0

or ...

you can just create one big function in your script and redirect the output for that function into one log file ...
Code:
#! /bin/ksh

admin=admin@some.com

myfunction(){
    some_command 
    some_script 
    ...
    some_command
    some_script
}

myfunction > /dir/log 2>&1
echo "i'm done"

if [ -f /dir/log ]
then
      cat /dir/log | mailx -s 'logfile for script is ready' $admin
      echo "i am done. report emailed to $admin" 
else
     echo "script has no entries in /dir/log up to this point"
fi

exit 0

Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Programming

Writing a UNIX shell script to call a C function and redirecting data to a .txt file

Hi, I am complete new to C programming and shell scripting. I just wrote a simple C code to calculate integral using trapezoid rule. I am prompting user to pass me No. of equally spaced points , N , upper and lower limit. My code looks as follows so far: #include<stdio.h> #include<string.h>... (2 Replies)
Discussion started by: bjhjh
2 Replies

2. Programming

Redirecting output to new file for command "perldoc perllocal"

Hi, I have to redirect output of the command "perldoc perllocal" to new file which contains all the perl module installed. Currently using perldoc perllocal >> mod_data This does not contain all perl modules installed locally on machine, and each character is doubled. Please... (3 Replies)
Discussion started by: asak
3 Replies

3. Shell Programming and Scripting

Format options while Redirecting output of sql to a file in shell

In my korn shell - I have a sql with say 6 columns whose output i am redirecting to a file and attaching this file while sending a mail. While all this happens correctly, i face issues in the format of this file. my intended output is Column_1 Column_2 Column_3 Column_4 ... (7 Replies)
Discussion started by: qwertyu
7 Replies

4. 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

5. Shell Programming and Scripting

Redirecting command output as well as commands

I have a Bourne Shell script that is normally run as a background job and redirects it's output to a file internally (using exec >>); I use "set -x" to capture each command which provides me with a nice shell execution log if it all goes to pieces. I now also need to be able to also run this as... (4 Replies)
Discussion started by: AncientCoder
4 Replies

6. Shell Programming and Scripting

Redirecting output of a command to a file

Hi We are having a requirement where one shell script, say a.sh (which uses Java and connects to Oracle database using JDBC) keeps on running everytime. I created a wrapper (to check whether a.sh is running and if not then to start it) and scheduled it in the crontab. Now all the output from... (3 Replies)
Discussion started by: ankitgoel
3 Replies

7. Shell Programming and Scripting

problem redirecting output of command to variable

Hi. I'm a newbie in scripting and i have this problem: i want to use the 'fuser' command on a file to tell if it's being accessed (for my purposes: still being written). I want to save the output of the command and later compare with the 'not being used' result. the script: #!/bin/bash... (2 Replies)
Discussion started by: nunovc
2 Replies

8. Shell Programming and Scripting

how to redirect the output of a grep command to a file inside a shell script

hi, i wat to get the output of a grep command in a file. but when i am trying out the same grep command in the unix prompt its working fine.. i am getting the output properly.. but when i am writing the same command inside my shell script , its just creating a new output file with no contents... (11 Replies)
Discussion started by: kripssmart
11 Replies

9. UNIX for Dummies Questions & Answers

how to get the output of a grep command to a file inside a shell script

hi, i wat to get the output of a grep command in a file. but when i am trying out the same grep command in the unix prompt its working fine.. i am getting the output properly.. but when i am writing the same command inside my shell script , its just creating a new output file with no contents... (1 Reply)
Discussion started by: kripssmart
1 Replies

10. 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
Login or Register to Ask a Question