Matt Mossholder on 31 Aug 2011 08:48:34 -0700


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

Re: [PLUG] Bash I/O redirection


On Wed, Aug 31, 2011 at 11:33 AM, Julien Vehent <julien@linuxwall.info> wrote:
Hey Guys,

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 ?
something like
 "1 > /tmp/log.txt"
and then have all the subsequent commands go to the log file ?


Thanks,
Julien

Julian,
  ÂYou could try putting blocks of commands inside of parens, which runs them in a subshell, and then just after the parens, put in your redirect.

Example:

#!/bin/bash
(
 Âecho "Hi Julien!"
 Âcat /etc/hosts
 Âecho "Bye Julien!"
) 1> /tmp/myoutput 2>&1

That will take all the stdout and stderr and stuff it into /tmp/myoutput.

  Â--MattÂ
___________________________________________________________________________
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