redirecting stdout from within ksh script

Is it possible to execute an intruction at the top of a ksh script to redirect any further standard output to a file? I want to avoid having to create another script which calls the main script with redirection.
yppirt090497Asked:
Who is Participating?
 
aditya070797Connect With a Mentor Commented:
Hi,
You can do that by enclosing the code in curly braces
and redirecting the output to a file at the closing of the curly braces.

Here is how you can do just that:

Ex: dummy.sh
---------------------------------------------
#!/bin/sh
# Note: open curly-brace:
{
# Do some shell things..
#....
echo "Done some shell things"
# Do some more silly shell things..
# ...
echo "Done some more silly shell things.."
# Done; close curly brace.
} >dummy.out 2>&1
# The above line should redirect both stdout and stderr
# of the preceding script to a file dummy.out  
---------------------------------------------




0
 
onestarCommented:
Can you give a example of what you are looking (a copy of you script)?
0
 
yppirt090497Author Commented:
Tested and it works nicely.

I was also sent another solution which uses 'exec'.

exec 1>foo
exec 2>foo
echo "test redirect"
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.