JP Vossen on 31 Aug 2011 10:56:50 -0700

[Date Prev] [Date Next] [Thread Prev] [Thread Next] [Date Index] [Thread Index]

Re: [PLUG] Bash I/O redirection

Date: Wed, 31 Aug 2011 11:33:31 -0400
From: Julien Vehent<>

I have this bash script that does an oracle backup (launched via
crontab), and I'd like to redirect the output of the script to a file.
The classic ways to do it would be:
     * use a wrapper script (that requires an additional script...)
     * call the script with '>' in the cron line (make my cron line ugly)
     * inside the script, put a ">>" after each command (berk !)

My question is: would there be a way to put a global redirection at the
beginning of the script ?

Yes.  You want:

_bash Cookbook_ Recipe 15.12 Redirecting Output for the Life of a Script

Short version (changed from the book's STDERR to STDOUT):

	# Optional, save the "old" STDOUT
	exec 3>&1

	# Redirect STDOUT to file
	exec 1 > /path/to/file

	# script goes here

	# Optional, revert change & close FH3
	exec 1>&3-

JP Vossen, CISSP            |:::======|
My Account, My Opinions     |=========|
"Microsoft Tax" = the additional hardware & yearly fees for the add-on
software required to protect Windows from its own poorly designed and
implemented self, while the overhead incidentally flattens Moore's Law.
Philadelphia Linux Users Group         --
Announcements -
General Discussion  --