<?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; python</title>
	<atom:link href="http://bashcurescancer.com/category/python/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>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>
		<item>
		<title>Wrapping dynamic languages in shell without an extra script</title>
		<link>http://bashcurescancer.com/wrapping-dynamic-languages-in-shell-without-an-extra-script.html</link>
		<comments>http://bashcurescancer.com/wrapping-dynamic-languages-in-shell-without-an-extra-script.html#comments</comments>
		<pubDate>Tue, 25 Mar 2008 23:16:30 +0000</pubDate>
		<dc:creator>Brock Noland</dc:creator>
				<category><![CDATA[python]]></category>
		<category><![CDATA[script]]></category>
		<category><![CDATA[Shell]]></category>
		<category><![CDATA[cx_Oracle]]></category>
		<category><![CDATA[LD_LIBRARY_PATH]]></category>
		<category><![CDATA[library]]></category>
		<category><![CDATA[oracle]]></category>

		<guid isPermaLink="false">http://bashcurescancer.com/wrapping-dynamic-languages-in-shell-without-an-extra-script.html</guid>
		<description><![CDATA[There are situations where, if you want a Python, PERL, PHP, etc script to be portable among a few different servers, it makes sense to wrap the script in shell. A few years ago I was trying to use the Python cx_Oracle module. This module is a wrapper for the native Oracle database driver. However, [...]]]></description>
			<content:encoded><![CDATA[<p>There are situations where, if you want a Python, PERL, PHP, etc script to be portable among a few different servers, it makes sense to wrap the script in shell. A few years ago I was trying to use the Python cx_Oracle module. This module is a wrapper for the native Oracle database driver. However, it requires the driver library directory be in the LD_LIBRARY_PATH environment variable.</p>
<p>No problem I thought. I&#8217;ll use the os.environ dict to set the variable.  Example script:</p>
<pre>$ cat python-only.sh
#!/usr/bin/python
import sys, os
sys.path.append("/usr/local/lib/python2.4/site-packages/")
if not os.environ.has_key('LD_LIBRARY_PATH'):
        os.environ['LD_LIBRARY_PATH'] = "/home/noland/oracle-lib"
else:
        os.environ['LD_LIBRARY_PATH'] = "/home/noland/oracle-lib:" + os.environ['LD_LIBRARY_PATH']
print "LD_LIBRARY_PATH looks OK in Python: LD_LIBRARY_PATH = ", os.environ['LD_LIBRARY_PATH']
os.system('echo LD_LIBRARY_PATH looks OK via os.system: LD_LIBRARY_PATH = $LD_LIBRARY_PATH')
try:
        import cx_Oracle
        print "Imported cx_Oracle! LD_LIBRARY_PATH was set correctly."
except ImportError, e:
        print "Woops, LD_LIBRARY_PATH was not set correctly: ", e</pre>
<p>This method does not work:</p>
<pre>$ ./python-only.sh
LD_LIBRARY_PATH looks OK in Python: LD_LIBRARY_PATH =  /home/noland/oracle-lib
LD_LIBRARY_PATH looks OK via os.system: LD_LIBRARY_PATH = /home/noland/oracle-lib
Woops, LD_LIBRARY_PATH was not set correctly:  libclntsh.so.10.1: cannot open shared object file: No such file or directory</pre>
<p>This seems to be a <a href="http://mail.python.org/pipermail/python-list/2006-July/394301.html">common problem</a>. However, when I was dealing with this a few years ago, I could not find a good resource on Google. I bite the bullet and wrote a separate shell script wrapper &#8211; hating invocation of the shell script. However, there is absolutely no reason I needed a separate shell script. I could have embedded the Python within a shell script. Example:</p>
<pre>$ cat python-and-bash.sh
#/bin/bash
export LD_LIBRARY_PATH=/home/noland/oracle-lib:$LD_LIBRARY_PATH
/usr/bin/python&lt;&lt;END_OF_PYTHON
import sys
sys.path.append("/usr/local/lib/python2.4/site-packages/")
try:
        import cx_Oracle
        print "Imported cx_Oracle! LD_LIBRARY_PATH was set correctly."
except ImportError, e:
        print "Woops, LD_LIBRARY_PATH was not set correctly: ", e
END_OF_PYTHON</pre>
<p>Ahh, much better:</p>
<pre>$ ./python-and-bash.sh
Imported cx_Oracle! LD_LIBRARY_PATH was set correctly.</pre>
<p>Of course I could have just set this variable in my profile. However, this creates an additional external dependency &#8211; which is what I was trying to avoid.</p>
]]></content:encoded>
			<wfw:commentRss>http://bashcurescancer.com/wrapping-dynamic-languages-in-shell-without-an-extra-script.html/feed</wfw:commentRss>
		<slash:comments>4</slash:comments>
		</item>
	</channel>
</rss>

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

