LinuxQuestions.org
Welcome to the most active Linux Forum on the web.
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 04-18-2008, 01:44 PM   #1
Hewson
Member
 
Registered: Feb 2007
Location: /home
Distribution: Kubuntu and CentOS
Posts: 214

Rep: Reputation: 32
redirecting BASH script stdout/stderr from the script itself


Hi all.

I need to have stdout and stderr sent both to stdout and to a file, the file needs to be unique everytime. The unique id is not known until the script is run. (in production the id will be a SQL unique key, but for simplicty sake lets just say its the PID of the script itself)

Contents of helloworld.sh
Code:
#!/bin/bash
 #create outputfile
fileName="hello_world$$.log"
 # this is wrong.  it will only redirect the output of that line.
 # i need something that will work for the rest of the script from here on out.
2>&1 | tee $fileName

echo "hello world"
execution of helloworld.sh
Code:
[hewson@chives test]$ ./helloworld.sh
hello world
[hewson@chives test]$ cat hello_world23186.log
[hewson@chives test]$
 
Old 04-18-2008, 02:23 PM   #2
GrapefruiTgirl
LQ Guru
 
Registered: Dec 2006
Location: underground
Distribution: Slackware64
Posts: 7,594

Rep: Reputation: 556Reputation: 556Reputation: 556Reputation: 556Reputation: 556Reputation: 556
Surely there are Bashier folks out there than I, but I have just done basically like you do, but do it on every line where the output has to go to the log.
But at the start, I would define the log:

LOG=/path/to/filename.log

then just end each line with "2>&1 | tee $LOG"

Another way, would be to (break the script into a little piece, and a big piece) use the little piece to create (or otherwise determine) the logfile name, then either:
1) export it, and run the second half of the script like ./script 2>&1 | tee $EXPORTED_LOGNAME
or
2) the first part catches/creates the logfile name same as above, but then calls the second half itself, from within, directing (tee'ing) all output to logfilename same as above:

# first script
LOG=/path/to/log.log
# call 2nd script:
/script_no2 2>&1 | tee $LOG


Any use? Hope it gives you ideas, at least!

Sasha
 
Old 04-18-2008, 03:06 PM   #3
Hewson
Member
 
Registered: Feb 2007
Location: /home
Distribution: Kubuntu and CentOS
Posts: 214

Original Poster
Rep: Reputation: 32
Quote:
Originally Posted by GrapefruiTgirl View Post
Surely there are Bashier folks out there than I, but I have just done basically like you do, but do it on every line where the output has to go to the log.
But at the start, I would define the log:

LOG=/path/to/filename.log

then just end each line with "2>&1 | tee $LOG"
Sasha,

Thanks for the response.

My script is around 400 lines, that simply won't work for me.

Quote:
Originally Posted by GrapefruiTgirl View Post
Another way, would be to (break the script into a little piece, and a big piece) use the little piece to create (or otherwise determine) the logfile name, then either:
1) export it, and run the second half of the script like ./script 2>&1 | tee $EXPORTED_LOGNAME
or
2) the first part catches/creates the logfile name same as above, but then calls the second half itself, from within, directing (tee'ing) all output to logfilename same as above:

# first script
LOG=/path/to/log.log
# call 2nd script:
/script_no2 2>&1 | tee $LOG
I was thinking something along the same lines, but I was hoping I wouldn't have to do this.

All the same, thanks for the reply!

-Hewson
 
Old 04-18-2008, 03:21 PM   #4
colucix
LQ Guru
 
Registered: Sep 2003
Location: Bologna
Distribution: CentOS 6.5 OpenSuSE 12.3
Posts: 10,509

Rep: Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983
Maybe I'm not a "bashier folk" anyway you can simply use exec to redirect both standard output and standard error of all commands in a script:
Code:
#!/bin/bash
logfile=$$.log
exec > $logfile 2>&1
 
Old 04-18-2008, 03:32 PM   #5
GrapefruiTgirl
LQ Guru
 
Registered: Dec 2006
Location: underground
Distribution: Slackware64
Posts: 7,594

Rep: Reputation: 556Reputation: 556Reputation: 556Reputation: 556Reputation: 556Reputation: 556
@ colucix--

Awesome! we both thank you; I had no idea

(You ARE bashier than I )
 
  


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



Similar Threads
Thread Thread Starter Forum Replies Last Post
shell script: how to send stdout one place & stderr another? BrianK Programming 8 09-21-2007 06:57 AM
Redirecting stdout, stderr to pty0? Rostfrei Linux - General 4 03-20-2007 03:15 AM
Shell script - stdout & stderr to terminal and file jantman Linux - Software 1 12-07-2006 04:34 PM
redirecting stdout to /dev/null and stderr to stdout? Thinking Programming 1 05-18-2006 02:36 AM
redirecting stdout and stderr to a file Avatar33 Programming 4 03-12-2005 07:55 AM

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

All times are GMT -5. The time now is 07:43 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