Alan D. Salewski on 31 Aug 2011 11:05:11 -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:31AM -0400, Julien Vehent spake thus: > Hey Guys, > *snip* > 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 Sure, the following does just that: #!/bin/bash # save wherever stdout and stderr are currently pointing exec 3>&1 exec 4>&2 # stdout and stderr will both write declare -r logpath='output.log' exec 1>${logpath} exec 2>&1 # generate some output to stdout and stderr echo 'foo' echo 'bar' 1>&2 echo 'baz' echo 'quux' 1>&2 # restore stderr and stdout to their original fds exec 2>&4 exec 1>&3 # close our temporary "hold" fds exec 4>&- exec 3>&- -- ----------------------------------------------------------------- a l a n d. s a l e w s k i salewski@att.net 1024D/FA2C3588 EDFA 195F EDF1 0933 1002 6396 7C92 5CB3 FA2C 3588 ----------------------------------------------------------------- ___________________________________________________________________________ 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