<?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>AdmiNirvana &#187; Scripting</title>
	<atom:link href="http://www.briandowney.net/blog/category/scripting/feed/" rel="self" type="application/rss+xml" />
	<link>http://www.briandowney.net/blog</link>
	<description>Technical musings of an entrepreneur.</description>
	<lastBuildDate>Thu, 20 Aug 2009 17:53:14 +0000</lastBuildDate>
	<generator>http://wordpress.org/?v=2.9.2</generator>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
			<item>
		<title>BASH Pipeline Exit Codes</title>
		<link>http://www.briandowney.net/blog/2008/05/05/bash-pipeline-exit-codes/</link>
		<comments>http://www.briandowney.net/blog/2008/05/05/bash-pipeline-exit-codes/#comments</comments>
		<pubDate>Mon, 05 May 2008 14:49:52 +0000</pubDate>
		<dc:creator>Brian</dc:creator>
				<category><![CDATA[Computers]]></category>
		<category><![CDATA[Linux]]></category>
		<category><![CDATA[Scripting]]></category>

		<guid isPermaLink="false">http://www.briandowney.net/blog/?p=99</guid>
		<description><![CDATA[I think I&#8217;ve mentioned many times on this blog, but one of the most satisfying things regarding Linux and Unix are that you&#8217;re never done learning about it.  A never-ending lesson in operating systems! Well, chalk up another lightbulb moment for me this morning.
Imagine a script wherein a process needs to be checked for [...]]]></description>
			<content:encoded><![CDATA[<p>I think I&#8217;ve mentioned many times on this blog, but one of the most satisfying things regarding Linux and Unix are that you&#8217;re never done learning about it.  A never-ending lesson in operating systems! Well, chalk up another lightbulb moment for me this morning.</p>
<p>Imagine a script wherein a process needs to be checked for proper exit.   Let&#8217;s say &#8220;mysqldump&#8221;.   Typically I&#8217;d do something like this, for example:</p>
<p><code><br />
#!/bin/bash<br />
STATUS=1<br />
while [ ${STATUS} -ne 0 ]<br />
do<br />
mysqldump -uroot -psomepass --all-databases &gt; sql-backup.sql<br />
STATUS=${?}<br />
done<br />
</code></p>
<p><code>exit 0<br />
</code></p>
<p>That&#8217;ll work just fine&#8211;the special reserved variable ${?} contains the exit code of the last run command.  Mysqldump is kind enough to use non-zero ones on any kind of error, so if it doesn&#8217;t work in our script we&#8217;ll retry.</p>
<p>But for instance, let&#8217;s say our script looks like this:</p>
<p><code><br />
#!/bin/bash<br />
STATUS=1<br />
while [ ${STATUS} -ne 0 ]<br />
do<br />
mysqldump -uroot -psomepass --all-databases | gzip  &gt; sql-backup.sql<br />
STATUS=${?}<br />
done<br />
</code></p>
<p><code>exit 0<br />
</code></p>
<p>The problem here is that ${?} now contains the exit code for gzip, not mysqldump!   Will gzip respond properly if mysqldump doesn&#8217;t provide an input stream from the pipe?  Maybe, maybe not.  Bottom line is that it isn&#8217;t reliable, and not what I&#8217;d consider good shell programming.</p>
<p>Instead, check out this solution:</p>
<p><code><br />
#!/bin/bash</code></p>
<p><code><br />
STATUS=1<br />
while [ ${STATUS} -ne 0 ]<br />
do<br />
mysqldump -uroot -psomepass --all-databases | gzip &gt; sql-backup.sql<br />
STATUS=${PIPESTATUS[0]}<br />
done<br />
</code></p>
<p><code> exit 0<br />
</code></p>
<p>The BASH reserved array ${PIPESTAUTUS[x]} contains the exit codes for all programs in the array. In this example, ${PIPESTATUS[0]} is mysqldump, and ${PIPESTATUS[1]} is gzip.</p>
]]></content:encoded>
			<wfw:commentRss>http://www.briandowney.net/blog/2008/05/05/bash-pipeline-exit-codes/feed/</wfw:commentRss>
		<slash:comments>2</slash:comments>
		</item>
		<item>
		<title>VMware Server Tips &#8216;n Tricks</title>
		<link>http://www.briandowney.net/blog/2008/04/08/vmware-server-tips-n-tricks/</link>
		<comments>http://www.briandowney.net/blog/2008/04/08/vmware-server-tips-n-tricks/#comments</comments>
		<pubDate>Tue, 08 Apr 2008 15:05:41 +0000</pubDate>
		<dc:creator>Brian</dc:creator>
				<category><![CDATA[Computers]]></category>
		<category><![CDATA[Linux]]></category>
		<category><![CDATA[Scripting]]></category>
		<category><![CDATA[vmware]]></category>
		<category><![CDATA[administration]]></category>
		<category><![CDATA[hints]]></category>
		<category><![CDATA[tips]]></category>
		<category><![CDATA[tricks]]></category>
		<category><![CDATA[vmware server]]></category>

		<guid isPermaLink="false">http://www.briandowney.net/blog/?p=74</guid>
		<description><![CDATA[As anyone whom reads this blog regularly knows, I&#8217;m a happy VMware Server user.  In using it, I&#8217;ve come across some handy methods in administrating it and the virtual machines created with it.  Without further ado, here they are!
Tip #1 &#8211; Start and stop your VMs from the command line
If your VMware server [...]]]></description>
			<content:encoded><![CDATA[<p>As anyone whom reads this blog regularly knows, I&#8217;m a happy VMware Server user.  In using it, I&#8217;ve come across some handy methods in administrating it and the virtual machines created with it.  Without further ado, here they are!</p>
<h3>Tip #1 &#8211; Start and stop your VMs from the command line</h3>
<p>If your VMware server is headless and gui-less (you didn&#8217;t install a GUI did you?) it&#8217;s handy to be able to start and stop your VM processes with a command line tool over ssh.  Use the vmware-cmd tool for this:</p>
<pre>vmware-cmd /path/to/vmxfile.vmx stop &lt;hard|soft&gt;</pre>
<p>or</p>
<pre>vmware-cmd /path/to/vmxfile.vmx start</pre>
<p>The third option is the powerop mode.  &#8217;soft&#8217; uses the VMware tools within the guest OS, while &#8216;hard&#8217; simply powers on and off the VM without the tools.</p>
<h3>Tip #2 &#8211; Re-install your VM Tools quickly</h3>
<p>After upgrading your kernel on Linux-based virtual machines, you&#8217;ll also have to re-compile  vmware tools&#8217;  kernel modules.   Upon initial installation,  you probably executed the usual:</p>
<pre>/usr/bin/vmware-config-tools.pl</pre>
<p>But did you know you can speed up the process and make it automatic by using the default options?   The next time you need to recompile your tools, use this instead</p>
<pre>/usr/bin/vmware-config-tools.pl -default</pre>
<h3>Tip #3 &#8211; Fine-grain your VM&#8217;s priority</h3>
<p>VMware Server does not provide the flexibility of ESX, but you can get it part-way there by using the Linux scheduler to prioritize your  virtual machines.  By default, VS gives all vmware-vmx processes a nice value of &#8220;-10&#8243;.   In Linux, processes with &#8220;-20&#8243; have the highest priority for system resources, and &#8220;20&#8243; have the lowest.   By adjusting your busy VMs to a higher negative number (e.g. -15) and your less-intensive VMs to a higher positive number (e.g 0) you can more finely tune your server&#8217;s performance and ensure timeslices on the host are more accurately granted.</p>
<p>To do this, use the `renice&#8217; command.   First, find the PIDs of your vmware-vmx processes, by using `ps&#8217;:</p>
<pre>[root@tlfvm5 ~]# ps -ef | grep vmware-vmx</pre>
<pre>root      3374     1 13 Mar18 ?        2-20:03:36 /usr/lib/vmware/bin/vmware-vmx -C /vmware/tlfmonitor/tlfmonitor.vmx -@ ""</pre>
<pre>root      4833     1 15 Mar18 ?        3-04:09:11 /usr/lib/vmware/bin/vmware-vmx -C /vmware/DellMonitor/DellMonitor.vmx -@ ""</pre>
<p>Then renice the appropriate PID.  For example, to give the &#8220;tlfmonitor&#8221; a bit of a bump to &#8220;-12&#8243;:</p>
<pre>renice -12 33</pre>
<p>Like all good things, moderation is key.  Start with smaller increments and note the change, then if needed bump it again.  It should be noted that your reniced values will disappear as soon as the PID terminates.   You can also give it a default higher priority via the .vmx file in the prority.grabbed and priority.ungrabbed directives (see <a href="http://sanbarrow.com/vmx/vmx-config-ini.html" target="_blank">http://sanbarrow.com/vmx/vmx-config-ini.html</a>).</p>
<h3>Tip #4 &#8211; Manage and extend your virtual disks</h3>
<p>VMware Server comes with a tool to completely manage your .vmdk disks.  The vmware-vdiskmanager tool can create, defrag, extend, and convert vmdks from one type to another.   For example, to expand a vmdk from 10GB to 15GB, power off the VM and issue this command:</p>
<pre>vmware-vdiskmanager -x 15Gb /path/to/vmdkfile.vmdk</pre>
<p>Note that this extends the raw disk, but not the guest file system.  For instance, after doing an extend in Linux on an ext3 file system, use &#8220;resize2fs&#8221; to adjust it accordingly.   You may want to run the vmware-vdiskmanager command without arguments to see some help on the different options, as well as some examples.</p>
<h3>Tip #5 &#8211; <strong>Install VMware tools from the command line</strong></h3>
<p>You don&#8217;t need to click &#8220;VM -&gt; Install Vmware Tools&#8230;&#8221; on the Server Console to mount the virtual media.   Do it from the command line!</p>
<pre>vmrun installtools /path/to/vmxfile.vmx</pre>
<p>This does precisely what clicking in the GUI does.  Once this has been run from the host, go to your VM and mount up the /dev/cdrom device and find your tools RPM ready to go.</p>
<p>That&#8217;s it for now.  Do you have any tips that are useful for other VMware Server administrators?  If so, let me know!</p>
]]></content:encoded>
			<wfw:commentRss>http://www.briandowney.net/blog/2008/04/08/vmware-server-tips-n-tricks/feed/</wfw:commentRss>
		<slash:comments>3</slash:comments>
		</item>
		<item>
		<title>Superfluous subshelling</title>
		<link>http://www.briandowney.net/blog/2008/02/14/superfluous-subshelling/</link>
		<comments>http://www.briandowney.net/blog/2008/02/14/superfluous-subshelling/#comments</comments>
		<pubDate>Thu, 14 Feb 2008 21:07:33 +0000</pubDate>
		<dc:creator>Brian</dc:creator>
				<category><![CDATA[Computers]]></category>
		<category><![CDATA[Linux]]></category>
		<category><![CDATA[Scripting]]></category>

		<guid isPermaLink="false">http://www.briandowney.net/blog/2008/02/14/superfluous-subshelling/</guid>
		<description><![CDATA[I run across this quite a bit and sort of do a &#8220;tsk-tsk&#8221; to the perpetrator.  Greg of yesthatsright.net suggested I make a post about it.
Let us assume you need to perform an operation on a bunch of files. I typically see it done as so in the BASH interpretor:

for I in `ls *.sh`
do
something
done

Now, [...]]]></description>
			<content:encoded><![CDATA[<p>I run across this quite a bit and sort of do a &#8220;tsk-tsk&#8221; to the perpetrator.  Greg of <a href="http://yesthatsright.net">yesthatsright.net</a> suggested I make a post about it.</p>
<p>Let us assume you need to perform an operation on a bunch of files. I typically see it done as so in the BASH interpretor:</p>
<p><code><br />
for I in `ls *.sh`<br />
do<br />
<em>something</em><br />
done<br />
</code></p>
<p>Now,  performing the backtick (`) subshell generally causes some hard to handle data mangling issues.   Most commonly a space in the file name or special character will result in odd permutations of ${I}.   For example in a file listing such as:</p>
<p><code><br />
file1.sh<br />
file2.sh<br />
file three.sh<br />
</code></p>
<p>Your resulting loop will end up with:</p>
<p><code><br />
[bdowney@tlfmgt1 ~]$ for I in `ls *.sh`; do echo ${I}; done<br />
file1.sh<br />
file2.sh<br />
file<br />
three.sh<br />
</code></p>
<p>The subshell returns strings to `for&#8217; which interprets whitespace as a new loop iteration.  This obviously causes issues for your loop logic, and I have seen people build in some pretty elaborate methods to handle it.   But it&#8217;s all for not&#8211;since `for&#8217; is a reserved shell function, it inherently understands file globbing.  Thus:</p>
<p><code><br />
[bdowney@tlfmgt1 ~]$ for I in *.sh; do echo ${I}; done<br />
file1.sh<br />
file2.sh<br />
file three.sh<br />
</code></p>
<p>Is all you need to do and solve your problem.  But what if one has a list of filenames in a file, or needs to pass more arguments to provide a better list of said files (for example, ls -t?).    Not to worry, the BASH built-in `read&#8217; to the rescue!</p>
<p><code><br />
[bdowney@tlfmgt1 ~]$ ls -tr *.sh | while read I; do echo ${I}; done<br />
file1.sh<br />
file2.sh<br />
file three.sh<br />
</code></p>
<p>And as you&#8217;d expect, works the same when redirecting STDIN:<br />
<code><br />
[bdowney@tlfmgt1 ~]$while read I; do echo ${I}; done &lt; input.file<br />
</code></p>
<p>So stop wasting those extra shell processes!</p>
]]></content:encoded>
			<wfw:commentRss>http://www.briandowney.net/blog/2008/02/14/superfluous-subshelling/feed/</wfw:commentRss>
		<slash:comments>2</slash:comments>
		</item>
		<item>
		<title>Clever cp enhancement</title>
		<link>http://www.briandowney.net/blog/2008/01/24/clever-cp-enhancement/</link>
		<comments>http://www.briandowney.net/blog/2008/01/24/clever-cp-enhancement/#comments</comments>
		<pubDate>Thu, 24 Jan 2008 16:35:36 +0000</pubDate>
		<dc:creator>Brian</dc:creator>
				<category><![CDATA[Linux]]></category>
		<category><![CDATA[Scripting]]></category>

		<guid isPermaLink="false">http://www.briandowney.net/blog/?p=6</guid>
		<description><![CDATA[A clever fellow I ran across used a dash of strace folded in with some shell functionality to give the ubiquitous `cp&#8217; command a wget-like progress bar:
% cp_p /mnt/raid/pub/iso/debian/debian-2.2r4potato-i386-netinst.iso &#62; /dev/null 
76% [===========================================&#62;                ]
Granted, you could probably do the same with a few pipes and `pv&#8216;, but this gets [...]]]></description>
			<content:encoded><![CDATA[<p>A clever fellow I ran across used a dash of strace folded in with some shell functionality to give the ubiquitous `cp&#8217; command a wget-like progress bar:</p>
<p><code>% cp_p /mnt/raid/pub/iso/debian/debian-2.2r4potato-i386-netinst.iso &gt; /dev/null </code></p>
<p><code>76% [===========================================&gt;                ]</code></p>
<p>Granted, you could probably do the same with a few pipes and `<a href="http://www.ivarch.com/programs/quickref/pv.shtml">pv</a>&#8216;, but this gets a medal for cleverness!</p>
<p>Full blog post <a href="http://chris-lamb.co.uk/2008/01/24/can-you-get-cp-to-give-a-progress-bar-like-wget/">here</a>.</p>
]]></content:encoded>
			<wfw:commentRss>http://www.briandowney.net/blog/2008/01/24/clever-cp-enhancement/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
	</channel>
</rss>
