<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0"
	xmlns:content="http://purl.org/rss/1.0/modules/content/"
	xmlns:wfw="http://wellformedweb.org/CommentAPI/"
	xmlns:dc="http://purl.org/dc/elements/1.1/"
	xmlns:atom="http://www.w3.org/2005/Atom"
	xmlns:sy="http://purl.org/rss/1.0/modules/syndication/"
	xmlns:slash="http://purl.org/rss/1.0/modules/slash/"
	>

<channel>
	<title>Open Sourcery &#187; ftp</title>
	<atom:link href="http://www.opensourcery.co.uk/tag/ftp/feed/" rel="self" type="application/rss+xml" />
	<link>http://www.opensourcery.co.uk</link>
	<description>Open Source Support and Consultancy</description>
	<lastBuildDate>Wed, 27 Aug 2008 12:45:47 +0000</lastBuildDate>
	<generator>http://wordpress.org/?v=2.9.1</generator>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
			<item>
		<title>Handling temporary files in BASH shell scripts</title>
		<link>http://www.opensourcery.co.uk/2008/06/handling-temporary-files-in-bash-shell-scripts/</link>
		<comments>http://www.opensourcery.co.uk/2008/06/handling-temporary-files-in-bash-shell-scripts/#comments</comments>
		<pubDate>Thu, 05 Jun 2008 13:41:59 +0000</pubDate>
		<dc:creator>dave</dc:creator>
				<category><![CDATA[BASH scripting]]></category>
		<category><![CDATA[System Administration]]></category>
		<category><![CDATA[Tips and tricks]]></category>
		<category><![CDATA[bash]]></category>
		<category><![CDATA[ftp]]></category>

		<guid isPermaLink="false">http://www.opensourcery.co.uk/?p=35</guid>
		<description><![CDATA[Temporary files left over from shell scripts clutter up your /tmp directory and may result in information leakage. Below are a pair of functions we use to gracefully handle the creation and removal of temporary files in shell scripts.
The first function is used to create a temporary file:
function os_mktemp {
&#160;[[ ! $1 ]] &#038;&#038; echo [...]<p>a</p>
]]></description>
			<content:encoded><![CDATA[<p>Temporary files left over from shell scripts clutter up your /tmp directory and may result in information leakage. Below are a pair of functions we use to gracefully handle the creation and removal of temporary files in shell scripts.</p>
<p>The first function is used to create a temporary file:</p>
<blockquote><p><code>function os_mktemp {<br />
&nbsp;[[ ! $1 ]] &#038;&#038; echo "os_mktemp: required a handle name" &#038;&#038; exit 99<br />
&nbsp;let OS_TEMPFILEHANDLE++;<br />
&nbsp;OS_TEMPFILE[$OS_TEMPFILEHANDLE]=`mktemp /tmp/ostmp.XXXXXXXX`<br />
&nbsp;eval F_$1=${OS_TEMPFILE[$OS_TEMPFILEHANDLE]}<br />
}<br />
</code></p></blockquote>
<p>It requires a single parameter, which is used to create a variable name containing the path to the temporary file. For example. <code>os_mktemp FTPOUTPUT</code> will return a variable <code>$F_FTPOUTPUT</code>. </p>
<p>The array OS_TEMPFILE is an array holding the names of all the temporary file names, this is used by the cleanup function to remove the temporary files.</p>
<p> <code>os_mktemp OUTPUT</code>; this results in a temporary file with a random name being created and the name being stored in the variable $F_OUTPUT.</p>
<p>The second function is used to remove all temporary files.</p>
<blockquote><p><code><br />
function os_cleanup {<br />
&nbsp;for FILE in ${OS_TEMPFILE[@]}; do<br />
&nbsp;&nbsp;[[ -e "$FILE" ]] &#038;&#038; rm "$FILE" || echo "os_cleanup: couldn't remove temporary file $FILE."<br />
&nbsp;done<br />
}<br />
</code></p></blockquote>
<p>To ensure the os_cleanup code is executed whenever the shell script closes, we use the BASH <em>trap</em> command. </p>
<blockquote><p><code><br />
trap os_cleanup INT TERM EXIT<br />
</code></p></blockquote>
<p>a</p>
]]></content:encoded>
			<wfw:commentRss>http://www.opensourcery.co.uk/2008/06/handling-temporary-files-in-bash-shell-scripts/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
	</channel>
</rss>
