LinuxQuestions.org
Help answer threads with 0 replies.
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > General
User Name
Password
General This forum is for non-technical general discussion which can include both Linux and non-Linux topics. Have fun!

Notices



Reply
 
Search this Thread
Old 08-26-2009, 10:41 AM   #1
jcllings
Member
 
Registered: Jun 2004
Location: Tacoma, WA
Distribution: Ubuntu
Posts: 37

Rep: Reputation: 15
Bash Redirection question


I have a script that writes output to a file using redirection:

exec &> $TEMPFILE

It would be useful if I also had the option to display the information so I was wondering how one might go about that from within a script?

Tried this but doesn't seem to work:

exec &> | tee $TEMPFILE


Jim c.
 
Old 08-26-2009, 10:48 AM   #2
GrapefruiTgirl
Guru
 
Registered: Dec 2006
Location: underground
Distribution: Slackware64
Posts: 7,594

Rep: Reputation: 550Reputation: 550Reputation: 550Reputation: 550Reputation: 550Reputation: 550
echo "---- mounting backup device hda10 --" | tee -a $SUMM $LOG

The above is a line from one of my backup scripts. I use the `tee` tool to make the output echo to the screen, as well as to the $SUMM and $LOG files.

Notice that there is no &> in the expression. Perhaps you could post the exact chunk of code you're working on, in case I'm missing something here, but otherwise, try just piping the `exec` directly via the pipe, into `tee` as I have done.

Sasha
 
Old 08-26-2009, 10:57 AM   #3
jcllings
Member
 
Registered: Jun 2004
Location: Tacoma, WA
Distribution: Ubuntu
Posts: 37

Original Poster
Rep: Reputation: 15
I could do it that way but then I would have to do that for each command that the script uses for output. My objective is to turn it on once and then turn it off later. Currently what is being used is

exec &> $TEMPFILE

Which redirects standard out and standard error to a file until later in the script where it is turned off with

exec 1> /dev/tty
exec 2> /dev/tty

Perhaps what I need is a named pipe or something.
 
Old 08-26-2009, 11:03 AM   #4
colucix
Moderator
 
Registered: Sep 2003
Location: Bologna
Distribution: CentOS 6.5 OpenSuSE 12.3
Posts: 10,509

Rep: Reputation: 1957Reputation: 1957Reputation: 1957Reputation: 1957Reputation: 1957Reputation: 1957Reputation: 1957Reputation: 1957Reputation: 1957Reputation: 1957Reputation: 1957
As far as I know, there is no chance to redirect standard output and standard error to a file and display them simultaneously using just a single exec statement. The process is a little more complicate and involves named pipes. Instead of adventuring in a long explanation, I prefer to point you to this article where the process is explained in detail, step by step.

@GrapefruiTgirl: I assume the OP wants to use "exec >" to redirect the standard output of the whole script without piping to tee every command in the script itself. Nevertheless your advice is good and I use the same quite often in my work.

Cheers!
Alex
 
Old 08-26-2009, 11:04 AM   #5
colucix
Moderator
 
Registered: Sep 2003
Location: Bologna
Distribution: CentOS 6.5 OpenSuSE 12.3
Posts: 10,509

Rep: Reputation: 1957Reputation: 1957Reputation: 1957Reputation: 1957Reputation: 1957Reputation: 1957Reputation: 1957Reputation: 1957Reputation: 1957Reputation: 1957Reputation: 1957
Quote:
Originally Posted by jcllings View Post
Perhaps what I need is a named pipe or something.
Just seen your new post. The answer is yes and you will find the article I linked really useful.
 
Old 08-26-2009, 11:10 AM   #6
GrapefruiTgirl
Guru
 
Registered: Dec 2006
Location: underground
Distribution: Slackware64
Posts: 7,594

Rep: Reputation: 550Reputation: 550Reputation: 550Reputation: 550Reputation: 550Reputation: 550
Yes, I think I understand now, and have another suggestion or two, which again may be "not precisely" what the OP wants, but may work nonetheless. (I haven't read the link provided by colucix, so it may well provide exactly what you want).

1) Look into the `set` command. I believe `set` can be used to set aspects of bash, or things running within bash, including re/direction for the duration of a script or bash instance.

2) Put all the code that you want re/redirected, into ONE script, and then, execute THAT script with ./script | tee 2&>1

3) put your stuff that you want re/directed, into a FUNCTION, or between { } braces, and subsequently re/direct the function call or {braced code} into the `tee` command.

Anyhow, these are just ideas, which work for me in various circumstances, but again, might not do precisely what you want, in your specific case.

If the link provided by colucix does give the answer you need, please enlighten us, so we'll (or for myself anyhow) know in the future

EDIT -- new posts above/below -- hopefully you get the named pipe to do what you want; please show us how, when you get it sorted!

Regards,
Sasha

Last edited by GrapefruiTgirl; 08-26-2009 at 11:12 AM.
 
  


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
BASH I/O Redirection on Solaris? jcllings Solaris / OpenSolaris 7 08-24-2009 03:03 PM
trouble with file redirection in bash jaziniho Programming 2 05-08-2009 10:22 AM
Bash I/O redirection problem R00ts Programming 1 10-26-2006 08:50 PM
bash: socket redirection? Thinking Linux - Software 1 04-15-2006 01:20 PM
Bash redirection and subshells nx5000 Programming 2 11-03-2005 07:13 AM


All times are GMT -5. The time now is 05:49 PM.

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
identi.ca: @linuxquestions
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration