Operating System - HP-UX
1833016 Members
2364 Online
110048 Solutions
New Discussion

Too late to redirect stdout/stderr after script is running??

 
SOLVED
Go to solution
Tim Medford
Valued Contributor

Too late to redirect stdout/stderr after script is running??

I would like to redirect stdout and stderr from within a shell script. I've done this before when "calling" another script:

myscript 1>/tmp/junk 2>&1

But what I'm trying to do in this case is capture the output from the actual running script itself after it has started. Is this possible? For example:

#!/usr/bin/sh
# Sample script

export STDOUT=/tmp/junk
export STDERR=/tmp/junkerror

echo Hello World
exit

I'm just not sure of the syntax or whether it's even possible.

Thanks in advance,
Tim
8 REPLIES 8
Ken Grabowski
Respected Contributor

Re: Too late to redirect stdout/stderr after script is running??

I'm not exactly clear on what you mean by after the script has started! How ever a if your script was:
echo "Hello World" 1>/tmp/junk 2>/tmp/junkerror
I believe you would get what your example was trying for.

The tee command may also give you some more options.
Tim Medford
Valued Contributor

Re: Too late to redirect stdout/stderr after script is running??

Thanks Ken.

I guess I didn't give a very good example. I knew I could redirect the output for that single echo command as you suggested.

Instead of just a single echo command the script actually contains dozens of commands doing a variety of things.

I'm trying to capture all the output from all the commands without individually redirecting all of them.
Gregory Fruth
Esteemed Contributor
Solution

Re: Too late to redirect stdout/stderr after script is running??

You can use "exec 2>&1" to redirect stderr to
stdout at the desired point in your script.
Is that what you want?
TwoProc
Honored Contributor

Re: Too late to redirect stdout/stderr after script is running??

I can think of an ugly way...
for it in a for loop (with one pass).

for i in 1
do
mycommand
nextcommand
another command
yet another command
done > myoutputfile 2>&1

We are the people our parents warned us about --Jimmy Buffett
Tim Medford
Valued Contributor

Re: Too late to redirect stdout/stderr after script is running??

Gregory - Thanks, that's exactly what I needed, I just couldn't figure out the syntax.

exec 1>/tmp/junk.out
exec 2>&1

do other stuff...

Ken Grabowski
Respected Contributor

Re: Too late to redirect stdout/stderr after script is running??

Thats a little different than what I was getting from your submission. There is no way to globally change where stdout and stderr are pointing.

You can of course use other programming techniques to simplify the task. If all the scripts that you are calling use the same redirection, try creating a function that takes the scripts as a parameter to the function. Then run that script from the function and apply the redirection there. That way you program it once and use it many times.

Best of luck!
Bill Hassell
Honored Contributor

Re: Too late to redirect stdout/stderr after script is running??

As Greg suggested, exec is the way to do this. From the man pages for sh-posix and ksh:

"If no arguments are given, the effect of this command is to modify file descriptors as prescribed by the input/output redirection list."

So, you can redirect stdin, stdout and stderr. So for your example:

exec 1>$STDOUT 2>$STDERR

You can also redirect stderr into stdout:

exec 2>&1

Or any of the redirection features of the shell. There is no limit on using exec multiple times with different redirection options.


Bill Hassell, sysadmin
Muthukumar_5
Honored Contributor

Re: Too late to redirect stdout/stderr after script is running??

You can start log files in different mode using exec command.

exec 2>/tmp/err.log 1>/tmp/output.log

Ways more:

a)

(
echo "ok"; # std output command
ls /stand/muthu; # std error command ;)
) 1> /tmp/out.log 2>/tmp/err.log

b)

{
echo "ok"; # std output command
ls /stand/muthu; # std error command ;)
} 1> /tmp/out.log 2>/tmp/err.log

hth.
Easy to suggest when don't know about the problem!