<?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>BASH Cures Cancer &#187; Brock&#8217;s Tools</title>
	<atom:link href="http://bashcurescancer.com/category/brocks-tools/feed" rel="self" type="application/rss+xml" />
	<link>http://bashcurescancer.com</link>
	<description>Learn the UNIX/Linux command line</description>
	<lastBuildDate>Tue, 25 Oct 2011 18:09:07 +0000</lastBuildDate>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.org/?v=3.2.1</generator>
		<item>
		<title>using kill to see if a process is alive</title>
		<link>http://bashcurescancer.com/using-kill-to-see-if-a-process-is-alive.html</link>
		<comments>http://bashcurescancer.com/using-kill-to-see-if-a-process-is-alive.html#comments</comments>
		<pubDate>Thu, 10 Apr 2008 04:43:55 +0000</pubDate>
		<dc:creator>Brock Noland</dc:creator>
				<category><![CDATA[Brock's Tools]]></category>
		<category><![CDATA[Good Practice]]></category>
		<category><![CDATA[Links]]></category>
		<category><![CDATA[script]]></category>
		<category><![CDATA[Shell]]></category>
		<category><![CDATA[tools]]></category>
		<category><![CDATA[kill]]></category>
		<category><![CDATA[process]]></category>
		<category><![CDATA[ps]]></category>
		<category><![CDATA[ps -ef]]></category>
		<category><![CDATA[ps faux]]></category>
		<category><![CDATA[ps grep]]></category>

		<guid isPermaLink="false">http://bashcurescancer.com/?p=141</guid>
		<description><![CDATA[I am making some changes to the moreutils sponge command. Sponge provides a method of prepending which is less specialized than my prepend util. However, it has trouble with large amounts of input. Regardless, while testing my changes, I want to watch it operate. Normally, you would just do so from a second terminal. That [...]]]></description>
			<content:encoded><![CDATA[<p>I am making some changes to the <a href="http://kitenet.net/~joey/code/moreutils/">moreutils</a> sponge command.  Sponge provides a method of prepending which is less specialized than my <a href="http://bashcurescancer.com/new-command-prepend.html">prepend</a> util. However, it has trouble with large amounts of input.</p>
<p>Regardless, while testing my changes, I want to watch it operate. Normally, you would just do so from a second terminal. That is a pain. kill -0 can be very useful for this. After backgrounding the command, I assign the pid (via the variable $!) to $pid using eval. eval is needed to stop BASH from expanding $! until after the background operation.</p>
<p>After that, I enter a while loop on kill -0 $pid, which will not kill $pid, but will return successfully until $pid has died:</p>
<pre># cat large-file-GB | ./sponge large-file-GB-copy &amp; eval 'pid=$!'; while kill -0 $pid; do sleep 10; ls -lh large-file* /tmp/sponge.*; echo;done
[1] 7937
-rw-r--r-- 1 root root 977M 2008-04-09 16:18 large-file-GB
-rw------- 1 root root 128M 2008-04-09 17:23 /tmp/sponge.JMsBWG

-rw-r--r-- 1 root root 977M 2008-04-09 16:18 large-file-GB
-rw------- 1 root root 384M 2008-04-09 17:23 /tmp/sponge.JMsBWG

-rw-r--r-- 1 root root 977M 2008-04-09 16:18 large-file-GB
-rw------- 1 root root 877M 2008-04-09 17:24 /tmp/sponge.JMsBWG

-rw-r--r-- 1 root root 977M 2008-04-09 16:18 large-file-GB
-rw-r--r-- 1 root root  20M 2008-04-09 17:24 large-file-GB-copy
-rw------- 1 root root 896M 2008-04-09 17:24 /tmp/sponge.JMsBWG

-rw-r--r-- 1 root root 977M 2008-04-09 16:18 large-file-GB
-rw-r--r-- 1 root root 413M 2008-04-09 17:25 large-file-GB-copy
-rw------- 1 root root 896M 2008-04-09 17:24 /tmp/sponge.JMsBWG

-rw-r--r-- 1 root root 977M 2008-04-09 16:18 large-file-GB
-rw-r--r-- 1 root root 836M 2008-04-09 17:25 large-file-GB-copy
-rw------- 1 root root 896M 2008-04-09 17:24 /tmp/sponge.JMsBWG

-rw-r--r-- 1 root root 977M 2008-04-09 16:18 large-file-GB
-rw-r--r-- 1 root root 920M 2008-04-09 17:25 large-file-GB-copy
[1]+  Done                    cat large-file-GB | ./sponge large-file-GB-copy
ls: cannot access /tmp/sponge.*: No such file or directory

-rw-r--r-- 1 root root 977M 2008-04-09 16:18 large-file-GB
-rw-r--r-- 1 root root 977M 2008-04-09 17:25 large-file-GB-copy
-bash: kill: (7937) - No such process
# md5sum large-file-GB*
b5c667a723a10a3485a33263c4c2b978  large-file-GB
b5c667a723a10a3485a33263c4c2b978  large-file-GB-copy</pre>
]]></content:encoded>
			<wfw:commentRss>http://bashcurescancer.com/using-kill-to-see-if-a-process-is-alive.html/feed</wfw:commentRss>
		<slash:comments>7</slash:comments>
		</item>
		<item>
		<title>New command: prepend</title>
		<link>http://bashcurescancer.com/new-command-prepend.html</link>
		<comments>http://bashcurescancer.com/new-command-prepend.html#comments</comments>
		<pubDate>Mon, 07 Apr 2008 02:44:37 +0000</pubDate>
		<dc:creator>Brock Noland</dc:creator>
				<category><![CDATA[Brock's Tools]]></category>
		<category><![CDATA[python]]></category>
		<category><![CDATA[script]]></category>
		<category><![CDATA[Shell]]></category>

		<guid isPermaLink="false">http://bashcurescancer.com/new-command-prepend.html</guid>
		<description><![CDATA[I am utilizing Google&#8217;s project hosting to host software which I create and feel is useful or want to keep track of. I called the project Brock&#8217;s Tools. The code that led me to create this project was a command I am calling prepend 1.1. (UPDATE: See this post on sponge as its a better [...]]]></description>
			<content:encoded><![CDATA[<p>I am utilizing Google&#8217;s project hosting to host software which I create and feel is useful or want to keep track of. I called the project <a href="http://code.google.com/p/brockstools/">Brock&#8217;s Tools</a>. The code that led me to create this project was a command I am calling <a href="http://brockstools.googlecode.com/files/prepend-1.1.tar.gz">prepend 1.1</a>. (UPDATE: See <a href="http://bashcurescancer.com/prepend-to-a-file-with-sponge-from-moreutils.html">this post on sponge</a> as its a better general case tool.)</p>
<p>prepend, prepend&#8217;s files or standard input to a file. For example,  you have three files:</p>
<pre>$ echo BROCK &gt; a
$ echo DAVID &gt; b
$ echo NOLAND &gt; c</pre>
<p>And you want to combine them into one file:</p>
<pre>$ echo "My name is:" | prepend - a b c
$ cat c
My name is:
BROCK
DAVID
NOLAND</pre>
<p>Or lets say you just want to append a file to itself:</p>
<pre>$ cat a
BROCK
$ cat a &gt;&gt; a
cat: a: input file is output file</pre>
<p>prepend does this:</p>
<pre>$ prepend a
$ cat a
BROCK
BROCK</pre>
<p>I come across the a situation where this would be useful quite often. Of course prepend&#8217;ing can be done in the shell:</p>
<pre>$ { echo "My name is:"; cat a b c; } &gt; tmp &amp;&amp; mv -f tmp c
$ cat c
My name is:
BROCK
DAVID
NOLAND</pre>
<p>However, that is unsafe and I have lost data that way. I perform this operation most often when dealing with XML. In this example, its trivial to open the file in an editor, but with a large file, its quite nasty to do so:</p>
<pre>$ cat something.xml
&lt;entry&gt;&lt;blah/&gt;&lt;more&gt;stuff 1&lt;/more&gt;&lt;/entry&gt;
&lt;entry&gt;&lt;blah/&gt;&lt;more&gt;stuff 2&lt;/more&gt;&lt;/entry&gt;
&lt;entry&gt;&lt;blah/&gt;&lt;more&gt;stuff 3 &lt;/more&gt;&lt;/entry&gt;
&lt;entry&gt;&lt;blah/&gt;&lt;more&gt;stuff 4&lt;/more&gt;&lt;/entry&gt;
$ echo "&lt;/entries&gt;" &gt;&gt; something.xml
$ cat something.xml
&lt;entry&gt;&lt;blah/&gt;&lt;more&gt;stuff 1&lt;/more&gt;&lt;/entry&gt;
&lt;entry&gt;&lt;blah/&gt;&lt;more&gt;stuff 2&lt;/more&gt;&lt;/entry&gt;
&lt;entry&gt;&lt;blah/&gt;&lt;more&gt;stuff 3 &lt;/more&gt;&lt;/entry&gt;
&lt;entry&gt;&lt;blah/&gt;&lt;more&gt;stuff 4&lt;/more&gt;&lt;/entry&gt;
&lt;/entries&gt;
$ echo "&lt;entries&gt;" | prepend - something.xml
$ cat something.xml
&lt;entries&gt;
&lt;entry&gt;&lt;blah/&gt;&lt;more&gt;stuff 1&lt;/more&gt;&lt;/entry&gt;
&lt;entry&gt;&lt;blah/&gt;&lt;more&gt;stuff 2&lt;/more&gt;&lt;/entry&gt;
&lt;entry&gt;&lt;blah/&gt;&lt;more&gt;stuff 3 &lt;/more&gt;&lt;/entry&gt;
&lt;entry&gt;&lt;blah/&gt;&lt;more&gt;stuff 4&lt;/more&gt;&lt;/entry&gt;
&lt;/entries&gt;</pre>
]]></content:encoded>
			<wfw:commentRss>http://bashcurescancer.com/new-command-prepend.html/feed</wfw:commentRss>
		<slash:comments>4</slash:comments>
		</item>
	</channel>
</rss>

<!-- Dynamic Page Served (once) in 0.210 seconds -->

