﻿<?xml version="1.0" encoding="utf-8"?>
<feed xmlns="http://purl.org/atom/ns#">
	<link xmlns="http://purl.org/atom/ns#" type="text/html" rel="alternate" href="http://sial.org/blog/" title="Jeremy Mates’s Blog"/>
	<title xmlns="http://purl.org/atom/ns#">Jeremy Mates’s Blog</title>
	<entry xmlns="http://purl.org/atom/ns#" xmlns:default="http://www.w3.org/1999/xhtml">
		<title xmlns="http://purl.org/atom/ns#">Shell exec Tip</title>
		<dc:subject>Coding</dc:subject>
		<summary xmlns="http://purl.org/atom/ns#">summary</summary>
		<content xmlns="http://purl.org/atom/ns#" mode="escaped">&lt;p&gt;The shell &lt;tt&gt;exec&lt;/tt&gt; built-in can be used without a command to perform I/O redirection:&lt;/p&gt;
&lt;blockquote&gt;“If no command is given except for I/O redirection, the I/O redirection is permanent and the shell is not replaced. Any file descriptors which are opened or &lt;a href="http://www.openbsd.org/cgi-bin/man.cgi?query=dup&amp;sektion=2&amp;arch=i386&amp;apropos=0&amp;manpath=OpenBSD+Current"&gt;&lt;tt&gt;dup(2)&lt;/tt&gt;&lt;/a&gt;'d in this way are made available to other executed commands (note that the Korn shell differs here: it does not pass on file descriptors greater than 2).” — &lt;a href="http://www.openbsd.org/cgi-bin/man.cgi?query=sh&amp;apropos=0&amp;sektion=0&amp;manpath=OpenBSD+Current&amp;arch=i386&amp;format=html"&gt;&lt;tt&gt;sh(1)&lt;/tt&gt;&lt;/a&gt;&lt;/blockquote&gt;
&lt;p&gt;This allows a running shell script to change where standard output goes on the fly, for example to reproduce the gist of a “send output into files that change over time” feature as better implemented by &lt;a href="http://freshmeat.net/projects/httplog/"&gt;&lt;tt&gt;httplog&lt;/tt&gt;&lt;/a&gt; and &lt;a href="http://sial.org/howto/logging/syslog-ng/"&gt;syslog-ng&lt;/a&gt;:&lt;/p&gt;
&lt;p class="sial-block-code"&gt;#!/bin/sh

while sleep 1; do
  exec &gt;&gt; `date +log.%H:%M`

  echo &quot;The time is now `date`&quot;
done&lt;/p&gt;
&lt;p&gt;Other uses include setting the output log after code first determines the filename based on script arguments.&lt;/p&gt;
&lt;!-- technorati tags start --&gt;&lt;p style="text-align:right;font-size:10px;"&gt;Technorati Tags: &lt;a href="http://www.technorati.com/tag/coding" rel="tag"&gt;coding&lt;/a&gt;, &lt;a href="http://www.technorati.com/tag/Unix" rel="tag"&gt;Unix&lt;/a&gt;&lt;/p&gt;&lt;!-- technorati tags end --&gt;
&lt;br /&gt;

		</content>
		<issued xmlns="http://purl.org/atom/ns#">2007-05-08T22:29:46-0700</issued>
		<link xmlns="http://purl.org/atom/ns#" type="text/html" rel="alternate" href="http://sial.org/blog/2007/05/shell_exec_tip.html" title=""/>
		<id xmlns="http://purl.org/atom/ns#">181</id>
	</entry>
</feed>
