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<julien@linuxwall.info>

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

http://books.google.com/books?id=Qu6H8fnAvGoC&pg=PA342&lpg=PA342&dq=%22redirecting+output+for+the+life+of+a+script%22&source=bl&ots=CheSaesF9-&sig=q_w1PceCFvxmJFmnNq1KrAUx7gM&hl=en&ei=p3ReTu-wK8Pn0QHWu_ndAg&sa=X&oi=book_result&ct=result&resnum=4&ved=0CCwQ6AEwAw

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-

Later,
JP
----------------------------|:::======|-------------------------------
JP Vossen, CISSP            |:::======|      http://bashcookbook.com/
My Account, My Opinions     |=========|      http://www.jpsdomain.org/
----------------------------|=========|-------------------------------
"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         --        http://www.phillylinux.org
Announcements - http://lists.phillylinux.org/mailman/listinfo/plug-announce
General Discussion  --   http://lists.phillylinux.org/mailman/listinfo/plug