<?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>CodeWord: Apokalyptik &#187; Bash</title>
	<atom:link href="http://blog.apokalyptik.com/category/bash/feed/" rel="self" type="application/rss+xml" />
	<link>http://blog.apokalyptik.com</link>
	<description>The random things that spew forth from my brain...</description>
	<lastBuildDate>Thu, 04 Mar 2010 09:45:42 +0000</lastBuildDate>
	<generator>http://wordpress.org/?v=3.0-alpha</generator>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
			<item>
		<title>php debugging the really really hard way</title>
		<link>http://blog.apokalyptik.com/2009/10/06/php-debugging-the-really-really-hard-way/</link>
		<comments>http://blog.apokalyptik.com/2009/10/06/php-debugging-the-really-really-hard-way/#comments</comments>
		<pubDate>Tue, 06 Oct 2009 22:18:56 +0000</pubDate>
		<dc:creator>apokalyptik</dc:creator>
				<category><![CDATA[Bash]]></category>
		<category><![CDATA[Business]]></category>
		<category><![CDATA[CLI]]></category>
		<category><![CDATA[Linux]]></category>
		<category><![CDATA[PHP]]></category>
		<category><![CDATA[Random Thoughts]]></category>
		<category><![CDATA[SRSLY]]></category>
		<category><![CDATA[Software Development]]></category>
		<category><![CDATA[WTF]]></category>
		<category><![CDATA[Web Stuff]]></category>

		<guid isPermaLink="false">http://blog.apokalyptik.com/?p=469</guid>
		<description><![CDATA[If you&#8217;re ever in a situation where something is only happening intermittently, and only on a live server, and only while it&#8217;s under load&#8230; Lets say its not generating any error_log or stderr output, and you cant run it manually to reproduce&#8230; (we&#8217;ve all been in this situation) How do you get any debugging output [...]]]></description>
			<content:encoded><![CDATA[<p>If you&#8217;re ever in a situation where something is only happening intermittently, and only on a live server, and only while it&#8217;s under load&#8230; Lets say its not generating any error_log or stderr output, and you cant run it manually to reproduce&#8230; (we&#8217;ve all been in this situation) How do you get any debugging output at all?</p>
<p>Step 1: add this to the top of your entry point php file</p>

<div class="wp_syntax"><div class="code"><pre class="php" style="font-family:monospace;"><span style="color: #b1b100;">if</span> <span style="color: #009900;">&#40;</span> <span style="color: #000088;">$_SERVER</span><span style="color: #009900;">&#91;</span><span style="color: #0000ff;">'REMOTE_ADDR'</span><span style="color: #009900;">&#93;</span> <span style="color: #339933;">==</span> <span style="color: #0000ff;">'127.0.0.1'</span> <span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
       <span style="color: #990000;">error_log</span><span style="color: #009900;">&#40;</span> <span style="color: #0000ff;">' :: '</span> <span style="color: #339933;">.</span> <span style="color: #990000;">getmypid</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
       <span style="color: #990000;">sleep</span><span style="color: #009900;">&#40;</span> <span style="color: #cc66cc;">10</span> <span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
<span style="color: #009900;">&#125;</span></pre></div></div>

<p>Step 2: use curl on the localhost to make the request</p>
<p>Step 3: (this assumes your error log is /tmp/php-error-output) run the following command in a second (root) terminal window</p>

<div class="wp_syntax"><div class="code"><pre class="bash" style="font-family:monospace;"><span style="color: #c20cb9; font-weight: bold;">strace</span> <span style="color: #660033;">-p</span> $<span style="color: #7a0874; font-weight: bold;">&#40;</span><span style="color: #c20cb9; font-weight: bold;">tail</span> <span style="color: #660033;">-n</span> <span style="color: #000000;">1000</span> <span style="color: #000000; font-weight: bold;">/</span>tmp<span style="color: #000000; font-weight: bold;">/</span>php-error-output <span style="color: #000000; font-weight: bold;">|</span> <span style="color: #c20cb9; font-weight: bold;">grep</span> <span style="color: #ff0000;">' :: '</span> <span style="color: #000000; font-weight: bold;">|</span> <span style="color: #c20cb9; font-weight: bold;">tail</span> <span style="color: #660033;">-n</span> <span style="color: #000000;">1</span> <span style="color: #000000; font-weight: bold;">|</span> <span style="color: #c20cb9; font-weight: bold;">sed</span> <span style="color: #660033;">-r</span> s<span style="color: #000000; font-weight: bold;">/</span><span style="color: #ff0000;">'^.+ :: '</span><span style="color: #000000; font-weight: bold;">//</span>g<span style="color: #7a0874; font-weight: bold;">&#41;</span> <span style="color: #660033;">-s</span> <span style="color: #000000;">10240</span> <span style="color: #000000;">2</span><span style="color: #000000; font-weight: bold;">&gt;&amp;</span><span style="color: #000000;">1</span></pre></div></div>

<p>Good luck&#8230;</p>
]]></content:encoded>
			<wfw:commentRss>http://blog.apokalyptik.com/2009/10/06/php-debugging-the-really-really-hard-way/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Using wait, $!, and () for threading in bash</title>
		<link>http://blog.apokalyptik.com/2008/12/11/using-wait-and-for-threading-in-bash/</link>
		<comments>http://blog.apokalyptik.com/2008/12/11/using-wait-and-for-threading-in-bash/#comments</comments>
		<pubDate>Fri, 12 Dec 2008 03:24:04 +0000</pubDate>
		<dc:creator>apokalyptik</dc:creator>
				<category><![CDATA[Bash]]></category>
		<category><![CDATA[Business]]></category>
		<category><![CDATA[CLI]]></category>
		<category><![CDATA[Linux]]></category>
		<category><![CDATA[Random Thoughts]]></category>
		<category><![CDATA[Software Development]]></category>
		<category><![CDATA[Web Stuff]]></category>

		<guid isPermaLink="false">http://blog.apokalyptik.com/?p=348</guid>
		<description><![CDATA[This is a simplistic use of the pattern that I wrote about in my last post to wait on multiple commands in bash.  In essence I have a script which runs a command (like uptime or restarting a daemon) on a whole bunch of servers (think pssh).  Anyways&#8230; this is how I modified [...]]]></description>
			<content:encoded><![CDATA[<p>This is a simplistic use of the <a href="/2008/12/05/bash-collecting-the-return-value-of-backgrounded-processes/">pattern that I wrote about</a> in my last post to wait on multiple commands in bash.  In essence I have a script which runs a command (like uptime or restarting a daemon) on a whole bunch of servers (think <a href="http://www.theether.org/pssh/">pssh</a>).  Anyways&#8230; this is how I modified the script to run the command on multiple hosts in parallel.  This is a bit simplistic as it runs, say, 10 parallel ssh commands and then waits for all 10 to complete.  I&#8217;m very confident that someone could easily adapt this to run at a constant concurrency level of $threads&#8230; but I didn&#8217;t need it just then so I didn&#8217;t go that far&#8230;  As a side note, this is possibly the first time I&#8217;ve ever *needed* an array in a bash script&#8230; hah&#8230;</p>

<div class="wp_syntax"><div class="code"><pre class="bash" style="font-family:monospace;"><span style="color: #666666; font-style: italic;"># $1 is the commandto run on the remote hosts</span>
<span style="color: #666666; font-style: italic;"># $2 is used for something not important for this script</span>
<span style="color: #666666; font-style: italic;"># $3 is the (optional) number of concurrent connections to use</span>
&nbsp;
<span style="color: #000000; font-weight: bold;">if</span> <span style="color: #7a0874; font-weight: bold;">&#91;</span> <span style="color: #000000; font-weight: bold;">!</span> <span style="color: #ff0000;">&quot;$3&quot;</span> == <span style="color: #ff0000;">&quot;&quot;</span> <span style="color: #7a0874; font-weight: bold;">&#93;</span>
<span style="color: #000000; font-weight: bold;">then</span>
    <span style="color: #007800;">threads</span>=$<span style="color: #000000;">3</span>
<span style="color: #000000; font-weight: bold;">else</span>
    <span style="color: #007800;">threads</span>=<span style="color: #000000;">1</span>
<span style="color: #000000; font-weight: bold;">fi</span>
&nbsp;
<span style="color: #007800;">cthreads</span>=<span style="color: #000000;">0</span>;
<span style="color: #007800;">stack</span>=<span style="color: #7a0874; font-weight: bold;">&#40;</span><span style="color: #7a0874; font-weight: bold;">&#41;</span>
<span style="color: #000000; font-weight: bold;">for</span> s <span style="color: #000000; font-weight: bold;">in</span> <span style="color: #007800;">$servers</span>
  <span style="color: #000000; font-weight: bold;">do</span>
    <span style="color: #000000; font-weight: bold;">if</span> <span style="color: #7a0874; font-weight: bold;">&#91;</span> <span style="color: #007800;">$cthreads</span> <span style="color: #660033;">-eq</span> <span style="color: #007800;">$threads</span> <span style="color: #7a0874; font-weight: bold;">&#93;</span>; <span style="color: #000000; font-weight: bold;">then</span>
        <span style="color: #000000; font-weight: bold;">for</span> job <span style="color: #000000; font-weight: bold;">in</span> <span style="color: #800000;">${stack[@]}</span>; <span style="color: #000000; font-weight: bold;">do</span>
              <span style="color: #7a0874; font-weight: bold;">wait</span> <span style="color: #007800;">$job</span>
        <span style="color: #000000; font-weight: bold;">done</span>
        <span style="color: #007800;">stack</span>=<span style="color: #7a0874; font-weight: bold;">&#40;</span><span style="color: #7a0874; font-weight: bold;">&#41;</span>
        <span style="color: #007800;">cthreads</span>=<span style="color: #000000;">0</span>
    <span style="color: #000000; font-weight: bold;">fi</span>
    <span style="color: #7a0874; font-weight: bold;">&#40;</span>
        <span style="color: #000000; font-weight: bold;">for</span> i <span style="color: #000000; font-weight: bold;">in</span> $<span style="color: #7a0874; font-weight: bold;">&#40;</span><span style="color: #c20cb9; font-weight: bold;">ssh</span> root<span style="color: #000000; font-weight: bold;">@</span><span style="color: #007800;">$s</span> <span style="color: #ff0000;">&quot;$1&quot;</span> <span style="color: #7a0874; font-weight: bold;">&#41;</span>
            <span style="color: #000000; font-weight: bold;">do</span>
                <span style="color: #7a0874; font-weight: bold;">echo</span> <span style="color: #660033;">-e</span> <span style="color: #ff0000;">&quot;<span style="color: #007800;">$s</span>:<span style="color: #000099; font-weight: bold;">\t</span><span style="color: #007800;">$i</span>&quot;</span>
        <span style="color: #000000; font-weight: bold;">done</span>
    <span style="color: #7a0874; font-weight: bold;">&#41;</span><span style="color: #000000; font-weight: bold;">&amp;</span> stack<span style="color: #7a0874; font-weight: bold;">&#91;</span><span style="color: #007800;">$cthreads</span><span style="color: #7a0874; font-weight: bold;">&#93;</span>=<span style="color: #007800;">$!</span>
    <span style="color: #7a0874; font-weight: bold;">let</span> <span style="color: #007800;">cthreads</span>=<span style="color: #007800;">$cthreads</span>+<span style="color: #000000;">1</span>
<span style="color: #000000; font-weight: bold;">done</span>
<span style="color: #000000; font-weight: bold;">for</span> job <span style="color: #000000; font-weight: bold;">in</span> <span style="color: #800000;">${stack[@]}</span>; <span style="color: #000000; font-weight: bold;">do</span>
    <span style="color: #7a0874; font-weight: bold;">wait</span> <span style="color: #007800;">$job</span>
<span style="color: #000000; font-weight: bold;">done</span></pre></div></div>

]]></content:encoded>
			<wfw:commentRss>http://blog.apokalyptik.com/2008/12/11/using-wait-and-for-threading-in-bash/feed/</wfw:commentRss>
		<slash:comments>1</slash:comments>
		</item>
		<item>
		<title>bash &#8211; collecting the return value of backgrounded processes</title>
		<link>http://blog.apokalyptik.com/2008/12/05/bash-collecting-the-return-value-of-backgrounded-processes/</link>
		<comments>http://blog.apokalyptik.com/2008/12/05/bash-collecting-the-return-value-of-backgrounded-processes/#comments</comments>
		<pubDate>Fri, 05 Dec 2008 22:05:30 +0000</pubDate>
		<dc:creator>apokalyptik</dc:creator>
				<category><![CDATA[Bash]]></category>
		<category><![CDATA[Business]]></category>
		<category><![CDATA[CLI]]></category>
		<category><![CDATA[Linux]]></category>
		<category><![CDATA[Random Thoughts]]></category>
		<category><![CDATA[Software Development]]></category>

		<guid isPermaLink="false">http://blog.apokalyptik.com/?p=311</guid>
		<description><![CDATA[You know that you can run something in the background in a bash script with ( command )&#038;, but a coworker recently wanted to run multiple commands, wait for all of them to complete, collect and decide what to do based on their return values&#8230; this proved much trickier.  Luckily there is an answer

#!/bin/bash
&#160;
&#40;sleep [...]]]></description>
			<content:encoded><![CDATA[<p>You know that you can run something in the background in a bash script with ( command )&#038;, but a coworker recently wanted to run multiple commands, wait for all of them to complete, collect and decide what to do based on their return values&#8230; this proved much trickier.  Luckily there is an answer</p>

<div class="wp_syntax"><div class="code"><pre class="bash" style="font-family:monospace;"><span style="color: #666666; font-style: italic;">#!/bin/bash</span>
&nbsp;
<span style="color: #7a0874; font-weight: bold;">&#40;</span><span style="color: #c20cb9; font-weight: bold;">sleep</span> <span style="color: #000000;">3</span>; <span style="color: #7a0874; font-weight: bold;">exit</span> <span style="color: #000000;">1</span><span style="color: #7a0874; font-weight: bold;">&#41;</span><span style="color: #000000; font-weight: bold;">&amp;</span> <span style="color: #007800;">p1</span>=<span style="color: #007800;">$!</span>
<span style="color: #7a0874; font-weight: bold;">&#40;</span><span style="color: #c20cb9; font-weight: bold;">sleep</span> <span style="color: #000000;">2</span>; <span style="color: #7a0874; font-weight: bold;">exit</span> <span style="color: #000000;">2</span><span style="color: #7a0874; font-weight: bold;">&#41;</span><span style="color: #000000; font-weight: bold;">&amp;</span> <span style="color: #007800;">p2</span>=<span style="color: #007800;">$!</span>
<span style="color: #7a0874; font-weight: bold;">&#40;</span><span style="color: #c20cb9; font-weight: bold;">sleep</span> <span style="color: #000000;">1</span>; <span style="color: #7a0874; font-weight: bold;">exit</span> <span style="color: #000000;">3</span><span style="color: #7a0874; font-weight: bold;">&#41;</span><span style="color: #000000; font-weight: bold;">&amp;</span> <span style="color: #007800;">p3</span>=<span style="color: #007800;">$!</span>
&nbsp;
<span style="color: #7a0874; font-weight: bold;">wait</span> <span style="color: #ff0000;">&quot;<span style="color: #007800;">$p1</span>&quot;</span>; <span style="color: #007800;">r1</span>=<span style="color: #007800;">$?</span>
<span style="color: #7a0874; font-weight: bold;">wait</span> <span style="color: #ff0000;">&quot;<span style="color: #007800;">$p2</span>&quot;</span>; <span style="color: #007800;">r2</span>=<span style="color: #007800;">$?</span>
<span style="color: #7a0874; font-weight: bold;">wait</span> <span style="color: #ff0000;">&quot;<span style="color: #007800;">$p3</span>&quot;</span>; <span style="color: #007800;">r3</span>=<span style="color: #007800;">$?</span>
&nbsp;
<span style="color: #7a0874; font-weight: bold;">echo</span> <span style="color: #ff0000;">&quot;<span style="color: #007800;">$p1</span>:<span style="color: #007800;">$r1</span> <span style="color: #007800;">$p2</span>:<span style="color: #007800;">$r2</span> <span style="color: #007800;">$p3</span>:<span style="color: #007800;">$r3</span>&quot;</span></pre></div></div>

]]></content:encoded>
			<wfw:commentRss>http://blog.apokalyptik.com/2008/12/05/bash-collecting-the-return-value-of-backgrounded-processes/feed/</wfw:commentRss>
		<slash:comments>2</slash:comments>
		</item>
		<item>
		<title>Command line arguments in bash scripts</title>
		<link>http://blog.apokalyptik.com/2008/07/07/command-line-arguments-in-bash-scripts/</link>
		<comments>http://blog.apokalyptik.com/2008/07/07/command-line-arguments-in-bash-scripts/#comments</comments>
		<pubDate>Mon, 07 Jul 2008 21:18:35 +0000</pubDate>
		<dc:creator>apokalyptik</dc:creator>
				<category><![CDATA[Bash]]></category>
		<category><![CDATA[Business]]></category>
		<category><![CDATA[CLI]]></category>
		<category><![CDATA[Linux]]></category>
		<category><![CDATA[Software Development]]></category>

		<guid isPermaLink="false">http://blog.apokalyptik.com/?p=292</guid>
		<description><![CDATA[This is something that has always annoyed me about bash scripts&#8230; The fact that it&#8217;s difficult to run

/path/to/script.sh --foo=bar -v -n 10 blah -one='last arg'

So I decided to write up a bash function that let me easily (once the function was complete) access this type of information.  And because I like sharing, here it [...]]]></description>
			<content:encoded><![CDATA[<p>This is something that has always annoyed me about bash scripts&#8230; The fact that it&#8217;s difficult to run</p>

<div class="wp_syntax"><div class="code"><pre class="bash" style="font-family:monospace;"><span style="color: #000000; font-weight: bold;">/</span>path<span style="color: #000000; font-weight: bold;">/</span>to<span style="color: #000000; font-weight: bold;">/</span>script.sh <span style="color: #660033;">--foo</span>=bar <span style="color: #660033;">-v</span> <span style="color: #660033;">-n</span> <span style="color: #000000;">10</span> blah <span style="color: #660033;">-one</span>=<span style="color: #ff0000;">'last arg'</span></pre></div></div>

<p>So I decided to write up a bash function that let me easily (once the function was complete) access this type of information.  And because I like sharing, here it is:</p>

<div class="wp_syntax"><div class="code"><pre class="bash" style="font-family:monospace;"><span style="color: #666666; font-style: italic;">#!/bin/bash</span>
<span style="color: #000000; font-weight: bold;">function</span> <span style="color: #c20cb9; font-weight: bold;">getopt</span><span style="color: #7a0874; font-weight: bold;">&#40;</span><span style="color: #7a0874; font-weight: bold;">&#41;</span> <span style="color: #7a0874; font-weight: bold;">&#123;</span>
  <span style="color: #007800;">var</span>=<span style="color: #ff0000;">&quot;&quot;</span>
  <span style="color: #007800;">wantarg</span>=<span style="color: #000000;">0</span>
  <span style="color: #000000; font-weight: bold;">for</span> <span style="color: #7a0874; font-weight: bold;">&#40;</span><span style="color: #7a0874; font-weight: bold;">&#40;</span> <span style="color: #007800;">i</span>=<span style="color: #000000;">1</span>; i<span style="color: #000000; font-weight: bold;">&lt;</span> =<span style="color: #007800;">$#</span>; i+=<span style="color: #000000;">1</span> <span style="color: #7a0874; font-weight: bold;">&#41;</span><span style="color: #7a0874; font-weight: bold;">&#41;</span>; <span style="color: #000000; font-weight: bold;">do</span>
    <span style="color: #007800;">lastvar</span>=<span style="color: #007800;">$var</span>
    <span style="color: #007800;">var</span>=<span style="color: #800000;">${!i}</span>
    <span style="color: #000000; font-weight: bold;">if</span> <span style="color: #7a0874; font-weight: bold;">&#91;</span> <span style="color: #ff0000;">&quot;<span style="color: #007800;">$var</span>&quot;</span> = <span style="color: #ff0000;">&quot;&quot;</span> <span style="color: #7a0874; font-weight: bold;">&#93;</span>; <span style="color: #000000; font-weight: bold;">then</span> 
        <span style="color: #7a0874; font-weight: bold;">continue</span> 
    <span style="color: #000000; font-weight: bold;">fi</span>
    <span style="color: #7a0874; font-weight: bold;">echo</span> \ <span style="color: #007800;">$var</span> <span style="color: #000000; font-weight: bold;">|</span> <span style="color: #c20cb9; font-weight: bold;">grep</span> <span style="color: #660033;">-q</span> <span style="color: #660033;">--</span> <span style="color: #ff0000;">'='</span>
    <span style="color: #000000; font-weight: bold;">if</span> <span style="color: #7a0874; font-weight: bold;">&#91;</span> <span style="color: #007800;">$?</span> <span style="color: #660033;">-eq</span> <span style="color: #000000;">0</span> <span style="color: #7a0874; font-weight: bold;">&#93;</span>; <span style="color: #000000; font-weight: bold;">then</span>
      <span style="color: #666666; font-style: italic;">## -*param=value</span>
      <span style="color: #007800;">var</span>=$<span style="color: #7a0874; font-weight: bold;">&#40;</span><span style="color: #7a0874; font-weight: bold;">echo</span> \ <span style="color: #007800;">$var</span> <span style="color: #000000; font-weight: bold;">|</span> <span style="color: #c20cb9; font-weight: bold;">sed</span> <span style="color: #660033;">-r</span> s<span style="color: #000000; font-weight: bold;">/</span><span style="color: #ff0000;">'^[ ]*-*'</span><span style="color: #000000; font-weight: bold;">/</span><span style="color: #ff0000;">''</span><span style="color: #000000; font-weight: bold;">/</span><span style="color: #7a0874; font-weight: bold;">&#41;</span>
      <span style="color: #007800;">myvar</span>=<span style="color: #800000;">${var%=*}</span>
      <span style="color: #007800;">myval</span>=<span style="color: #800000;">${var#*=}</span>
      <span style="color: #7a0874; font-weight: bold;">eval</span> <span style="color: #ff0000;">&quot;<span style="color: #007800;">${myvar}</span>&quot;</span>=<span style="color: #ff0000;">&quot;'<span style="color: #007800;">$myval</span>'&quot;</span>
    <span style="color: #000000; font-weight: bold;">else</span>
      <span style="color: #7a0874; font-weight: bold;">echo</span> \ <span style="color: #007800;">$var</span> <span style="color: #000000; font-weight: bold;">|</span> <span style="color: #c20cb9; font-weight: bold;">grep</span> <span style="color: #660033;">-E</span> <span style="color: #660033;">-q</span> <span style="color: #660033;">--</span> <span style="color: #ff0000;">'^[ ]*-'</span>
      <span style="color: #000000; font-weight: bold;">if</span> <span style="color: #7a0874; font-weight: bold;">&#91;</span> <span style="color: #007800;">$?</span> <span style="color: #660033;">-eq</span> <span style="color: #000000;">0</span> <span style="color: #7a0874; font-weight: bold;">&#93;</span>; <span style="color: #000000; font-weight: bold;">then</span>
        <span style="color: #666666; font-style: italic;"># -*param$</span>
        <span style="color: #007800;">var</span>=$<span style="color: #7a0874; font-weight: bold;">&#40;</span><span style="color: #7a0874; font-weight: bold;">echo</span> \ <span style="color: #007800;">$var</span> <span style="color: #000000; font-weight: bold;">|</span> <span style="color: #c20cb9; font-weight: bold;">sed</span> <span style="color: #660033;">-r</span> s<span style="color: #000000; font-weight: bold;">/</span><span style="color: #ff0000;">'^[ ]*-*'</span><span style="color: #000000; font-weight: bold;">/</span><span style="color: #ff0000;">''</span><span style="color: #000000; font-weight: bold;">/</span><span style="color: #7a0874; font-weight: bold;">&#41;</span>
        <span style="color: #7a0874; font-weight: bold;">eval</span> <span style="color: #ff0000;">&quot;<span style="color: #007800;">${var}</span>&quot;</span>=<span style="color: #000000;">1</span>
        <span style="color: #007800;">wantarg</span>=<span style="color: #000000;">1</span>
      <span style="color: #000000; font-weight: bold;">else</span>
        <span style="color: #7a0874; font-weight: bold;">echo</span> \ <span style="color: #007800;">$var</span> <span style="color: #000000; font-weight: bold;">|</span> <span style="color: #c20cb9; font-weight: bold;">grep</span> <span style="color: #660033;">-E</span> <span style="color: #660033;">--</span> <span style="color: #ff0000;">'^[ ]*-'</span>
        <span style="color: #000000; font-weight: bold;">if</span> <span style="color: #7a0874; font-weight: bold;">&#91;</span> <span style="color: #007800;">$?</span> <span style="color: #660033;">-eq</span> <span style="color: #000000;">0</span> <span style="color: #7a0874; font-weight: bold;">&#93;</span>; <span style="color: #000000; font-weight: bold;">then</span>
          <span style="color: #666666; font-style: italic;"># the current one has a dash, so cannot be</span>
          <span style="color: #666666; font-style: italic;"># the argument to the last parameter</span>
          <span style="color: #007800;">wantarg</span>=<span style="color: #000000;">0</span>
        <span style="color: #000000; font-weight: bold;">fi</span>
        <span style="color: #000000; font-weight: bold;">if</span> <span style="color: #7a0874; font-weight: bold;">&#91;</span> <span style="color: #007800;">$wantarg</span> <span style="color: #660033;">-eq</span> <span style="color: #000000;">1</span> <span style="color: #7a0874; font-weight: bold;">&#93;</span>; <span style="color: #000000; font-weight: bold;">then</span>
          <span style="color: #666666; font-style: italic;"># parameter argument</span>
          <span style="color: #007800;">val</span>=<span style="color: #007800;">$var</span>
          <span style="color: #007800;">var</span>=<span style="color: #007800;">$lastvar</span>
          <span style="color: #7a0874; font-weight: bold;">eval</span> <span style="color: #ff0000;">&quot;<span style="color: #007800;">${var}</span>&quot;</span>=<span style="color: #ff0000;">&quot;'<span style="color: #007800;">${val}</span>'&quot;</span>
          <span style="color: #007800;">wantarg</span>=<span style="color: #000000;">0</span>
        <span style="color: #000000; font-weight: bold;">else</span>
          <span style="color: #666666; font-style: italic;"># parameter</span>
          <span style="color: #000000; font-weight: bold;">if</span> <span style="color: #7a0874; font-weight: bold;">&#91;</span> <span style="color: #ff0000;">&quot;<span style="color: #007800;">${!var}</span>&quot;</span> = <span style="color: #ff0000;">&quot;&quot;</span> <span style="color: #7a0874; font-weight: bold;">&#93;</span>; <span style="color: #000000; font-weight: bold;">then</span>
            <span style="color: #7a0874; font-weight: bold;">eval</span> <span style="color: #ff0000;">&quot;<span style="color: #007800;">${var}</span>&quot;</span>=<span style="color: #000000;">1</span>
          <span style="color: #000000; font-weight: bold;">fi</span>
          <span style="color: #007800;">wantarg</span>=<span style="color: #000000;">0</span>
        <span style="color: #000000; font-weight: bold;">fi</span>
      <span style="color: #000000; font-weight: bold;">fi</span>
    <span style="color: #000000; font-weight: bold;">fi</span>
  <span style="color: #000000; font-weight: bold;">done</span>
<span style="color: #7a0874; font-weight: bold;">&#125;</span>
&nbsp;
<span style="color: #007800;">OIFS</span>=<span style="color: #007800;">$IFS</span>; <span style="color: #007800;">IFS</span>=$<span style="color: #7a0874; font-weight: bold;">&#40;</span><span style="color: #7a0874; font-weight: bold;">echo</span> <span style="color: #660033;">-e</span> <span style="color: #ff0000;">&quot;<span style="color: #000099; font-weight: bold;">\n</span>&quot;</span><span style="color: #7a0874; font-weight: bold;">&#41;</span>; <span style="color: #c20cb9; font-weight: bold;">getopt</span> $<span style="color: #000000; font-weight: bold;">@</span>; <span style="color: #007800;">IFS</span>=<span style="color: #007800;">$OIFS</span></pre></div></div>

<p>now at this point (assuming the above command line parameter and script) I should have access to the following variables:  $foo (&#8220;bar&#8221;) $v (1) $n (10) $blah (1) $one (&#8220;last arg&#8221;), like so:</p>
</pre>

<div class="wp_syntax"><div class="code"><pre class="bash" style="font-family:monospace;"><span style="color: #007800;">OIFS</span>=<span style="color: #007800;">$IFS</span>; <span style="color: #007800;">IFS</span>=$<span style="color: #7a0874; font-weight: bold;">&#40;</span><span style="color: #7a0874; font-weight: bold;">echo</span> <span style="color: #660033;">-e</span> <span style="color: #ff0000;">&quot;<span style="color: #000099; font-weight: bold;">\n</span>&quot;</span><span style="color: #7a0874; font-weight: bold;">&#41;</span>; <span style="color: #c20cb9; font-weight: bold;">getopt</span> $<span style="color: #000000; font-weight: bold;">@</span>; <span style="color: #007800;">IFS</span>=<span style="color: #007800;">$OIFS</span>
&nbsp;
<span style="color: #7a0874; font-weight: bold;">echo</span> <span style="color: #660033;">-e</span> <span style="color: #ff0000;">&quot;
foo:<span style="color: #000099; font-weight: bold;">\t</span><span style="color: #007800;">$foo</span>
v:<span style="color: #000099; font-weight: bold;">\t</span><span style="color: #007800;">$v</span>
n:<span style="color: #000099; font-weight: bold;">\t</span><span style="color: #007800;">$n</span>
blah:<span style="color: #000099; font-weight: bold;">\t</span><span style="color: #007800;">$blah</span>
one:<span style="color: #000099; font-weight: bold;">\t</span><span style="color: #007800;">$one</span>
&quot;</span></pre></div></div>

<p>You might be curious about this line:</p>

<div class="wp_syntax"><div class="code"><pre class="bash" style="font-family:monospace;"><span style="color: #007800;">OIFS</span>=<span style="color: #007800;">$IFS</span>; <span style="color: #007800;">IFS</span>=$<span style="color: #7a0874; font-weight: bold;">&#40;</span><span style="color: #7a0874; font-weight: bold;">echo</span> <span style="color: #660033;">-e</span> <span style="color: #ff0000;">&quot;<span style="color: #000099; font-weight: bold;">\n</span>&quot;</span><span style="color: #7a0874; font-weight: bold;">&#41;</span>; <span style="color: #c20cb9; font-weight: bold;">getopt</span> $<span style="color: #000000; font-weight: bold;">@</span>; <span style="color: #007800;">IFS</span>=<span style="color: #007800;">$OIFS</span></pre></div></div>

<p>IFS is the variable that tells bash how strings are separated (and mastering its use will go a long way towards enhancing your bash scripting skills.)  Anyhow, by default IFS=" " which normally is OK, but in our case we dont want "last arg" to be two seperate strings, but one.  I cannot put the IFS assignment inside the function because by that point bash has already split the variable, it needs to be done at a level of the script in which $@ has not been touched yet.  So I store the current IFS variable in $OIFS (Old IFS) and set IFS to a newline character.  After running the function we reassign IFS to what it was beforehand.  This is because I dont know what you might be doing with your IFS.  There are lots of reasons you might have already assigned it to something else, and I wouldnt want to break your flow.  So we do the polite thing.</p>
<p>And in case the above gets munged for some reason you can see the plain text version here: <a href="/files/bash-getopt/getopt.sh">bash-getopt/getopt.sh</a></p>
<p>Anyways, hope this helps someone out. If not it's still here for me when *I* need it <img src='http://blog.apokalyptik.com/wp-includes/images/smilies/icon_wink.gif' alt=';)' class='wp-smiley' /> </p>
]]></content:encoded>
			<wfw:commentRss>http://blog.apokalyptik.com/2008/07/07/command-line-arguments-in-bash-scripts/feed/</wfw:commentRss>
		<slash:comments>11</slash:comments>
		</item>
		<item>
		<title>Bash Coding Convention ../</title>
		<link>http://blog.apokalyptik.com/2008/06/19/bash-coding-convention/</link>
		<comments>http://blog.apokalyptik.com/2008/06/19/bash-coding-convention/#comments</comments>
		<pubDate>Thu, 19 Jun 2008 18:24:15 +0000</pubDate>
		<dc:creator>apokalyptik</dc:creator>
				<category><![CDATA[Bash]]></category>
		<category><![CDATA[Business]]></category>
		<category><![CDATA[CLI]]></category>
		<category><![CDATA[Linux]]></category>
		<category><![CDATA[OS X]]></category>
		<category><![CDATA[PHP]]></category>
		<category><![CDATA[Software Development]]></category>

		<guid isPermaLink="false">http://blog.apokalyptik.com/?p=291</guid>
		<description><![CDATA[We use dirname() a lot in php to make relative paths work from multiple locations like so.  The advantages are many:

require dirname&#40; dirname&#40; __FILE__ &#41; &#41; . '/required-file.php';
$data = file_get_contents&#40; dirname&#40;__FILE__&#41;.'/data/info.dat'&#41;;

But in bash we often dont do the same thing, we settle for the old standby &#8220;../&#8221;.  Which is a shame because unless [...]]]></description>
			<content:encoded><![CDATA[<p>We use dirname() a lot in php to make relative paths work from multiple locations like so.  The advantages are many:</p>

<div class="wp_syntax"><div class="code"><pre class="php" style="font-family:monospace;"><span style="color: #b1b100;">require</span> <span style="color: #990000;">dirname</span><span style="color: #009900;">&#40;</span> <span style="color: #990000;">dirname</span><span style="color: #009900;">&#40;</span> <span style="color: #009900; font-weight: bold;">__FILE__</span> <span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#41;</span> <span style="color: #339933;">.</span> <span style="color: #0000ff;">'/required-file.php'</span><span style="color: #339933;">;</span>
<span style="color: #000088;">$data</span> <span style="color: #339933;">=</span> <span style="color: #990000;">file_get_contents</span><span style="color: #009900;">&#40;</span> <span style="color: #990000;">dirname</span><span style="color: #009900;">&#40;</span><span style="color: #009900; font-weight: bold;">__FILE__</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">.</span><span style="color: #0000ff;">'/data/info.dat'</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span></pre></div></div>

<p>But in bash we often dont do the same thing, we settle for the old standby &#8220;../&#8221;.  Which is a shame because unless your directory structure is set up exactly right, and you have proper permissions, and you run the command from the right spot, it doesnt work as planned.  I think part of the reason is that its not obvious how to reliably get a full path to the script from inside itself.  Another reason is that ../ is shorter to type and easier to remember.  Finally there&#8217;s always one time scripts for which this methodology is overkill.  But if you&#8217;re planning to write a script which other people will (or might) be using, I think it&#8217;s good practice to do it right.  Googling for things you&#8217;d think to search for on this subject does not yeild very informative results, or incomplete (incorrect) methods&#8230; so&#8230; here&#8217;s how to do the above php in bash:</p>

<div class="wp_syntax"><div class="code"><pre class="bash" style="font-family:monospace;"><span style="color: #7a0874; font-weight: bold;">source</span> $<span style="color: #7a0874; font-weight: bold;">&#40;</span><span style="color: #c20cb9; font-weight: bold;">dirname</span> $<span style="color: #7a0874; font-weight: bold;">&#40;</span><span style="color: #c20cb9; font-weight: bold;">dirname</span> $<span style="color: #7a0874; font-weight: bold;">&#40;</span><span style="color: #c20cb9; font-weight: bold;">readlink</span> <span style="color: #660033;">-f</span> $<span style="color: #000000;">0</span><span style="color: #7a0874; font-weight: bold;">&#41;</span><span style="color: #7a0874; font-weight: bold;">&#41;</span><span style="color: #7a0874; font-weight: bold;">&#41;</span><span style="color: #000000; font-weight: bold;">/</span>required-file.sh 
<span style="color: #007800;">data</span>=$<span style="color: #7a0874; font-weight: bold;">&#40;</span><span style="color: #c20cb9; font-weight: bold;">cat</span> $<span style="color: #7a0874; font-weight: bold;">&#40;</span><span style="color: #c20cb9; font-weight: bold;">dirname</span> $<span style="color: #7a0874; font-weight: bold;">&#40;</span><span style="color: #c20cb9; font-weight: bold;">readlink</span> <span style="color: #660033;">-f</span> $<span style="color: #000000;">0</span><span style="color: #7a0874; font-weight: bold;">&#41;</span><span style="color: #7a0874; font-weight: bold;">&#41;</span><span style="color: #000000; font-weight: bold;">/</span>data<span style="color: #000000; font-weight: bold;">/</span>info.dat<span style="color: #7a0874; font-weight: bold;">&#41;</span></pre></div></div>

<p>Hope this helps someone <img src='http://blog.apokalyptik.com/wp-includes/images/smilies/icon_smile.gif' alt=':)' class='wp-smiley' /> </p>
<p>As a side note, the OSX readlink binary functions differently.  You&#8217;ll want to use a package manager to install gnu coreutils, and iether use greadlink, or link greadlink to a higher precedence location on your $PATH (I have /opt/local/bin:/opt/local/sbin: at the beginning of my $PATH)</p>
]]></content:encoded>
			<wfw:commentRss>http://blog.apokalyptik.com/2008/06/19/bash-coding-convention/feed/</wfw:commentRss>
		<slash:comments>2</slash:comments>
		</item>
		<item>
		<title>As Close to A Real Daemon As Bash Scripts Get</title>
		<link>http://blog.apokalyptik.com/2008/05/09/as-close-to-a-real-daemon-as-bash-scripts-get/</link>
		<comments>http://blog.apokalyptik.com/2008/05/09/as-close-to-a-real-daemon-as-bash-scripts-get/#comments</comments>
		<pubDate>Fri, 09 May 2008 18:18:47 +0000</pubDate>
		<dc:creator>apokalyptik</dc:creator>
				<category><![CDATA[Bash]]></category>
		<category><![CDATA[Business]]></category>
		<category><![CDATA[CLI]]></category>
		<category><![CDATA[Linux]]></category>
		<category><![CDATA[Software Development]]></category>

		<guid isPermaLink="false">http://blog.apokalyptik.com/?p=284</guid>
		<description><![CDATA[I&#8217;ve written a little something which is gaining some traction internally, and I always intended to share it with the world.  So&#8230; Here.  daemon-functions.sh
What it does is allow you to write a bash function called &#8220;payload&#8221; like so:

function payload&#40;&#41; &#123;
while &#91; true &#93;; do
checkforterm
date
sleep 1
done
&#125;
source path/to/daemon-functions.sh

Once you&#8217;ve done that it all just happens.  [...]]]></description>
			<content:encoded><![CDATA[<p>I&#8217;ve written a little something which is gaining some traction internally, and I always intended to share it with the world.  So&#8230; Here.  <a href="http://blog.apokalyptik.com/files/bash-daemon/daemon-functions.sh">daemon-functions.sh</a></p>
<p>What it does is allow you to write a bash function called &#8220;payload&#8221; like so:</p>

<div class="wp_syntax"><div class="code"><pre class="bash" style="font-family:monospace;"><span style="color: #000000; font-weight: bold;">function</span> payload<span style="color: #7a0874; font-weight: bold;">&#40;</span><span style="color: #7a0874; font-weight: bold;">&#41;</span> <span style="color: #7a0874; font-weight: bold;">&#123;</span>
<span style="color: #000000; font-weight: bold;">while</span> <span style="color: #7a0874; font-weight: bold;">&#91;</span> <span style="color: #c20cb9; font-weight: bold;">true</span> <span style="color: #7a0874; font-weight: bold;">&#93;</span>; <span style="color: #000000; font-weight: bold;">do</span>
checkforterm
<span style="color: #c20cb9; font-weight: bold;">date</span>
<span style="color: #c20cb9; font-weight: bold;">sleep</span> <span style="color: #000000;">1</span>
<span style="color: #000000; font-weight: bold;">done</span>
<span style="color: #7a0874; font-weight: bold;">&#125;</span>
<span style="color: #7a0874; font-weight: bold;">source</span> path<span style="color: #000000; font-weight: bold;">/</span>to<span style="color: #000000; font-weight: bold;">/</span>daemon-functions.sh</pre></div></div>

<p>Once you&#8217;ve done that it all just happens.  daemon-functions gives you logging of stderr, stdout, a pid file, start, stop, pause, resume, and more functions.  when you start your daemon it detaches completely from your terminal and runs in the background.  Works very simply with monit straight out of the box.  you can have as many daemons as you wish in the same directory and they wont clobber each other (as the pid, control, and log files all are dynamically keyed off of the original script name.)  Furthermore inside your execution loop inside of the payload function place a checkforterm call at any place which it makes sense for your script to be paused, or stopped. it can detect stale pid files and run anyway if the process isnt really running.  As an added bonus you dont actually have to loop inside payload, you can put any thing in there, have a script thats not a daemon, but will take an hour, day, week, month to finish? stick it in, run it, and forget it.</p>
]]></content:encoded>
			<wfw:commentRss>http://blog.apokalyptik.com/2008/05/09/as-close-to-a-real-daemon-as-bash-scripts-get/feed/</wfw:commentRss>
		<slash:comments>2</slash:comments>
		</item>
		<item>
		<title>Bash Tip: Closing File Descriptors</title>
		<link>http://blog.apokalyptik.com/2007/10/24/bash-tip-closing-file-descriptors/</link>
		<comments>http://blog.apokalyptik.com/2007/10/24/bash-tip-closing-file-descriptors/#comments</comments>
		<pubDate>Thu, 25 Oct 2007 01:05:17 +0000</pubDate>
		<dc:creator>apokalyptik</dc:creator>
				<category><![CDATA[Bash]]></category>
		<category><![CDATA[CLI]]></category>
		<category><![CDATA[Linux]]></category>
		<category><![CDATA[Software Development]]></category>

		<guid isPermaLink="false">http://blog.apokalyptik.com/2007/10/24/bash-tip-closing-file-descriptors/</guid>
		<description><![CDATA[I recently found that you can close bash file descriptors fairly easily, it goes like this:

    exec 0&#62;&#38;- # close stdin
    exec 1&#62;&#38;- # close stdout
    exec 2&#62;&#38;- # close stderr

Which makes it easy to daemonize things using only bash (lets face it there are times [...]]]></description>
			<content:encoded><![CDATA[<p>I recently found that you can close bash file descriptors fairly easily, it goes like this:</p>

<div class="wp_syntax"><div class="code"><pre class="bash" style="font-family:monospace;">    <span style="color: #7a0874; font-weight: bold;">exec</span> <span style="color: #000000;">0</span><span style="color: #000000; font-weight: bold;">&gt;&amp;</span>- <span style="color: #666666; font-style: italic;"># close stdin</span>
    <span style="color: #7a0874; font-weight: bold;">exec</span> <span style="color: #000000;">1</span><span style="color: #000000; font-weight: bold;">&gt;&amp;</span>- <span style="color: #666666; font-style: italic;"># close stdout</span>
    <span style="color: #7a0874; font-weight: bold;">exec</span> <span style="color: #000000;">2</span><span style="color: #000000; font-weight: bold;">&gt;&amp;</span>- <span style="color: #666666; font-style: italic;"># close stderr</span></pre></div></div>

<p>Which makes it easy to daemonize things using only bash (lets face it there are times when you JUST don&#8217;t need anything more than a simple bash script, you just need it backgrounded/daemonized).  Take this example of a daemon that copies any new files created in a directory to another place on the filesystem</p>

<div class="wp_syntax"><div class="code"><pre class="bash" style="font-family:monospace;"><span style="color: #666666; font-style: italic;">#!/bin/bash</span>
&nbsp;
<span style="color: #666666; font-style: italic;">##</span>
<span style="color: #666666; font-style: italic;">## Shell Daemon For: Backup /root/</span>
<span style="color: #666666; font-style: italic;">## (poorly coded, quick and dirty, example)</span>
<span style="color: #666666; font-style: italic;">##</span>
&nbsp;
<span style="color: #007800;">PIDFILE</span>=<span style="color: #ff0000;">&quot;/var/run/rootmirror.pid&quot;</span>
<span style="color: #007800;">LOGFILE</span>=<span style="color: #ff0000;">&quot;/log/log/rootmirror-%Y-%m-%d.log&quot;</span>
<span style="color: #007800;">NOHUP</span>=<span style="color: #ff0000;">&quot;/usr/bin/nohup&quot;</span>
<span style="color: #007800;">CRONOLOG</span>=<span style="color: #ff0000;">&quot;/usr/bin/cronolog&quot;</span>
<span style="color: #000000; font-weight: bold;">case</span> $<span style="color: #000000;">1</span> <span style="color: #000000; font-weight: bold;">in</span>
start<span style="color: #7a0874; font-weight: bold;">&#41;</span>
    <span style="color: #7a0874; font-weight: bold;">exec</span> <span style="color: #000000;">0</span><span style="color: #000000; font-weight: bold;">&gt;&amp;</span>- <span style="color: #666666; font-style: italic;"># close stdin</span>
    <span style="color: #7a0874; font-weight: bold;">exec</span> <span style="color: #000000;">1</span><span style="color: #000000; font-weight: bold;">&gt;&amp;</span>- <span style="color: #666666; font-style: italic;"># close stdout</span>
    <span style="color: #7a0874; font-weight: bold;">exec</span> <span style="color: #000000;">2</span><span style="color: #000000; font-weight: bold;">&gt;&amp;</span>- <span style="color: #666666; font-style: italic;"># close stderr</span>
    <span style="color: #007800;">$NOHUP</span> $<span style="color: #000000;">0</span> run <span style="color: #000000; font-weight: bold;">|</span> <span style="color: #007800;">$CRONOLOG</span> <span style="color: #007800;">$LOGFILE</span> <span style="color: #000000; font-weight: bold;">&amp;</span>gt;<span style="color: #000000; font-weight: bold;">&amp;</span>gt; <span style="color: #000000; font-weight: bold;">/</span>dev<span style="color: #000000; font-weight: bold;">/</span>null <span style="color: #000000; font-weight: bold;">&amp;</span>amp;
    <span style="color: #000000; font-weight: bold;">;;</span>
stop<span style="color: #7a0874; font-weight: bold;">&#41;</span>
    <span style="color: #000000; font-weight: bold;">/</span>bin<span style="color: #000000; font-weight: bold;">/</span><span style="color: #c20cb9; font-weight: bold;">kill</span> $<span style="color: #7a0874; font-weight: bold;">&#40;</span><span style="color: #c20cb9; font-weight: bold;">cat</span> <span style="color: #007800;">$PIDFILE</span><span style="color: #7a0874; font-weight: bold;">&#41;</span>
    <span style="color: #000000; font-weight: bold;">;;</span>
run<span style="color: #7a0874; font-weight: bold;">&#41;</span>
    pgrep <span style="color: #660033;">-f</span> <span style="color: #ff0000;">&quot;$0 $1&quot;</span> <span style="color: #000000; font-weight: bold;">&amp;</span>gt; <span style="color: #007800;">$PIDFILE</span>
    <span style="color: #000000; font-weight: bold;">while</span> <span style="color: #7a0874; font-weight: bold;">&#91;</span> <span style="color: #c20cb9; font-weight: bold;">true</span> <span style="color: #7a0874; font-weight: bold;">&#93;</span>; <span style="color: #000000; font-weight: bold;">do</span>
        <span style="color: #007800;">event</span>=$<span style="color: #7a0874; font-weight: bold;">&#40;</span>inotifywait <span style="color: #660033;">-q</span> <span style="color: #660033;">-e</span> close_write <span style="color: #660033;">--format</span> <span style="color: #ff0000;">&quot;%f&quot;</span> <span style="color: #000000; font-weight: bold;">/</span>root<span style="color: #000000; font-weight: bold;">/</span><span style="color: #7a0874; font-weight: bold;">&#41;</span>
        <span style="color: #7a0874; font-weight: bold;">&#40;</span> <span style="color: #c20cb9; font-weight: bold;">cp</span> <span style="color: #660033;">-v</span> <span style="color: #ff0000;">&quot;/root/<span style="color: #007800;">$event</span>&quot;</span> <span style="color: #ff0000;">&quot;/var/lib/rootmirror/<span style="color: #007800;">$event</span>&quot;</span> <span style="color: #7a0874; font-weight: bold;">&#41;</span><span style="color: #000000; font-weight: bold;">&amp;</span>amp;
    <span style="color: #000000; font-weight: bold;">done</span>
    <span style="color: #000000; font-weight: bold;">;;</span>
<span style="color: #000000; font-weight: bold;">*</span><span style="color: #7a0874; font-weight: bold;">&#41;</span>
    <span style="color: #7a0874; font-weight: bold;">echo</span> <span style="color: #ff0000;">&quot;$0 [ start | stop ]&quot;</span>
    <span style="color: #7a0874; font-weight: bold;">exit</span> <span style="color: #000000;">0</span>
    <span style="color: #000000; font-weight: bold;">;;</span>
<span style="color: #000000; font-weight: bold;">esac</span></pre></div></div>

<p>One especially nice detail here is that this wont hang while exiting your SSH session after you start it up (a big pet peeve of mine).</p>
]]></content:encoded>
			<wfw:commentRss>http://blog.apokalyptik.com/2007/10/24/bash-tip-closing-file-descriptors/feed/</wfw:commentRss>
		<slash:comments>2</slash:comments>
		</item>
		<item>
		<title>Scripting without killing system load</title>
		<link>http://blog.apokalyptik.com/2007/08/20/scripting-without-killing-system-load/</link>
		<comments>http://blog.apokalyptik.com/2007/08/20/scripting-without-killing-system-load/#comments</comments>
		<pubDate>Mon, 20 Aug 2007 18:25:01 +0000</pubDate>
		<dc:creator>apokalyptik</dc:creator>
				<category><![CDATA[Bash]]></category>
		<category><![CDATA[CLI]]></category>
		<category><![CDATA[Linux]]></category>
		<category><![CDATA[Random Thoughts]]></category>

		<guid isPermaLink="false">http://blog.apokalyptik.com/2007/08/20/scripting-without-killing-system-load/</guid>
		<description><![CDATA[Let us pretend for a moment that you have a critical system which can *just* handle the strain that it&#8217;s under (I&#8217;m sure all of you have workloads well under your system capabilities, or capabilities well over your workload requirements, of course; still for the sake of argument&#8230;)  And you have a job to [...]]]></description>
			<content:encoded><![CDATA[<p>Let us pretend for a moment that you have a critical system which can *just* handle the strain that it&#8217;s under (I&#8217;m sure all of you have workloads well under your system capabilities, or capabilities well over your workload requirements, of course; still for the sake of argument&#8230;)  And you have a job to do which will induce more load.  The job has to be done.  The system has to remain responsive.   Your classic response to this problem is adding a delay, for example:</p>

<div class="wp_syntax"><div class="code"><pre class="bash" style="font-family:monospace;">     <span style="color: #666666; font-style: italic;">#!/bin/bash</span>
     <span style="color: #7a0874; font-weight: bold;">cd</span> <span style="color: #000000; font-weight: bold;">/</span>foo
     <span style="color: #c20cb9; font-weight: bold;">find</span> .<span style="color: #000000; font-weight: bold;">/</span> <span style="color: #660033;">-type</span> d <span style="color: #660033;">-daystart</span> <span style="color: #660033;">-ctime</span> +<span style="color: #000000;">1</span> <span style="color: #660033;">-maxdepth</span> <span style="color: #000000;">1</span> <span style="color: #000000; font-weight: bold;">|</span> <span style="color: #c20cb9; font-weight: bold;">head</span> <span style="color: #660033;">-n</span> <span style="color: #000000;">500</span> <span style="color: #000000; font-weight: bold;">|</span> <span style="color: #c20cb9; font-weight: bold;">xargs</span> <span style="color: #660033;">--</span> <span style="color: #c20cb9; font-weight: bold;">rm</span> <span style="color: #660033;">-rv</span>
     <span style="color: #000000; font-weight: bold;">while</span> <span style="color: #7a0874; font-weight: bold;">&#91;</span> <span style="color: #007800;">$?</span> <span style="color: #660033;">-eq</span> <span style="color: #000000;">0</span> <span style="color: #7a0874; font-weight: bold;">&#93;</span>; <span style="color: #000000; font-weight: bold;">do</span>
          <span style="color: #c20cb9; font-weight: bold;">sleep</span> <span style="color: #000000;">60</span>
          <span style="color: #c20cb9; font-weight: bold;">find</span> .<span style="color: #000000; font-weight: bold;">/</span> <span style="color: #660033;">-type</span> d <span style="color: #660033;">-daystart</span> <span style="color: #660033;">-ctime</span> +<span style="color: #000000;">1</span> <span style="color: #660033;">-maxdepth</span> <span style="color: #000000;">1</span> <span style="color: #000000; font-weight: bold;">|</span> <span style="color: #c20cb9; font-weight: bold;">head</span> <span style="color: #660033;">-n</span> <span style="color: #000000;">500</span> <span style="color: #000000; font-weight: bold;">|</span> <span style="color: #c20cb9; font-weight: bold;">xargs</span> <span style="color: #660033;">--</span> <span style="color: #c20cb9; font-weight: bold;">rm</span> <span style="color: #660033;">-rv</span>
     <span style="color: #000000; font-weight: bold;">done</span></pre></div></div>

<p>Of course this is a fairly simplistic example.    Still it illustrates my point.  The problem with this solution is that the machine you&#8217;re working on is likely to have a variable workload where its main use comes in surges.     By defining a sleep time you have to iether sleep so long that the job takes forever to finish, or skirt with high loads and slow response times.  Ideally you would be able to let her rip while the load is low and throttle her back while the load is high, right?    Well we can!  Like so:</p>

<div class="wp_syntax"><div class="code"><pre class="bash" style="font-family:monospace;">     <span style="color: #666666; font-style: italic;">#!/bin/bash</span>
     <span style="color: #000000; font-weight: bold;">function</span> waitonload<span style="color: #7a0874; font-weight: bold;">&#40;</span><span style="color: #7a0874; font-weight: bold;">&#41;</span> <span style="color: #7a0874; font-weight: bold;">&#123;</span>
          <span style="color: #007800;">loadAvg</span>=$<span style="color: #7a0874; font-weight: bold;">&#40;</span><span style="color: #c20cb9; font-weight: bold;">cat</span> <span style="color: #000000; font-weight: bold;">/</span>proc<span style="color: #000000; font-weight: bold;">/</span>loadavg <span style="color: #000000; font-weight: bold;">|</span> <span style="color: #c20cb9; font-weight: bold;">cut</span> <span style="color: #660033;">-f1</span> <span style="color: #660033;">-d</span><span style="color: #ff0000;">'.'</span><span style="color: #7a0874; font-weight: bold;">&#41;</span>
          <span style="color: #000000; font-weight: bold;">while</span> <span style="color: #7a0874; font-weight: bold;">&#91;</span> <span style="color: #007800;">$loadAvg</span> <span style="color: #660033;">-gt</span> $<span style="color: #000000;">1</span> <span style="color: #7a0874; font-weight: bold;">&#93;</span>; <span style="color: #000000; font-weight: bold;">do</span>
               <span style="color: #c20cb9; font-weight: bold;">sleep</span> <span style="color: #000000;">1</span>
               <span style="color: #7a0874; font-weight: bold;">echo</span> <span style="color: #660033;">-n</span> .
               <span style="color: #007800;">loadAvg</span>=$<span style="color: #7a0874; font-weight: bold;">&#40;</span><span style="color: #c20cb9; font-weight: bold;">cat</span> <span style="color: #000000; font-weight: bold;">/</span>proc<span style="color: #000000; font-weight: bold;">/</span>loadavg <span style="color: #000000; font-weight: bold;">|</span> <span style="color: #c20cb9; font-weight: bold;">cut</span> <span style="color: #660033;">-f1</span> <span style="color: #660033;">-d</span><span style="color: #ff0000;">'.'</span><span style="color: #7a0874; font-weight: bold;">&#41;</span>
               <span style="color: #000000; font-weight: bold;">if</span> <span style="color: #7a0874; font-weight: bold;">&#91;</span> <span style="color: #007800;">$loadAvg</span> <span style="color: #660033;">-le</span> $<span style="color: #000000;">1</span> <span style="color: #7a0874; font-weight: bold;">&#93;</span>; <span style="color: #000000; font-weight: bold;">then</span> <span style="color: #7a0874; font-weight: bold;">echo</span>; <span style="color: #000000; font-weight: bold;">fi</span>
          <span style="color: #000000; font-weight: bold;">done</span>
     <span style="color: #7a0874; font-weight: bold;">&#125;</span>
&nbsp;
     waitonload <span style="color: #000000;">1</span>
     <span style="color: #c20cb9; font-weight: bold;">find</span> .<span style="color: #000000; font-weight: bold;">/</span> <span style="color: #660033;">-type</span> d <span style="color: #660033;">-daystart</span> <span style="color: #660033;">-ctime</span> +<span style="color: #000000;">1</span> <span style="color: #660033;">-maxdepth</span> <span style="color: #000000;">1</span> <span style="color: #000000; font-weight: bold;">|</span> <span style="color: #c20cb9; font-weight: bold;">head</span> <span style="color: #660033;">-n</span> <span style="color: #000000;">500</span> <span style="color: #000000; font-weight: bold;">|</span> <span style="color: #c20cb9; font-weight: bold;">xargs</span> <span style="color: #660033;">--</span> <span style="color: #c20cb9; font-weight: bold;">rm</span> <span style="color: #660033;">-rv</span>
     <span style="color: #000000; font-weight: bold;">while</span> <span style="color: #7a0874; font-weight: bold;">&#91;</span> <span style="color: #007800;">$?</span> <span style="color: #660033;">-eq</span> <span style="color: #000000;">0</span> <span style="color: #7a0874; font-weight: bold;">&#93;</span>; <span style="color: #000000; font-weight: bold;">do</span>
          waitonload <span style="color: #000000;">1</span>
          <span style="color: #c20cb9; font-weight: bold;">find</span> .<span style="color: #000000; font-weight: bold;">/</span> <span style="color: #660033;">-type</span> d <span style="color: #660033;">-daystart</span> <span style="color: #660033;">-ctime</span> +<span style="color: #000000;">1</span> <span style="color: #660033;">-maxdepth</span> <span style="color: #000000;">1</span> <span style="color: #000000; font-weight: bold;">|</span> <span style="color: #c20cb9; font-weight: bold;">head</span> <span style="color: #660033;">-n</span> <span style="color: #000000;">500</span> <span style="color: #000000; font-weight: bold;">|</span> <span style="color: #c20cb9; font-weight: bold;">xargs</span> <span style="color: #660033;">--</span> <span style="color: #c20cb9; font-weight: bold;">rm</span> <span style="color: #660033;">-rv</span>
     <span style="color: #000000; font-weight: bold;">done</span></pre></div></div>

<p>This modification will only run the desired commands when the system load is less than 2, it will wait for that condition to continue the loop.    This can be very handy for very large jobs needing to be run on loaded systems.  Especially jobs which can be subdivided into small tasks!</p>
]]></content:encoded>
			<wfw:commentRss>http://blog.apokalyptik.com/2007/08/20/scripting-without-killing-system-load/feed/</wfw:commentRss>
		<slash:comments>4</slash:comments>
		</item>
		<item>
		<title>Backgrounding Chained Commands in Bash</title>
		<link>http://blog.apokalyptik.com/2007/05/18/backgrounding-chained-commands-in-bash/</link>
		<comments>http://blog.apokalyptik.com/2007/05/18/backgrounding-chained-commands-in-bash/#comments</comments>
		<pubDate>Fri, 18 May 2007 21:51:44 +0000</pubDate>
		<dc:creator>apokalyptik</dc:creator>
				<category><![CDATA[Bash]]></category>
		<category><![CDATA[Business]]></category>
		<category><![CDATA[CLI]]></category>
		<category><![CDATA[Linux]]></category>
		<category><![CDATA[Personal]]></category>
		<category><![CDATA[Random Thoughts]]></category>
		<category><![CDATA[Software Development]]></category>

		<guid isPermaLink="false">http://blog.apokalyptik.com/2007/05/18/backgrounding-chained-commands-in-bash/</guid>
		<description><![CDATA[Sometimes it&#8217;s desirable to have a chain of commands backgrounded so that a multi-step process can be run in parallel.  And often times its not desirable to have yet another script made to do a simple task that doesn&#8217;t warrant the added complexity.  An example of this would be running backups in parallel. [...]]]></description>
			<content:encoded><![CDATA[<p>Sometimes it&#8217;s desirable to have a chain of commands backgrounded so that a multi-step process can be run in parallel.  And often times its not desirable to have yet another script made to do a simple task that doesn&#8217;t warrant the added complexity.  An example of this would be running backups in parallel.  The script sniplet below would allow up to 4 simultaneous tar backups to run at once &#8212; recording the start and stop times of each individually &#8212; and then wait for all the tar processes to finish before exiting</p>

<div class="wp_syntax"><div class="code"><pre class="bash" style="font-family:monospace;"><span style="color: #007800;">max_tar_count</span>=<span style="color: #000000;">4</span>
<span style="color: #000000; font-weight: bold;">for</span> i <span style="color: #000000; font-weight: bold;">in</span> <span style="color: #000000;">1</span> <span style="color: #000000;">3</span> <span style="color: #000000;">5</span> <span style="color: #000000;">7</span> <span style="color: #000000;">2</span> <span style="color: #000000;">4</span> <span style="color: #000000;">6</span> <span style="color: #000000;">8</span>
 <span style="color: #000000; font-weight: bold;">do</span>
 <span style="color: #007800;">cur_tar_count</span>=$<span style="color: #7a0874; font-weight: bold;">&#40;</span><span style="color: #c20cb9; font-weight: bold;">ps</span> wauxxx <span style="color: #000000; font-weight: bold;">|</span> <span style="color: #c20cb9; font-weight: bold;">grep</span> <span style="color: #660033;">-v</span> <span style="color: #c20cb9; font-weight: bold;">grep</span> <span style="color: #000000; font-weight: bold;">|</span> <span style="color: #c20cb9; font-weight: bold;">grep</span> <span style="color: #c20cb9; font-weight: bold;">tar</span> <span style="color: #000000; font-weight: bold;">|</span> <span style="color: #c20cb9; font-weight: bold;">wc</span> -l<span style="color: #7a0874; font-weight: bold;">&#41;</span>
 <span style="color: #000000; font-weight: bold;">if</span> <span style="color: #7a0874; font-weight: bold;">&#91;</span> <span style="color: #007800;">$cur_tar_count</span> <span style="color: #660033;">-ge</span> <span style="color: #007800;">$max_tar_count</span> <span style="color: #7a0874; font-weight: bold;">&#93;</span>
  <span style="color: #000000; font-weight: bold;">then</span> 
  <span style="color: #000000; font-weight: bold;">while</span> <span style="color: #7a0874; font-weight: bold;">&#91;</span> <span style="color: #007800;">$cur_tar_count</span> <span style="color: #660033;">-ge</span> <span style="color: #007800;">$max_tar_count</span> <span style="color: #7a0874; font-weight: bold;">&#93;</span>
   <span style="color: #000000; font-weight: bold;">do</span>
   <span style="color: #c20cb9; font-weight: bold;">sleep</span> <span style="color: #000000;">60</span>
   <span style="color: #007800;">cur_tar_count</span>=$<span style="color: #7a0874; font-weight: bold;">&#40;</span><span style="color: #c20cb9; font-weight: bold;">ps</span> wauxxx <span style="color: #000000; font-weight: bold;">|</span> <span style="color: #c20cb9; font-weight: bold;">grep</span> <span style="color: #660033;">-v</span> <span style="color: #c20cb9; font-weight: bold;">grep</span> <span style="color: #000000; font-weight: bold;">|</span> <span style="color: #c20cb9; font-weight: bold;">grep</span> <span style="color: #c20cb9; font-weight: bold;">tar</span> <span style="color: #000000; font-weight: bold;">|</span> <span style="color: #c20cb9; font-weight: bold;">wc</span> -l<span style="color: #7a0874; font-weight: bold;">&#41;</span>
  <span style="color: #000000; font-weight: bold;">done</span>
 <span style="color: #000000; font-weight: bold;">fi</span>
 <span style="color: #000000; font-weight: bold;">&lt;</span>font <span style="color: #007800;">color</span>=<span style="color: #ff0000;">&quot;red&quot;</span><span style="color: #000000; font-weight: bold;">&gt;</span><span style="color: #7a0874; font-weight: bold;">&#40;</span> <span style="color: #7a0874; font-weight: bold;">echo</span> <span style="color: #c20cb9; font-weight: bold;">date</span> <span style="color: #000000; font-weight: bold;">&amp;</span>gt; <span style="color: #000000; font-weight: bold;">/</span>backups<span style="color: #000000; font-weight: bold;">/</span><span style="color: #007800;">$i</span>.start <span style="color: #000000; font-weight: bold;">&amp;</span>amp;<span style="color: #000000; font-weight: bold;">&amp;</span>amp;<span style="color: #000000; font-weight: bold;">&lt;/</span>font<span style="color: #000000; font-weight: bold;">&gt;</span>
 <span style="color: #000000; font-weight: bold;">&lt;</span>font <span style="color: #007800;">color</span>=<span style="color: #ff0000;">&quot;red&quot;</span><span style="color: #000000; font-weight: bold;">&gt;</span>    <span style="color: #c20cb9; font-weight: bold;">tar</span> <span style="color: #000000; font-weight: bold;">/</span>backups<span style="color: #000000; font-weight: bold;">/</span><span style="color: #007800;">$i</span>.tar  <span style="color: #000000; font-weight: bold;">/</span>data<span style="color: #000000; font-weight: bold;">/</span><span style="color: #007800;">$i</span> <span style="color: #000000; font-weight: bold;">&amp;</span>amp;<span style="color: #000000; font-weight: bold;">&amp;</span>amp;<span style="color: #000000; font-weight: bold;">&lt;/</span>font<span style="color: #000000; font-weight: bold;">&gt;</span>
 <span style="color: #000000; font-weight: bold;">&lt;</span>font <span style="color: #007800;">color</span>=<span style="color: #ff0000;">&quot;red&quot;</span><span style="color: #000000; font-weight: bold;">&gt;</span>    <span style="color: #7a0874; font-weight: bold;">echo</span> <span style="color: #c20cb9; font-weight: bold;">date</span> <span style="color: #000000; font-weight: bold;">&amp;</span>gt; <span style="color: #000000; font-weight: bold;">/</span>backups<span style="color: #000000; font-weight: bold;">/</span><span style="color: #007800;">$i</span>.stop <span style="color: #7a0874; font-weight: bold;">&#41;</span><span style="color: #000000; font-weight: bold;">&amp;</span>amp;<span style="color: #000000; font-weight: bold;">&lt;/</span>font<span style="color: #000000; font-weight: bold;">&gt;</span>
<span style="color: #000000; font-weight: bold;">done</span>
<span style="color: #007800;">cur_tar_count</span>=$<span style="color: #7a0874; font-weight: bold;">&#40;</span><span style="color: #c20cb9; font-weight: bold;">ps</span> wauxxx <span style="color: #000000; font-weight: bold;">|</span> <span style="color: #c20cb9; font-weight: bold;">grep</span> <span style="color: #660033;">-v</span> <span style="color: #c20cb9; font-weight: bold;">grep</span> <span style="color: #000000; font-weight: bold;">|</span> <span style="color: #c20cb9; font-weight: bold;">grep</span> <span style="color: #c20cb9; font-weight: bold;">tar</span> <span style="color: #000000; font-weight: bold;">|</span> <span style="color: #c20cb9; font-weight: bold;">wc</span> -l<span style="color: #7a0874; font-weight: bold;">&#41;</span>
<span style="color: #000000; font-weight: bold;">while</span> <span style="color: #7a0874; font-weight: bold;">&#91;</span> <span style="color: #007800;">$cur_tar_count</span> <span style="color: #660033;">-gt</span> <span style="color: #000000;">0</span> <span style="color: #7a0874; font-weight: bold;">&#93;</span>
 <span style="color: #000000; font-weight: bold;">do</span>
 <span style="color: #c20cb9; font-weight: bold;">sleep</span> <span style="color: #000000;">60</span>
 <span style="color: #007800;">cur_tar_count</span>=$<span style="color: #7a0874; font-weight: bold;">&#40;</span><span style="color: #c20cb9; font-weight: bold;">ps</span> wauxxx <span style="color: #000000; font-weight: bold;">|</span> <span style="color: #c20cb9; font-weight: bold;">grep</span> <span style="color: #660033;">-v</span> <span style="color: #c20cb9; font-weight: bold;">grep</span> <span style="color: #000000; font-weight: bold;">|</span> <span style="color: #c20cb9; font-weight: bold;">grep</span> <span style="color: #c20cb9; font-weight: bold;">tar</span> <span style="color: #000000; font-weight: bold;">|</span> <span style="color: #c20cb9; font-weight: bold;">wc</span> -l<span style="color: #7a0874; font-weight: bold;">&#41;</span>
<span style="color: #000000; font-weight: bold;">done</span></pre></div></div>

<p>The real magick above is highlighted in red. You DO want that last loop in there to make the script wait until all the backups are really done before exiting.</p>
]]></content:encoded>
			<wfw:commentRss>http://blog.apokalyptik.com/2007/05/18/backgrounding-chained-commands-in-bash/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
	</channel>
</rss>
