LinuxQuestions.org
Share your knowledge at the LQ Wiki.
Go Back   LinuxQuestions.org > Forums > Linux Forums > Linux - Newbie
User Name
Password
Linux - Newbie This Linux forum is for members that are new to Linux.
Just starting out and have a question? If it is not in the man pages or the how-to's this is the place!

Notices


Reply
  Search this Thread
Old 06-02-2011, 02:44 AM   #1
vinaytp
Member
 
Registered: Apr 2009
Location: Bengaluru, India
Distribution: RHEL 5.4, 6.0, Ubuntu 10.04
Posts: 704

Rep: Reputation: 55
Output of script to both screen and file


Hi All,

I have a script, I am able to send the output of the script to both the screen and a file as shown below

Code:
./script | tee -a temp.log
I wonder is it possible to achieve same thing by executing some commands inside the script.

As for other users, who execute the script it is not advisable to pipe the output of script with the help of tee command.

Again, keeping |tee at the end of every line of the script doesn't seem to be advisable. I tried with this for couple of lines and it works.

Is it possible to get the output of script to both screen and file by just executing name of the script, with some script tweaking ?

Code:
./script
Thanks,
Vinaya
 
Old 06-02-2011, 05:03 AM   #2
druuna
LQ Veteran
 
Registered: Sep 2003
Posts: 10,532
Blog Entries: 7

Rep: Reputation: 2389Reputation: 2389Reputation: 2389Reputation: 2389Reputation: 2389Reputation: 2389Reputation: 2389Reputation: 2389Reputation: 2389Reputation: 2389Reputation: 2389
Hi,

Is this what you are looking for:
Code:
#!/bin/bash

DelibError()
{
  echo "--------------------------------"
  echo "error output:"
  ls -l NoSuchFile
}

MainFunc()
{
  echo "--------------------------------"
  echo "ls -l output:"
  ls -l

  echo "--------------------------------"
  echo "pwd output:"
  pwd

  DelibError
}

MainFunc 2>&1 | tee -a log.file

exit 0
All the executed commands are inside a main function (MainFunc in the above example). You call the function, redirect stderr to stdout (if needed) and pipe all through tee. As shown in the example you can also use functions inside the main function and the output is shown and written to file.

Hope this helps.
 
1 members found this post helpful.
Old 06-02-2011, 06:43 AM   #3
vinaytp
Member
 
Registered: Apr 2009
Location: Bengaluru, India
Distribution: RHEL 5.4, 6.0, Ubuntu 10.04
Posts: 704

Original Poster
Rep: Reputation: 55
Hello Sir,

Super script. Works superbly !!

I had few command line arguments to the script. I passed them while calling the function

Code:
MainFunc $1 $2 2>&1 | tee -a log.file
Thank you so much !!

Last edited by vinaytp; 06-02-2011 at 06:50 AM.
 
Old 06-02-2011, 06:50 AM   #4
druuna
LQ Veteran
 
Registered: Sep 2003
Posts: 10,532
Blog Entries: 7

Rep: Reputation: 2389Reputation: 2389Reputation: 2389Reputation: 2389Reputation: 2389Reputation: 2389Reputation: 2389Reputation: 2389Reputation: 2389Reputation: 2389Reputation: 2389
You're welcome
 
Old 06-02-2011, 07:59 AM   #5
grail
LQ Guru
 
Registered: Sep 2009
Location: Perth
Distribution: Manjaro
Posts: 9,496

Rep: Reputation: 2867Reputation: 2867Reputation: 2867Reputation: 2867Reputation: 2867Reputation: 2867Reputation: 2867Reputation: 2867Reputation: 2867Reputation: 2867Reputation: 2867
Just to add something a little extra, you can also do the following:
Code:
#!/bin/bash

DelibError()
{
  echo "--------------------------------"
  echo "error output:"
  ls -l NoSuchFile
}

{  
  echo "--------------------------------"
  echo "ls -l output:"
  ls -l

  echo "--------------------------------"
  echo "pwd output:"
  pwd

  echo "--------------------------------"
  echo "Command line parameters:"
  echo $1 $2

  DelibError
} |& tee -a log.file

exit 0
 
  


Reply


Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
Standard script output to screen & -x option output to file BoBeau236 Programming 6 05-10-2011 07:43 AM
Output Redirection - Trying to output to screen and file? helptonewbie Linux - Newbie 7 03-19-2009 07:05 AM
Bash Scripting: Redirect output of entire script to file & screen Kristijan Programming 1 09-11-2008 05:31 AM
redirect screen output to file timbuck Linux - Software 5 12-09-2005 06:57 PM
Log Screen Output to a file sdandeker Linux - Newbie 3 09-17-2003 02:57 AM


All times are GMT -5. The time now is 04:12 AM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration