LinuxQuestions.org
Review your favorite Linux distribution.
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Linux Forums > Linux - General
User Name
Password
Linux - General This Linux forum is for general Linux questions and discussion.
If it is Linux Related and doesn't seem to fit in any other forum then this is the place.

Notices


Reply
  Search this Thread
Old 05-19-2006, 07:37 PM   #1
Arodef
Member
 
Registered: Apr 2004
Distribution: Centos, Fedora
Posts: 125

Rep: Reputation: 17
Shell scripting: How to redirect output from within the script itself?


I know I can do standard and error output redirection when I execute a shell script:

Ex: ./myscript.sh &> capture.txt

Would it be possible to add a line to the myscript.sh itself which would redirect the output so if I just type:

./myscript.sh

I would get a file capture.txt? Also, what would the command be from within the script if I wanted the standard output and error output to not only be captured in a file but to also still output to the terminal?

Thanks for any help!
 
Old 05-19-2006, 07:46 PM   #2
zackarya
Member
 
Registered: Jul 2003
Distribution: OpenSuse 10, Debian
Posts: 152

Rep: Reputation: 30
Hi Arodef,
I found this website which I think answers your questions.

http://www.cpqlinux.com/redirect.html

Zackarya
 
Old 05-19-2006, 08:29 PM   #3
zhangmaike
Member
 
Registered: Oct 2004
Distribution: Slackware
Posts: 376

Rep: Reputation: 31
To the beginning of your script, add:
Code:
exec &> capture.txt
for example:
Code:
#!/bin/bash

exec &> capture.txt

echo "This will be piped to capture.txt"
That will redirect all output to the file capture.txt.

As for your second question, I don't think it's possible to have stdout and stderr sent to both the terminal and a file using exec. You can do it from the command line easily enough:

./myscript.sh 2>&1 | tee capture.txt

The link which zackarya provided also discusses the tee command.
 
Old 05-23-2006, 04:21 PM   #4
Arodef
Member
 
Registered: Apr 2004
Distribution: Centos, Fedora
Posts: 125

Original Poster
Rep: Reputation: 17
Thanks for the suggestions. I did some more googling and I think the following should allow you to use exec to capture the stout and sterr to a file and also get terminal output:

Code:
exec &> capture.txt
tail -f capture.txt >/dev/tty7 &
 
Old 05-23-2006, 07:30 PM   #5
spirit receiver
Member
 
Registered: May 2006
Location: Frankfurt, Germany
Distribution: SUSE 10.2
Posts: 424

Rep: Reputation: 33
This will also do the trick (and give you a little more flexibility):
Code:
#! /bin/bash

TARGET="target-file"

# file descriptor 4 prints to STDOUT and to TARGET
exec 4> >(while read a; do echo $a; echo $a >>$TARGET; done)
# file descriptor 5 remembers STDOUT
exec 5>&1

# now STDOUT is redirected
exec >&4
date

# wer're done
exec >&5
echo "Date sent to fd4."
 
  


Reply



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 On
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
Shell Scripting: Getting a pid and killing it via a shell script topcat Programming 15 10-28-2007 02:14 AM
Shell Scripting - use the output of a command as an argument for another timgiffney Programming 6 04-17-2006 11:13 AM
output one page at a time in shell scripting sailu_mvn Linux - Software 2 01-06-2006 02:04 PM
Redirect script output to log window in wxPython wapcaplet Programming 1 07-04-2004 08:59 PM
Scripting : Dual line output into next script as variables facets Programming 6 06-10-2004 02:28 PM

LinuxQuestions.org > Forums > Linux Forums > Linux - General

All times are GMT -5. The time now is 12:51 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
Open Source Consulting | Domain Registration