<?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>Lâmôlabs &#187; shell</title>
	<atom:link href="http://www.lamolabs.org/blog/tag/shell/feed/" rel="self" type="application/rss+xml" />
	<link>http://www.lamolabs.org/blog</link>
	<description>Lame Oh Labs .... Linux &#38; Tech! Is there anything else?</description>
	<lastBuildDate>Thu, 29 Jul 2010 16:00:07 +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>[one-liner]: Previewing a Pretty Printed Text File using enscript &amp; ps2pdf</title>
		<link>http://www.lamolabs.org/blog/2842/one-liner-previewing-a-pretty-printed-text-file-using-enscript-ps2pdf/</link>
		<comments>http://www.lamolabs.org/blog/2842/one-liner-previewing-a-pretty-printed-text-file-using-enscript-ps2pdf/#comments</comments>
		<pubDate>Mon, 09 Nov 2009 15:45:07 +0000</pubDate>
		<dc:creator>slmingol</dc:creator>
				<category><![CDATA[tips & tricks]]></category>
		<category><![CDATA[one-liner]]></category>
		<category><![CDATA[shell]]></category>

		<guid isPermaLink="false">http://www.lamolabs.org/blog/?p=2842</guid>
		<description><![CDATA[Background

<p>Before my wife and I had kids we thought it would be nice to collect the United States quarters that were released during 1999 through 2008 to commemorate each of the 50 states. Seemed like something simple to do and would be a nice gift for the kids when they got older. So we bought [...]]]></description>
			<content:encoded><![CDATA[<h3>Background</h3>

<p>Before my wife and I had kids we thought it would be nice to collect the <a href="http://www.usmint.gov/mint_Programs/50sq_program/">United States quarters</a> that were released during <strong>1999</strong> through <strong>2008</strong> to commemorate each of the <strong>50 states</strong>. Seemed like something simple to do and would be a nice gift for the kids when they got older. So we bought a couple of the blue books which you can fill up with quarters as you find them. Each book contains 100 slots, 2 for each state. One slot is for the Philadelphia minted version of the quarter, and the other slot is for the Denver mint.</p>

<h3>Problem</h3>

<p>Well we ended up having 3 kids so we have to collect 300 quarters. The task of finding the quarters has been more of a dad task so when I have a chance, I&#8217;ll put a $5 dollar bill in various soda machines at work and go quarter fishing. This approach has been working fairly well and we&#8217;ve collected ~130 of the 300 quarters thus far.</p>

<p>However I&#8217;ve started getting to the point where I&#8217;m netting a lot of duplicates and the job of having to bring them home to weed through them is starting to get old. Having a list of which quarters we already have would sure be nice, so I could quickly nix any duplicates.</p>

<h3>Solution</h3>

<p>Of course I wanted a low tech solution, i.e. a piece of paper in my wallet would do the job, but how to do it?</p>

<p>The answer? A text file that I could maintain would suffice. No need for a bloated spreadsheet or some fancy handheld app. So I created a file, <strong>quarters.txt</strong>, like so:</p>


<div class="wp_codebox"><table width="100%" ><tr id="p28423"><td class="line_numbers"><pre>1
2
3
4
5
6
7
8
9
10
11
12
13
14
</pre></td><td class="code" id="p2842code3"><pre class="text" style="font-family:monospace;">State           Year  Cnt (P/D)
=====           ====  =========
Alabama         2003  2/1
Alaska          2008  3/1
Arizona         2008  3/0
Arkansas        2003  3/0
California      2005  3/0
Colorado        2006  3/0
Connecticut     1999  3/2
Delaware        1999  0/0
Florida         2004  2/0
Georgia         1999  2/1
Hawaii          2008  3/0
...</pre></td></tr></table></div>




<p>It&#8217;s about as simple a text file as you can get. 3 columns, <strong>State</strong>, <strong>Year</strong>, and <strong>Counts</strong>. The 3rd column shows how many <strong>P</strong> and <strong>D</strong> quarters I have for a given state. So for example, for Alabama, I&#8217;ve got 2 Philadelphia minted quarters, and 1 Denver minted.</p>

<p>So you&#8217;re probably wondering, &#8220;why the hell is this guy writing up this in a blog post?&#8221; </p>

<p>We&#8217;ll the interesting bit to this low tech solution is how I print this list out. For this task I make use of a pretty powerful <span class="caps">UNIX </span>command called <strong>enscript</strong>, which lets you do all kinds of nifty things to a text file to augment how it looks when it gets printed.</p>

<p>About the only thing enscript doesn&#8217;t do for you, is give you the ability to preview your text file prior to printing. To accomplish this bit, I made use of another powerful <span class="caps">UNIX </span>command called <strong>ps2pdf</strong>. This command will take a postscript file (ps) and convert it to a pdf file.</p>

<p>So putting all the pieces together I came up with the following command:</p>


<div class="wp_codebox"><table width="100%" ><tr id="p28424"><td class="line_numbers"><pre>1
</pre></td><td class="code" id="p2842code4"><pre class="bash" style="font-family:monospace;">enscript <span style="color: #660033;">--fancy-header</span> <span style="color: #660033;">-U</span> 4 quarters.txt <span style="color: #660033;">-o</span> - <span style="color: #000000; font-weight: bold;">|</span> <span style="color: #c20cb9; font-weight: bold;">ps2pdf</span> - quarters_sm.pdf</pre></td></tr></table></div>




<p>The first part of this command, will call enscript instructing it to convert the file <strong>quarters.txt</strong>, and print it to standard out <strong>-o -</strong>. The printout will include some <strong>fancy headers</strong> and enscript will print the text file out in what is called <strong>4 UP</strong>. This means that 4 pages will be printed on a single piece of paper. You could also print the page out in 2 <span class="caps">UP,</span> 8 <span class="caps">UP, </span>etc. It only needs to be a power of 2. <span class="caps">BTW,</span> 2 and 4 are the most commonly used, 8 is pretty hard to read.</p>

<p>The second part of this command passes the postscript generated by enscript through a <span class="caps">UNIX </span>pipe which gets picked up by ps2pdf, and converts it into a <span class="caps">PDF </span>file, quarters_sm.pdf.</p>

<p>From here you can check what the page would look like using your favorite <span class="caps">PDF </span>viewer, such as <strong>evince</strong> or <strong>xpdf</strong>. Once you&#8217;re comfortable with the page you can actually print it out from the <span class="caps">PDF </span>reader, or via the command-line.</p>

<p><span id="more-2842"></span></p>

<p>Here&#8217;s what the resulting <span class="caps">PDF </span>file looks like:</p>

<div id="attachment_2844" class="wp-caption aligncenter" style="width: 424px"><a href="http://www.lamolabs.org/blog/wp-content/uploads/2009/11/ss_quarters_pdf.png" rel="lightbox[2842]"><img src="http://www.lamolabs.org/blog/wp-content/uploads/2009/11/ss_quarters_pdf.png" alt="PDF file" title="ss_quarters_pdf" width="414" height="532" class="size-full wp-image-2844" /></a><p class="wp-caption-text">PDF file</p></div>

<p>Here&#8217;s a portion of the <span class="caps">PDF </span>file at a 150% of it&#8217;s original size:</p>

<div id="attachment_2845" class="wp-caption aligncenter" style="width: 597px"><a href="http://www.lamolabs.org/blog/wp-content/uploads/2009/11/ss_quarters_pdf_150pct.png" rel="lightbox[2842]"><img src="http://www.lamolabs.org/blog/wp-content/uploads/2009/11/ss_quarters_pdf_150pct.png" alt="screenshot of quarters_sm.pdf at 150%" title="ss_quarters_pdf_150pct" width="587" height="638" class="size-full wp-image-2845" /></a><p class="wp-caption-text">screenshot of quarters_sm.pdf at 150%</p></div>

<h3>Useful Links</h3>


<ul>
<li><a href="https://www.cs.utk.edu/help/doku.php?id=printing%3Alinux_printing_guide">enscript printing guide</a></li>
</ul>



<p><strong><span class="caps">NOTE</span>:</strong> For further details regarding my <strong>one-liner</strong> blog posts, check out my <a href="http://www.lamolabs.org/blog/2147/intro-blogs-one-liner-code-block-style-guides/">one-liner style guide primer</a>.</p><div id="wherego_related"><h3>Readers who viewed this page, also viewed:</h3><ul><li><a href="http://www.lamolabs.org/blog/1766/pushing-pulling-files-around-using-tar-ssh-scp-rsync/" rel="bookmark" class="wherego_title">Pushing &#038; Pulling Files Around Using tar, ssh, scp, &#038; rsync</a></li><li><a href="http://www.lamolabs.org/blog/2745/one-liner-copying-moving-files-efficiently-with-xargs/" rel="bookmark" class="wherego_title">[one-liner]: Copying &#038; Moving Files efficiently with xargs</a></li><li><a href="http://www.lamolabs.org/blog/2147/intro-blogs-one-liner-code-block-style-guides/" rel="bookmark" class="wherego_title">Intro to my Blog&#8217;s [one-liner] Code Block Style Guides</a></li><li>Powered by <a href="http://ajaydsouza.com/wordpress/plugins/where-did-they-go-from-here/">Where did they go from here?</a></li></ul></div>]]></content:encoded>
			<wfw:commentRss>http://www.lamolabs.org/blog/2842/one-liner-previewing-a-pretty-printed-text-file-using-enscript-ps2pdf/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>[one-liner]: Copying &amp; Moving Files efficiently with xargs</title>
		<link>http://www.lamolabs.org/blog/2745/one-liner-copying-moving-files-efficiently-with-xargs/</link>
		<comments>http://www.lamolabs.org/blog/2745/one-liner-copying-moving-files-efficiently-with-xargs/#comments</comments>
		<pubDate>Thu, 15 Oct 2009 14:13:24 +0000</pubDate>
		<dc:creator>slmingol</dc:creator>
				<category><![CDATA[tips & tricks]]></category>
		<category><![CDATA[one-liner]]></category>
		<category><![CDATA[shell]]></category>

		<guid isPermaLink="false">http://www.lamolabs.org/blog/?p=2745</guid>
		<description><![CDATA[Background

<p>From time to time I need to move and/or copy a subset of files from one directory to another. I typically would use something like one of these to do the task:</p>


1
2
3
4
5
#-- COPY
find . -type f -ctime -1 &#124; xargs -I &#123;&#125; cp &#123;&#125; /some/other/directory
&#160;
#-- MOVE
find . -type f -ctime -1 &#124; xargs -I &#123;&#125; [...]]]></description>
			<content:encoded><![CDATA[<h4>Background</h4>

<p>From time to time I need to move and/or copy a subset of files from one directory to another. I typically would use something like one of these to do the task:</p>


<div class="wp_codebox"><table width="100%" ><tr id="p27458"><td class="line_numbers"><pre>1
2
3
4
5
</pre></td><td class="code" id="p2745code8"><pre class="bash" style="font-family:monospace;"><span style="color: #666666; font-style: italic;">#-- COPY</span>
<span style="color: #c20cb9; font-weight: bold;">find</span> . <span style="color: #660033;">-type</span> f <span style="color: #660033;">-ctime</span> <span style="color: #660033;">-1</span> <span style="color: #000000; font-weight: bold;">|</span> <span style="color: #c20cb9; font-weight: bold;">xargs</span> <span style="color: #660033;">-I</span> <span style="color: #7a0874; font-weight: bold;">&#123;</span><span style="color: #7a0874; font-weight: bold;">&#125;</span> <span style="color: #c20cb9; font-weight: bold;">cp</span> <span style="color: #7a0874; font-weight: bold;">&#123;</span><span style="color: #7a0874; font-weight: bold;">&#125;</span> <span style="color: #000000; font-weight: bold;">/</span>some<span style="color: #000000; font-weight: bold;">/</span>other<span style="color: #000000; font-weight: bold;">/</span>directory
&nbsp;
<span style="color: #666666; font-style: italic;">#-- MOVE</span>
<span style="color: #c20cb9; font-weight: bold;">find</span> . <span style="color: #660033;">-type</span> f <span style="color: #660033;">-ctime</span> <span style="color: #660033;">-1</span> <span style="color: #000000; font-weight: bold;">|</span> <span style="color: #c20cb9; font-weight: bold;">xargs</span> <span style="color: #660033;">-I</span> <span style="color: #7a0874; font-weight: bold;">&#123;</span><span style="color: #7a0874; font-weight: bold;">&#125;</span> <span style="color: #c20cb9; font-weight: bold;">mv</span> <span style="color: #7a0874; font-weight: bold;">&#123;</span><span style="color: #7a0874; font-weight: bold;">&#125;</span> <span style="color: #000000; font-weight: bold;">/</span>some<span style="color: #000000; font-weight: bold;">/</span>other<span style="color: #000000; font-weight: bold;">/</span>directory</pre></td></tr></table></div>




<p><strong><span class="caps">NOTE</span>:</strong> The <strong>1st command</strong> finds all the files in the current directory that are <em>less than 24 hours old</em>, and <strong>copies</strong> them to <em>/some/other/directory</em>. The <strong>2nd command</strong> finds all the files in the current directory that are <em>less than 24 hours old</em>, and <strong>moves</strong> them to <em>/some/other/directory</em>.</p>

<p>But then I realized that by using xargs&#8217; <strong>&ndash;I switch</strong> I was basically limiting <strong>xargs</strong> to doing a single file at a time. According to xargs&#8217; man page, when you use the <strong>&ndash;I switch</strong> you&#8217;re implying the <strong>&ndash;x switch</strong> <span class="caps">AND </span>the <strong>&ndash;L 1 switch</strong>. The <strong>L switch</strong> is what tells <strong>xargs</strong> how many lines of input to process at a time, so we&#8217;re basically telling it to only handle one file at a time. This made no sense. I was unintentially limiting xargs&#8217; ability to optimize the command-line. So I found a better way.</p>

<h4>New Approach</h4>

<p>By utilizing 2 little used switches (&ndash;t | &ndash;&ndash;target-directory) on <strong>cp</strong> and <strong>mv</strong> I could un-tie <strong>xargs&#8217;</strong> hands.</p>

<h5>copy</h5>


<div class="wp_codebox"><table width="100%" ><tr id="p27459"><td class="line_numbers"><pre>1
2
3
4
5
</pre></td><td class="code" id="p2745code9"><pre class="bash" style="font-family:monospace;"><span style="color: #666666; font-style: italic;"># long form</span>
<span style="color: #c20cb9; font-weight: bold;">find</span> . <span style="color: #660033;">-type</span> f <span style="color: #660033;">-ctime</span> <span style="color: #660033;">-1</span> <span style="color: #000000; font-weight: bold;">|</span> <span style="color: #c20cb9; font-weight: bold;">xargs</span> <span style="color: #660033;">-0</span> <span style="color: #c20cb9; font-weight: bold;">cp</span> <span style="color: #660033;">--target-directory</span>=<span style="color: #000000; font-weight: bold;">/</span>some<span style="color: #000000; font-weight: bold;">/</span>other<span style="color: #000000; font-weight: bold;">/</span>directory
&nbsp;
<span style="color: #666666; font-style: italic;"># short form</span>
<span style="color: #c20cb9; font-weight: bold;">find</span> . <span style="color: #660033;">-type</span> f <span style="color: #660033;">-ctime</span> <span style="color: #660033;">-1</span> <span style="color: #000000; font-weight: bold;">|</span> <span style="color: #c20cb9; font-weight: bold;">xargs</span> <span style="color: #660033;">-0</span> <span style="color: #c20cb9; font-weight: bold;">cp</span> <span style="color: #660033;">-t</span> <span style="color: #000000; font-weight: bold;">/</span>some<span style="color: #000000; font-weight: bold;">/</span>other<span style="color: #000000; font-weight: bold;">/</span>directory</pre></td></tr></table></div>




<h5>move</h5>


<div class="wp_codebox"><table width="100%" ><tr id="p274510"><td class="line_numbers"><pre>1
2
3
4
5
</pre></td><td class="code" id="p2745code10"><pre class="bash" style="font-family:monospace;"><span style="color: #666666; font-style: italic;"># long form</span>
<span style="color: #c20cb9; font-weight: bold;">find</span> . <span style="color: #660033;">-type</span> f <span style="color: #660033;">-ctime</span> <span style="color: #660033;">-1</span> <span style="color: #000000; font-weight: bold;">|</span> <span style="color: #c20cb9; font-weight: bold;">xargs</span> <span style="color: #660033;">-0</span> <span style="color: #c20cb9; font-weight: bold;">mv</span> <span style="color: #660033;">--target-directory</span>=<span style="color: #000000; font-weight: bold;">/</span>some<span style="color: #000000; font-weight: bold;">/</span>other<span style="color: #000000; font-weight: bold;">/</span>directory
&nbsp;
<span style="color: #666666; font-style: italic;"># short form</span>
<span style="color: #c20cb9; font-weight: bold;">find</span> . <span style="color: #660033;">-type</span> f <span style="color: #660033;">-ctime</span> <span style="color: #660033;">-1</span> <span style="color: #000000; font-weight: bold;">|</span> <span style="color: #c20cb9; font-weight: bold;">xargs</span> <span style="color: #660033;">-0</span> <span style="color: #c20cb9; font-weight: bold;">mv</span> <span style="color: #660033;">-t</span> <span style="color: #000000; font-weight: bold;">/</span>some<span style="color: #000000; font-weight: bold;">/</span>other<span style="color: #000000; font-weight: bold;">/</span>directory</pre></td></tr></table></div>




<p><strong><span class="caps">NOTE</span>:</strong> For further details regarding my <strong>one-liner</strong> blog posts, check out my <a href="http://www.lamolabs.org/blog/2147/intro-blogs-one-liner-code-block-style-guides/">one-liner style guide primer</a>.</p><div id="wherego_related"><h3>Readers who viewed this page, also viewed:</h3><ul><li><a href="http://www.lamolabs.org/blog/2681/one-liner-how-to-use-the-bash-shells-export-command/" rel="bookmark" class="wherego_title">[one-liner]: How to Use the Bash Shell&#8217;s export Command</a></li><li><a href="http://www.lamolabs.org/blog/2147/intro-blogs-one-liner-code-block-style-guides/" rel="bookmark" class="wherego_title">Intro to my Blog&#8217;s [one-liner] Code Block Style Guides</a></li><li>Powered by <a href="http://ajaydsouza.com/wordpress/plugins/where-did-they-go-from-here/">Where did they go from here?</a></li></ul></div>]]></content:encoded>
			<wfw:commentRss>http://www.lamolabs.org/blog/2745/one-liner-copying-moving-files-efficiently-with-xargs/feed/</wfw:commentRss>
		<slash:comments>4</slash:comments>
		</item>
		<item>
		<title>[one-liner]: How to Use the Bash Shell&#8217;s export Command</title>
		<link>http://www.lamolabs.org/blog/2681/one-liner-how-to-use-the-bash-shells-export-command/</link>
		<comments>http://www.lamolabs.org/blog/2681/one-liner-how-to-use-the-bash-shells-export-command/#comments</comments>
		<pubDate>Thu, 01 Oct 2009 14:35:12 +0000</pubDate>
		<dc:creator>slmingol</dc:creator>
				<category><![CDATA[tips & tricks]]></category>
		<category><![CDATA[bash]]></category>
		<category><![CDATA[one-liner]]></category>
		<category><![CDATA[shell]]></category>
		<category><![CDATA[tip]]></category>

		<guid isPermaLink="false">http://www.lamolabs.org/blog/?p=2681</guid>
		<description><![CDATA[Problem

<p>Recently at my day job I&#8217;ve been having to go through some pretty old Bash scripts that I&#8217;ve basically inherited. As I&#8217;ve been going through them I&#8217;ve been seeing a lot of confusion as to the proper use of Bash&#8217;s export command. The major offense? Not really understand whether a particular variables needs to be [...]]]></description>
			<content:encoded><![CDATA[<h4>Problem</h4>

<p>Recently at my day job I&#8217;ve been having to go through some pretty old Bash scripts that I&#8217;ve basically inherited. As I&#8217;ve been going through them I&#8217;ve been seeing a lot of confusion as to the proper use of Bash&#8217;s <strong>export command</strong>. The major offense? Not really understand whether a particular variables needs to be exported, or not. So I thought I&#8217;d take a moment just to clarify when and when not to use export.</p>

<p>The <strong>export command</strong> has really only one true purpose. To <strong>mark and/or unmark variables</strong> (and functions) that you want to have automatically exported to environments of <strong>subsequently executed commands</strong>. So if you create a script that calls other commands, and you want to push variables into the environment of these commands, then you&#8217;ll want to use <strong>export</strong>.</p>

<h4>Example #1 (without export)</h4>

<p>For example, let&#8217;s say we have the following 2 scripts:</p>


<div class="wp_codebox"><table width="100%" ><tr id="p268123"><td class="line_numbers"><pre>1
2
3
4
5
6
7
</pre></td><td class="code" id="p2681code23"><pre class="bash" style="font-family:monospace;"><span style="color: #666666; font-style: italic;">#!/bin/bash</span>
<span style="color: #666666; font-style: italic;"># script #1: parent.bash</span>
&nbsp;
<span style="color: #007800;">var1</span>=<span style="color: #ff0000;">&quot;this was set by the parent shell script&quot;</span>
<span style="color: #7a0874; font-weight: bold;">echo</span> <span style="color: #ff0000;">&quot;inside $0 script: <span style="color: #007800;">$var1</span>&quot;</span>
&nbsp;
.<span style="color: #000000; font-weight: bold;">/</span>child.bash</pre></td></tr></table></div>





<div class="wp_codebox"><table width="100%" ><tr id="p268124"><td class="line_numbers"><pre>1
2
3
4
</pre></td><td class="code" id="p2681code24"><pre class="bash" style="font-family:monospace;"><span style="color: #666666; font-style: italic;">#!/bin/bash</span>
<span style="color: #666666; font-style: italic;"># script #2: child.bash</span>
&nbsp;
<span style="color: #7a0874; font-weight: bold;">echo</span> <span style="color: #ff0000;">&quot;inside $0 script: <span style="color: #007800;">$var1</span>&quot;</span></pre></td></tr></table></div>




<p>And when I run the script <strong>parent.bash</strong> I get this output:</p>


<div class="wp_codebox"><table width="100%" ><tr id="p268125"><td class="line_numbers"><pre>1
2
3
4
5
</pre></td><td class="code" id="p2681code25"><pre class="bash" style="font-family:monospace;"><span style="color: #666666; font-style: italic;"># output from parent.bash &amp; child.bash (without export)</span>
&nbsp;
<span style="color: #000000; font-weight: bold;">%</span> .<span style="color: #000000; font-weight: bold;">/</span>parent.bash
inside .<span style="color: #000000; font-weight: bold;">/</span>parent.bash script: this was <span style="color: #000000; font-weight: bold;">set</span> by the parent shell script
inside .<span style="color: #000000; font-weight: bold;">/</span>child.bash script:</pre></td></tr></table></div>




<p>Notice how the variable <strong>$var1</strong>, which was set in the <strong>parent.bash</strong> script, didn&#8217;t get displayed by the <strong>child.bash</strong> script? Now watch this example with the variable <strong>$var1</strong> exported in the <strong>parent.bash</strong> script.</p>

<h4>Example #2 (with export)</h4>


<div class="wp_codebox"><table width="100%" ><tr id="p268126"><td class="line_numbers"><pre>1
2
3
4
5
6
7
</pre></td><td class="code" id="p2681code26"><pre class="bash" style="font-family:monospace;"><span style="color: #666666; font-style: italic;">#!/bin/bash</span>
<span style="color: #666666; font-style: italic;"># script #1: parent.bash</span>
&nbsp;
<span style="color: #7a0874; font-weight: bold;">export</span> <span style="color: #007800;">var1</span>=<span style="color: #ff0000;">&quot;this was set by the parent shell script&quot;</span>
<span style="color: #7a0874; font-weight: bold;">echo</span> <span style="color: #ff0000;">&quot;inside $0 script: <span style="color: #007800;">$var1</span>&quot;</span>
&nbsp;
.<span style="color: #000000; font-weight: bold;">/</span>child.bash</pre></td></tr></table></div>





<div class="wp_codebox"><table width="100%" ><tr id="p268127"><td class="line_numbers"><pre>1
2
3
4
</pre></td><td class="code" id="p2681code27"><pre class="bash" style="font-family:monospace;"><span style="color: #666666; font-style: italic;">#!/bin/bash</span>
<span style="color: #666666; font-style: italic;"># script #2: child.bash</span>
&nbsp;
<span style="color: #7a0874; font-weight: bold;">echo</span> <span style="color: #ff0000;">&quot;inside $0 script: <span style="color: #007800;">$var1</span>&quot;</span></pre></td></tr></table></div>




<p>And when we run <strong>parent.bash</strong></p>


<div class="wp_codebox"><table width="100%" ><tr id="p268128"><td class="line_numbers"><pre>1
2
3
4
5
</pre></td><td class="code" id="p2681code28"><pre class="bash" style="font-family:monospace;"><span style="color: #666666; font-style: italic;"># output from parent.bash &amp; child.bash (with export)</span>
&nbsp;
<span style="color: #000000; font-weight: bold;">%</span> .<span style="color: #000000; font-weight: bold;">/</span>parent.bash
inside .<span style="color: #000000; font-weight: bold;">/</span>parent.bash script: this was <span style="color: #000000; font-weight: bold;">set</span> my the parent shell script
inside .<span style="color: #000000; font-weight: bold;">/</span>child.bash script: this was <span style="color: #000000; font-weight: bold;">set</span> my the parent shell script</pre></td></tr></table></div>




<h4>Example #3 (un-exporting)</h4>

<p>Export isn&#8217;t just a one trick pony. It can also unmark a previously exported variable.</p>

<p><span id="more-2681"></span></p>


<div class="wp_codebox"><table width="100%" ><tr id="p268129"><td class="line_numbers"><pre>1
2
3
4
5
6
7
8
9
</pre></td><td class="code" id="p2681code29"><pre class="bash" style="font-family:monospace;"><span style="color: #666666; font-style: italic;">#!/bin/bash</span>
<span style="color: #666666; font-style: italic;"># script #1: parent.bash</span>
&nbsp;
<span style="color: #7a0874; font-weight: bold;">export</span> <span style="color: #007800;">var1</span>=<span style="color: #ff0000;">&quot;this was set by the parent shell script&quot;</span>
<span style="color: #7a0874; font-weight: bold;">echo</span> <span style="color: #ff0000;">&quot;inside $0 script: <span style="color: #007800;">$var1</span>&quot;</span>
&nbsp;
.<span style="color: #000000; font-weight: bold;">/</span>child.bash
<span style="color: #7a0874; font-weight: bold;">export</span> <span style="color: #660033;">-n</span> var1
.<span style="color: #000000; font-weight: bold;">/</span>child.bash</pre></td></tr></table></div>





<div class="wp_codebox"><table width="100%" ><tr id="p268130"><td class="line_numbers"><pre>1
2
3
4
</pre></td><td class="code" id="p2681code30"><pre class="bash" style="font-family:monospace;"><span style="color: #666666; font-style: italic;">#!/bin/bash</span>
<span style="color: #666666; font-style: italic;"># script #2: child.bash</span>
&nbsp;
<span style="color: #7a0874; font-weight: bold;">echo</span> <span style="color: #ff0000;">&quot;inside $0 script: <span style="color: #007800;">$var1</span>&quot;</span></pre></td></tr></table></div>





<div class="wp_codebox"><table width="100%" ><tr id="p268131"><td class="line_numbers"><pre>1
2
3
4
5
6
</pre></td><td class="code" id="p2681code31"><pre class="bash" style="font-family:monospace;"><span style="color: #666666; font-style: italic;"># output from parent.bash &amp; child.bash (with a before/after export)</span>
&nbsp;
<span style="color: #000000; font-weight: bold;">%</span> .<span style="color: #000000; font-weight: bold;">/</span>parent.bash
inside .<span style="color: #000000; font-weight: bold;">/</span>parent.bash script: this was <span style="color: #000000; font-weight: bold;">set</span> by the parent shell script
inside .<span style="color: #000000; font-weight: bold;">/</span>child.bash script: this was <span style="color: #000000; font-weight: bold;">set</span> by the parent shell script
inside .<span style="color: #000000; font-weight: bold;">/</span>child.bash script:</pre></td></tr></table></div>




<p>Here we see the effects of the <strong>export -n</strong> on <strong>child.bash</strong> the <strong>2nd time</strong> it&#8217;s called.</p>

<h4>Which Variables are Flagged for Export?</h4>

<p>You can use the command <strong>export -p</strong> to get a list of all the variables marked for export, like this:</p>


<div class="wp_codebox"><table width="100%" ><tr id="p268132"><td class="line_numbers"><pre>1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
</pre></td><td class="code" id="p2681code32"><pre class="bash" style="font-family:monospace;"><span style="color: #000000; font-weight: bold;">%</span> <span style="color: #7a0874; font-weight: bold;">export</span> <span style="color: #660033;">-p</span>
<span style="color: #7a0874; font-weight: bold;">declare</span> <span style="color: #660033;">-x</span> <span style="color: #007800;">CCACHE_DIR</span>=<span style="color: #ff0000;">&quot;/var/cache/ccache&quot;</span>
<span style="color: #7a0874; font-weight: bold;">declare</span> <span style="color: #660033;">-x</span> <span style="color: #007800;">CCACHE_UMASK</span>=<span style="color: #ff0000;">&quot;002&quot;</span>
<span style="color: #7a0874; font-weight: bold;">declare</span> <span style="color: #660033;">-x</span> <span style="color: #007800;">COLORTERM</span>=<span style="color: #ff0000;">&quot;gnome-terminal&quot;</span>
<span style="color: #7a0874; font-weight: bold;">declare</span> <span style="color: #660033;">-x</span> <span style="color: #007800;">CVS_RSH</span>=<span style="color: #ff0000;">&quot;ssh&quot;</span>
<span style="color: #7a0874; font-weight: bold;">declare</span> <span style="color: #660033;">-x</span> <span style="color: #007800;">DESKTOP_SESSION</span>=<span style="color: #ff0000;">&quot;gnome&quot;</span>
<span style="color: #7a0874; font-weight: bold;">declare</span> <span style="color: #660033;">-x</span> <span style="color: #007800;">DISPLAY</span>=<span style="color: #ff0000;">&quot;:0.0&quot;</span>
...
...
<span style="color: #7a0874; font-weight: bold;">declare</span> <span style="color: #660033;">-x</span> <span style="color: #007800;">SHELL</span>=<span style="color: #ff0000;">&quot;/bin/bash&quot;</span>
<span style="color: #7a0874; font-weight: bold;">declare</span> <span style="color: #660033;">-x</span> <span style="color: #007800;">SHLVL</span>=<span style="color: #ff0000;">&quot;4&quot;</span>
<span style="color: #7a0874; font-weight: bold;">declare</span> <span style="color: #660033;">-x</span> <span style="color: #007800;">TERM</span>=<span style="color: #ff0000;">&quot;xterm&quot;</span>
<span style="color: #7a0874; font-weight: bold;">declare</span> <span style="color: #660033;">-x</span> <span style="color: #007800;">USER</span>=<span style="color: #ff0000;">&quot;root&quot;</span>
<span style="color: #7a0874; font-weight: bold;">declare</span> <span style="color: #660033;">-x</span> <span style="color: #007800;">WINDOWID</span>=<span style="color: #ff0000;">&quot;77021004&quot;</span>
<span style="color: #7a0874; font-weight: bold;">declare</span> <span style="color: #660033;">-x</span> <span style="color: #007800;">XMODIFIERS</span>=<span style="color: #ff0000;">&quot;@im=imsettings&quot;</span>
<span style="color: #7a0874; font-weight: bold;">declare</span> <span style="color: #660033;">-x</span> <span style="color: #007800;">var1</span>=<span style="color: #ff0000;">&quot;this was set by the parent shell script&quot;</span></pre></td></tr></table></div>




<h4>What else?</h4>

<p>There is one additional trick related to exporting variables, but it doesn&#8217;t make use of the export command. It uses Bash&#8217;s <strong>set</strong> command. This command allows you to automatically export <span class="caps">ALL </span>the variables that have been modified or created to the environment of subsequent commands. </p>

<p>Here&#8217;s a quick example:</p>


<div class="wp_codebox"><table width="100%" ><tr id="p268133"><td class="line_numbers"><pre>1
2
3
4
5
6
7
8
9
10
11
12
</pre></td><td class="code" id="p2681code33"><pre class="bash" style="font-family:monospace;"><span style="color: #666666; font-style: italic;">#!/bin/bash</span>
<span style="color: #666666; font-style: italic;"># script #1: parent.bash</span>
&nbsp;
<span style="color: #666666; font-style: italic;"># automatically export EVERYTHING</span>
<span style="color: #000000; font-weight: bold;">set</span> <span style="color: #660033;">-a</span>
&nbsp;
<span style="color: #007800;">var1</span>=<span style="color: #ff0000;">&quot;this was set by the parent shell script&quot;</span>
<span style="color: #007800;">var2</span>=<span style="color: #ff0000;">&quot;this was set by the parent shell too&quot;</span>
<span style="color: #7a0874; font-weight: bold;">echo</span> <span style="color: #ff0000;">&quot;inside $0 script: <span style="color: #007800;">$var1</span>&quot;</span>
<span style="color: #7a0874; font-weight: bold;">echo</span> <span style="color: #ff0000;">&quot;inside $0 script: <span style="color: #007800;">$var2</span>&quot;</span>
&nbsp;
.<span style="color: #000000; font-weight: bold;">/</span>child.bash</pre></td></tr></table></div>





<div class="wp_codebox"><table width="100%" ><tr id="p268134"><td class="line_numbers"><pre>1
2
3
4
5
</pre></td><td class="code" id="p2681code34"><pre class="bash" style="font-family:monospace;"><span style="color: #666666; font-style: italic;">#!/bin/bash</span>
<span style="color: #666666; font-style: italic;"># script #2: child.bash</span>
&nbsp;
<span style="color: #7a0874; font-weight: bold;">echo</span> <span style="color: #ff0000;">&quot;inside $0 script: <span style="color: #007800;">$var1</span>&quot;</span>
<span style="color: #7a0874; font-weight: bold;">echo</span> <span style="color: #ff0000;">&quot;inside $0 script: <span style="color: #007800;">$var2</span>&quot;</span></pre></td></tr></table></div>




<h4>Useful links</h4>


<ul>
<li><a href="http://www.ibm.com/developerworks/library/l-bash.html">Daniel Robbin&#8217;s 3 part Bash tutorial</a></li>
<li><a href="http://www.gnu.org/software/bash/bash.html">Bash Reference Manual</a></li>
</ul>

<div id="wherego_related"><h3>Readers who viewed this page, also viewed:</h3><ul><li><a href="http://www.lamolabs.org/blog/1724/one-liner-bash-wait-command/" rel="bookmark" class="wherego_title">[one-liner]: Bash&#8217;s wait Command</a></li><li>Powered by <a href="http://ajaydsouza.com/wordpress/plugins/where-did-they-go-from-here/">Where did they go from here?</a></li></ul></div>]]></content:encoded>
			<wfw:commentRss>http://www.lamolabs.org/blog/2681/one-liner-how-to-use-the-bash-shells-export-command/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>[one-liner]: Determining a Hard Drive&#8217;s Manufaturer Under Fedora 10 &amp; CentOS 5</title>
		<link>http://www.lamolabs.org/blog/2602/one-liner-determining-a-hard-drives-manufaturer-under-fedora-10-centos-5/</link>
		<comments>http://www.lamolabs.org/blog/2602/one-liner-determining-a-hard-drives-manufaturer-under-fedora-10-centos-5/#comments</comments>
		<pubDate>Mon, 21 Sep 2009 14:13:51 +0000</pubDate>
		<dc:creator>slmingol</dc:creator>
				<category><![CDATA[tips & tricks]]></category>
		<category><![CDATA[bash]]></category>
		<category><![CDATA[centos]]></category>
		<category><![CDATA[fedora]]></category>
		<category><![CDATA[redhat]]></category>
		<category><![CDATA[shell]]></category>
		<category><![CDATA[tip]]></category>

		<guid isPermaLink="false">http://www.lamolabs.org/blog/?p=2602</guid>
		<description><![CDATA[Background

<p>I recently saw a post over on Linux Journal that discussed how to glean information about a system&#8217;s hard drive, such as its serial number, without having to actually open up the case and physically check it. So I thought I&#8217;d take the opportunity to write up a blog post with the specifics of how [...]]]></description>
			<content:encoded><![CDATA[<h4>Background</h4>

<p>I recently saw a <a href="http://www.linuxjournal.com/content/tech-tip-retrieve-disk-info-command-line">post</a> over on <a href="http://www.linuxjournal.com/">Linux Journal</a> that discussed how to glean information about a system&#8217;s hard drive, such as its serial number, without having to actually open up the case and physically check it. So I thought I&#8217;d take the opportunity to write up a blog post with the specifics of how to do this under Fedora &amp; CentOS, just so I&#8217;d have this info handy for future use.</p>

<p><span class="caps">BTW,</span> I was able to accomplish this task several different ways, so this post will cover all the different ways that I could get this info.</p>

<h4>Command #1: lshw</h4>

<p>This is probably the best tool for getting at a system&#8217;s internals. First make sure it&#8217;s installed.</p>


<div class="wp_codebox"><table width="100%" ><tr id="p260244"><td class="line_numbers"><pre>1
</pre></td><td class="code" id="p2602code44"><pre class="bash" style="font-family:monospace;">yum <span style="color: #c20cb9; font-weight: bold;">install</span> lshw</pre></td></tr></table></div>




<p>For our example you would run the command <strong>lshw -class disk</strong>:</p>


<div class="wp_codebox"><table width="100%" ><tr id="p260245"><td class="line_numbers"><pre>1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
</pre></td><td class="code" id="p2602code45"><pre class="bash" style="font-family:monospace;"><span style="color: #000000; font-weight: bold;">%</span> lshw <span style="color: #660033;">-class</span> disk
  <span style="color: #000000; font-weight: bold;">*</span>-disk                  
       description: ATA Disk
       product: HTS726060M9AT00
       vendor: Hitachi
       physical <span style="color: #c20cb9; font-weight: bold;">id</span>: 0
       bus info: scsi<span style="color: #000000; font-weight: bold;">@</span>0:0.0.0
       logical name: <span style="color: #000000; font-weight: bold;">/</span>dev<span style="color: #000000; font-weight: bold;">/</span>sda
       version: MH4O
       serial: MRH403M4GS551Y
       <span style="color: #c20cb9; font-weight: bold;">size</span>: 55GiB <span style="color: #7a0874; font-weight: bold;">&#40;</span>60GB<span style="color: #7a0874; font-weight: bold;">&#41;</span>
       capabilities: partitioned partitioned:dos
       configuration: <span style="color: #007800;">ansiversion</span>=5 <span style="color: #007800;">signature</span>=cccdcccd
  <span style="color: #000000; font-weight: bold;">*</span>-cdrom
       description: DVD reader
       product: UJDA755yDVD<span style="color: #000000; font-weight: bold;">/</span>CDRW
       vendor: MATSHITA
       physical <span style="color: #c20cb9; font-weight: bold;">id</span>: 1
       bus info: scsi<span style="color: #000000; font-weight: bold;">@</span>1:0.0.0
       logical name: <span style="color: #000000; font-weight: bold;">/</span>dev<span style="color: #000000; font-weight: bold;">/</span>cdrom
       logical name: <span style="color: #000000; font-weight: bold;">/</span>dev<span style="color: #000000; font-weight: bold;">/</span>cdrw
       logical name: <span style="color: #000000; font-weight: bold;">/</span>dev<span style="color: #000000; font-weight: bold;">/</span>dvd
       logical name: <span style="color: #000000; font-weight: bold;">/</span>dev<span style="color: #000000; font-weight: bold;">/</span>scd0
       logical name: <span style="color: #000000; font-weight: bold;">/</span>dev<span style="color: #000000; font-weight: bold;">/</span>sr0
       version: 1.71
       capabilities: removable audio cd-r cd-rw dvd
       configuration: <span style="color: #007800;">ansiversion</span>=5 <span style="color: #007800;">status</span>=nodisc</pre></td></tr></table></div>




<p>The first section that&#8217;s returned is called <strong>-disk</strong>. Here&#8217;s you&#8217;ll see the vendor: Hitachi, the product number, <span class="caps">HTS726060M9AT00, </span>and my serial number: <span class="caps">MRH403M4GS551Y.</span></p>

<h4>Command #2: smartctl</h4>

<p>The next tool that would give this type of info is called <strong>smartctl</strong>. It&#8217;s a tool that&#8217;s part of the smartmontool package. You may be familiar with the acronym <a href="http://en.wikipedia.org/wiki/S.M.A.R.T"><span class="caps">S.M.A.R.T.</span></a>. The acronym stands for: <strong>Self-Monitoring, Analysis, and Reporting Technology</strong>. This is a standard that most modern disks have in which vital statistics about a disk drive are provided through a standard <span class="caps">API.</span> Here&#8217;s how to install it.</p>


<div class="wp_codebox"><table width="100%" ><tr id="p260246"><td class="line_numbers"><pre>1
</pre></td><td class="code" id="p2602code46"><pre class="bash" style="font-family:monospace;">yum <span style="color: #c20cb9; font-weight: bold;">install</span> smartmontools</pre></td></tr></table></div>




<p>&#8230;and once installed you can use the bundled in tool <strong>smartctl</strong> like so:</p>

<p><span id="more-2602"></span></p>


<div class="wp_codebox"><table width="100%" ><tr id="p260247"><td class="line_numbers"><pre>1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
</pre></td><td class="code" id="p2602code47"><pre class="bash" style="font-family:monospace;"><span style="color: #666666; font-style: italic;"># smartctl example</span>
&nbsp;
<span style="color: #000000; font-weight: bold;">%</span> smartctl <span style="color: #660033;">-i</span> <span style="color: #000000; font-weight: bold;">/</span>dev<span style="color: #000000; font-weight: bold;">/</span>sda
smartctl version 5.38 <span style="color: #7a0874; font-weight: bold;">&#91;</span>i386-redhat-linux-gnu<span style="color: #7a0874; font-weight: bold;">&#93;</span> Copyright <span style="color: #7a0874; font-weight: bold;">&#40;</span>C<span style="color: #7a0874; font-weight: bold;">&#41;</span> 2002-8 Bruce Allen
Home page is http:<span style="color: #000000; font-weight: bold;">//</span>smartmontools.sourceforge.net<span style="color: #000000; font-weight: bold;">/</span>
&nbsp;
=== START OF INFORMATION SECTION ===
Model Family:     Hitachi Travelstar 7K60
Device Model:     HTS726060M9AT00
Serial Number:    MRH403M4GS551Y
Firmware Version: MH4OA6BA
User Capacity:    60,011,642,880 bytes
Device is:        In smartctl database <span style="color: #7a0874; font-weight: bold;">&#91;</span><span style="color: #000000; font-weight: bold;">for</span> details use: <span style="color: #660033;">-P</span> show<span style="color: #7a0874; font-weight: bold;">&#93;</span>
ATA Version is:   <span style="color: #000000;">6</span>
ATA Standard is:  ATA<span style="color: #000000; font-weight: bold;">/</span>ATAPI-<span style="color: #000000;">6</span> T13 1410D revision 3a
Local Time is:    Mon Sep <span style="color: #000000;">21</span> 00:03:<span style="color: #000000;">50</span> <span style="color: #000000;">2009</span> EDT
SMART support is: Available - device has SMART capability.
SMART support is: Enabled</pre></td></tr></table></div>




<h4>Command #3: hdparm</h4>

<p>Another way that I&#8217;ve used to get at hard drive meta data is with the command <strong>hdparm</strong>. This is probably the oldest way, at least that I&#8217;m familiar with, for getting at hard drive meta data. To install it:</p>


<div class="wp_codebox"><table width="100%" ><tr id="p260248"><td class="line_numbers"><pre>1
</pre></td><td class="code" id="p2602code48"><pre class="bash" style="font-family:monospace;">yum <span style="color: #c20cb9; font-weight: bold;">install</span> hdparm</pre></td></tr></table></div>




<p>Using it is simply:</p>


<div class="wp_codebox"><table width="100%" ><tr id="p260249"><td class="line_numbers"><pre>1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
</pre></td><td class="code" id="p2602code49"><pre class="bash" style="font-family:monospace;"><span style="color: #666666; font-style: italic;"># hdparm example</span>
&nbsp;
<span style="color: #000000; font-weight: bold;">%</span> hdparm <span style="color: #660033;">-i</span> <span style="color: #000000; font-weight: bold;">/</span>dev<span style="color: #000000; font-weight: bold;">/</span>sda
&nbsp;
<span style="color: #000000; font-weight: bold;">/</span>dev<span style="color: #000000; font-weight: bold;">/</span>sda:
&nbsp;
 <span style="color: #007800;">Model</span>=HTS726060M9AT00                         , <span style="color: #007800;">FwRev</span>=MH4OA6BA, <span style="color: #007800;">SerialNo</span>=      MRH403M4GS551Y
 <span style="color: #007800;">Config</span>=<span style="color: #7a0874; font-weight: bold;">&#123;</span> HardSect NotMFM HdSw<span style="color: #000000; font-weight: bold;">&gt;</span>15uSec Fixed DTR<span style="color: #000000; font-weight: bold;">&gt;</span>10Mbs <span style="color: #7a0874; font-weight: bold;">&#125;</span>
 <span style="color: #007800;">RawCHS</span>=16383<span style="color: #000000; font-weight: bold;">/</span>16<span style="color: #000000; font-weight: bold;">/</span>63, <span style="color: #007800;">TrkSize</span>=0, <span style="color: #007800;">SectSize</span>=0, <span style="color: #007800;">ECCbytes</span>=4
 <span style="color: #007800;">BuffType</span>=DualPortCache, <span style="color: #007800;">BuffSize</span>=7877kB, <span style="color: #007800;">MaxMultSect</span>=16, <span style="color: #007800;">MultSect</span>=?0?
 <span style="color: #007800;">CurCHS</span>=16383<span style="color: #000000; font-weight: bold;">/</span>16<span style="color: #000000; font-weight: bold;">/</span>63, <span style="color: #007800;">CurSects</span>=16514064, <span style="color: #007800;">LBA</span>=<span style="color: #c20cb9; font-weight: bold;">yes</span>, <span style="color: #007800;">LBAsects</span>=117210240
 <span style="color: #007800;">IORDY</span>=on<span style="color: #000000; font-weight: bold;">/</span>off, <span style="color: #007800;">tPIO</span>=<span style="color: #7a0874; font-weight: bold;">&#123;</span>min:240,w<span style="color: #000000; font-weight: bold;">/</span>IORDY:120<span style="color: #7a0874; font-weight: bold;">&#125;</span>, <span style="color: #007800;">tDMA</span>=<span style="color: #7a0874; font-weight: bold;">&#123;</span>min:120,rec:120<span style="color: #7a0874; font-weight: bold;">&#125;</span>
 PIO modes:  pio0 pio1 pio2 pio3 pio4 
 DMA modes:  mdma0 mdma1 mdma2 
 UDMA modes: udma0 udma1 udma2 udma3 udma4 <span style="color: #000000; font-weight: bold;">*</span>udma5 
 <span style="color: #007800;">AdvancedPM</span>=<span style="color: #c20cb9; font-weight: bold;">yes</span>: <span style="color: #007800;">mode</span>=0xC0 <span style="color: #7a0874; font-weight: bold;">&#40;</span>192<span style="color: #7a0874; font-weight: bold;">&#41;</span> <span style="color: #007800;">WriteCache</span>=enabled
 Drive conforms to: ATA<span style="color: #000000; font-weight: bold;">/</span>ATAPI-<span style="color: #000000;">6</span> T13 1410D revision 3a:  ATA<span style="color: #000000; font-weight: bold;">/</span>ATAPI-<span style="color: #000000;">2</span>,<span style="color: #000000;">3</span>,<span style="color: #000000;">4</span>,<span style="color: #000000;">5</span>,<span style="color: #000000;">6</span>
&nbsp;
 <span style="color: #000000; font-weight: bold;">*</span> signifies the current active mode</pre></td></tr></table></div>




<h4>Command #4: lsscsi</h4>

<p>Here&#8217;s another tool, <strong>lsscsi</strong>, that I&#8217;ve used off and on to get at hard drive meta data. It&#8217;s probably the least known of all the tools mentioned here. Installation is the same as the others:</p>


<div class="wp_codebox"><table width="100%" ><tr id="p260250"><td class="line_numbers"><pre>1
</pre></td><td class="code" id="p2602code50"><pre class="bash" style="font-family:monospace;">yum <span style="color: #c20cb9; font-weight: bold;">install</span> lsscsi</pre></td></tr></table></div>




<p>It&#8217;s usage is pretty much in-line with the other commands too:</p>


<div class="wp_codebox"><table width="100%" ><tr id="p260251"><td class="line_numbers"><pre>1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
</pre></td><td class="code" id="p2602code51"><pre class="bash" style="font-family:monospace;"><span style="color: #666666; font-style: italic;"># lsscsi example</span>
&nbsp;
<span style="color: #000000; font-weight: bold;">%</span> lsscsi <span style="color: #660033;">-lll</span> <span style="color: #660033;">-vvv</span>
 sysfsroot: <span style="color: #000000; font-weight: bold;">/</span>sys
<span style="color: #7a0874; font-weight: bold;">&#91;</span>0:0:0:0<span style="color: #7a0874; font-weight: bold;">&#93;</span>    disk    ATA      HTS726060M9AT00  MH4O  <span style="color: #000000; font-weight: bold;">/</span>dev<span style="color: #000000; font-weight: bold;">/</span>sda
  <span style="color: #007800;">device_blocked</span>=0
  <span style="color: #007800;">iocounterbits</span>=32
  <span style="color: #007800;">iodone_cnt</span>=0x92983
  <span style="color: #007800;">ioerr_cnt</span>=0x4f
  <span style="color: #007800;">iorequest_cnt</span>=0x92983
  <span style="color: #007800;">queue_depth</span>=1
  <span style="color: #007800;">queue_type</span>=none
  <span style="color: #007800;">scsi_level</span>=6
  <span style="color: #007800;">state</span>=running
  <span style="color: #007800;">timeout</span>=60
  <span style="color: #7a0874; font-weight: bold;">type</span>=0
  <span style="color: #c20cb9; font-weight: bold;">dir</span>: <span style="color: #000000; font-weight: bold;">/</span>sys<span style="color: #000000; font-weight: bold;">/</span>bus<span style="color: #000000; font-weight: bold;">/</span>scsi<span style="color: #000000; font-weight: bold;">/</span>devices<span style="color: #000000; font-weight: bold;">/</span>0:0:0:0  <span style="color: #7a0874; font-weight: bold;">&#91;</span><span style="color: #000000; font-weight: bold;">/</span>sys<span style="color: #000000; font-weight: bold;">/</span>devices<span style="color: #000000; font-weight: bold;">/</span>pci0000:00<span style="color: #000000; font-weight: bold;">/</span>0000:00:1f.1<span style="color: #000000; font-weight: bold;">/</span>host0<span style="color: #000000; font-weight: bold;">/</span>target0:0:0<span style="color: #000000; font-weight: bold;">/</span>0:0:0:0<span style="color: #7a0874; font-weight: bold;">&#93;</span>
<span style="color: #7a0874; font-weight: bold;">&#91;</span>1:0:0:0<span style="color: #7a0874; font-weight: bold;">&#93;</span>    cd<span style="color: #000000; font-weight: bold;">/</span>dvd  MATSHITA UJDA755yDVD<span style="color: #000000; font-weight: bold;">/</span>CDRW 1.71  <span style="color: #000000; font-weight: bold;">/</span>dev<span style="color: #000000; font-weight: bold;">/</span>sr0
  <span style="color: #007800;">device_blocked</span>=0
  <span style="color: #007800;">iocounterbits</span>=32
  <span style="color: #007800;">iodone_cnt</span>=0xa0f7
  <span style="color: #007800;">ioerr_cnt</span>=0x0
  <span style="color: #007800;">iorequest_cnt</span>=0x283a7
  <span style="color: #007800;">queue_depth</span>=1
  <span style="color: #007800;">queue_type</span>=none
  <span style="color: #007800;">scsi_level</span>=6
  <span style="color: #007800;">state</span>=running
  <span style="color: #007800;">timeout</span>=0
  <span style="color: #7a0874; font-weight: bold;">type</span>=5
  <span style="color: #c20cb9; font-weight: bold;">dir</span>: <span style="color: #000000; font-weight: bold;">/</span>sys<span style="color: #000000; font-weight: bold;">/</span>bus<span style="color: #000000; font-weight: bold;">/</span>scsi<span style="color: #000000; font-weight: bold;">/</span>devices<span style="color: #000000; font-weight: bold;">/</span><span style="color: #000000;">1</span>:<span style="color: #000000;">0</span>:<span style="color: #000000;">0</span>:<span style="color: #000000;">0</span>  <span style="color: #7a0874; font-weight: bold;">&#91;</span><span style="color: #000000; font-weight: bold;">/</span>sys<span style="color: #000000; font-weight: bold;">/</span>devices<span style="color: #000000; font-weight: bold;">/</span>pci0000:00<span style="color: #000000; font-weight: bold;">/</span>0000:00:1f.1<span style="color: #000000; font-weight: bold;">/</span>host1<span style="color: #000000; font-weight: bold;">/</span>target1:<span style="color: #000000;">0</span>:<span style="color: #000000;">0</span><span style="color: #000000; font-weight: bold;">/</span><span style="color: #000000;">1</span>:<span style="color: #000000;">0</span>:<span style="color: #000000;">0</span>:<span style="color: #000000;">0</span><span style="color: #7a0874; font-weight: bold;">&#93;</span></pre></td></tr></table></div>




<h4>Command #4: /dev/disk/by-id directory</h4>

<p>Finally you can get the manufacturer&#8217;s product number &amp; serial number can be had by going directly to the /dev directory, specifically here: <strong>/dev/disk/by-id</strong>. In the resulting output you&#8217;ll see 2 important substrings. The first, <span class="caps">HTS726060M9AT00, </span>is he product number while the second, <span class="caps">MRH403M4GS551Y, </span>is the serial number.</p>


<div class="wp_codebox"><table width="100%" ><tr id="p260252"><td class="line_numbers"><pre>1
2
3
4
5
6
7
8
9
</pre></td><td class="code" id="p2602code52"><pre class="bash" style="font-family:monospace;"><span style="color: #666666; font-style: italic;"># /dev/disk/by-id example</span>
&nbsp;
<span style="color: #000000; font-weight: bold;">%</span> <span style="color: #c20cb9; font-weight: bold;">ls</span> <span style="color: #660033;">-1</span> <span style="color: #000000; font-weight: bold;">/</span>dev<span style="color: #000000; font-weight: bold;">/</span>disk<span style="color: #000000; font-weight: bold;">/</span>by-id
ata-HTS726060M9AT00_MRH403M4GS551Y
ata-HTS726060M9AT00_MRH403M4GS551Y-part1
ata-HTS726060M9AT00_MRH403M4GS551Y-part2
scsi-SATA_HTS726060M9AT00_MRH403M4GS551Y
scsi-SATA_HTS726060M9AT00_MRH403M4GS551Y-part1
scsi-SATA_HTS726060M9AT00_MRH403M4GS551Y-part2</pre></td></tr></table></div>




<p><strong><span class="caps">NOTE</span>:</strong> For further details regarding my <strong>one-liner</strong> blog posts, check out my <a href="http://www.lamolabs.org/blog/2147/intro-blogs-one-liner-code-block-style-guides/">one-liner style guide primer</a>.</p><div id="wherego_related"><h3>Readers who viewed this page, also viewed:</h3><ul><li><a href="http://www.lamolabs.org/blog/2616/one-liner-analyzing-a-systems-bios-from-the-command-line-under-fedora-centos-rhel/" rel="bookmark" class="wherego_title">[one-liner]: Analyzing a System&#8217;s BIOS from the Command Line under Fedora, CentOS, or RHEL</a></li><li>Powered by <a href="http://ajaydsouza.com/wordpress/plugins/where-did-they-go-from-here/">Where did they go from here?</a></li></ul></div>]]></content:encoded>
			<wfw:commentRss>http://www.lamolabs.org/blog/2602/one-liner-determining-a-hard-drives-manufaturer-under-fedora-10-centos-5/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>[one-liner]: Filtering ps from ps (redux)</title>
		<link>http://www.lamolabs.org/blog/2461/one-liner-filtering-ps-from-ps-redux/</link>
		<comments>http://www.lamolabs.org/blog/2461/one-liner-filtering-ps-from-ps-redux/#comments</comments>
		<pubDate>Sat, 05 Sep 2009 05:13:57 +0000</pubDate>
		<dc:creator>slmingol</dc:creator>
				<category><![CDATA[tips & tricks]]></category>
		<category><![CDATA[one-liner]]></category>
		<category><![CDATA[shell]]></category>
		<category><![CDATA[sysadmin]]></category>
		<category><![CDATA[tip]]></category>

		<guid isPermaLink="false">http://www.lamolabs.org/blog/?p=2461</guid>
		<description><![CDATA[<p>As it is with UNIX &#38; Linux there is always another way. In my previous article &#91;one-liner&#93;: Filtering ps from ps, one reader, Christoph, mentioned an alternative method to the one I outlined. In this case, I would consider his to be a better way, so I thought I would take a second to demonstrate [...]]]></description>
			<content:encoded><![CDATA[<p>As it is with <span class="caps">UNIX </span>&amp; Linux there is always <strong>another way</strong>. In my previous article <a href="http://www.lamolabs.org/blog/2425/one-liner-filtering-ps-from-ps/">&#91;one-liner&#93;: Filtering ps from ps</a>, one reader, Christoph, mentioned an <a href="http://www.lamolabs.org/blog/2425/one-liner-filtering-ps-from-ps/comment-page-1/#comment-302">alternative method</a> to the one I outlined. In this case, I would consider his to be a better way, so I thought I would take a second to demonstrate this alternative method. The alternative? Use the command <a href="http://en.wikipedia.org/wiki/Pgrep">pgrep</a>.</p>

<h4>The Original Approach</h4>

<p>My original post offered the following one-liner:</p>


<div class="wp_codebox"><table width="100%" ><tr id="p246157"><td class="line_numbers"><pre>1
2
3
4
5
6
7
8
9
10
</pre></td><td class="code" id="p2461code57"><pre class="bash" style="font-family:monospace;"><span style="color: #000000; font-weight: bold;">%</span> <span style="color: #c20cb9; font-weight: bold;">ps</span> <span style="color: #660033;">-eaf</span> <span style="color: #000000; font-weight: bold;">|</span> <span style="color: #c20cb9; font-weight: bold;">grep</span> <span style="color: #ff0000;">&quot;[h]ttpd&quot;</span>
root      <span style="color: #000000;">2683</span>     <span style="color: #000000;">1</span>  <span style="color: #000000;">0</span>  <span style="color: #000000;">2008</span> ?        00:<span style="color: #000000;">20</span>:<span style="color: #000000;">31</span> <span style="color: #000000; font-weight: bold;">/</span>usr<span style="color: #000000; font-weight: bold;">/</span>sbin<span style="color: #000000; font-weight: bold;">/</span>httpd
apache   <span style="color: #000000;">17146</span>  <span style="color: #000000;">2683</span>  <span style="color: #000000;">0</span> Aug30 ?        00:00:02 <span style="color: #000000; font-weight: bold;">/</span>usr<span style="color: #000000; font-weight: bold;">/</span>sbin<span style="color: #000000; font-weight: bold;">/</span>httpd
apache   <span style="color: #000000;">17147</span>  <span style="color: #000000;">2683</span>  <span style="color: #000000;">0</span> Aug30 ?        00:00:02 <span style="color: #000000; font-weight: bold;">/</span>usr<span style="color: #000000; font-weight: bold;">/</span>sbin<span style="color: #000000; font-weight: bold;">/</span>httpd
apache   <span style="color: #000000;">17149</span>  <span style="color: #000000;">2683</span>  <span style="color: #000000;">0</span> Aug30 ?        00:00:02 <span style="color: #000000; font-weight: bold;">/</span>usr<span style="color: #000000; font-weight: bold;">/</span>sbin<span style="color: #000000; font-weight: bold;">/</span>httpd
apache   <span style="color: #000000;">17150</span>  <span style="color: #000000;">2683</span>  <span style="color: #000000;">0</span> Aug30 ?        00:00:02 <span style="color: #000000; font-weight: bold;">/</span>usr<span style="color: #000000; font-weight: bold;">/</span>sbin<span style="color: #000000; font-weight: bold;">/</span>httpd
apache   <span style="color: #000000;">17151</span>  <span style="color: #000000;">2683</span>  <span style="color: #000000;">0</span> Aug30 ?        00:00:02 <span style="color: #000000; font-weight: bold;">/</span>usr<span style="color: #000000; font-weight: bold;">/</span>sbin<span style="color: #000000; font-weight: bold;">/</span>httpd
apache   <span style="color: #000000;">17152</span>  <span style="color: #000000;">2683</span>  <span style="color: #000000;">0</span> Aug30 ?        00:00:02 <span style="color: #000000; font-weight: bold;">/</span>usr<span style="color: #000000; font-weight: bold;">/</span>sbin<span style="color: #000000; font-weight: bold;">/</span>httpd
apache   <span style="color: #000000;">17153</span>  <span style="color: #000000;">2683</span>  <span style="color: #000000;">0</span> Aug30 ?        00:00:02 <span style="color: #000000; font-weight: bold;">/</span>usr<span style="color: #000000; font-weight: bold;">/</span>sbin<span style="color: #000000; font-weight: bold;">/</span>httpd
apache   <span style="color: #000000;">17154</span>  <span style="color: #000000;">2683</span>  <span style="color: #000000;">0</span> Aug30 ?        00:00:02 <span style="color: #000000; font-weight: bold;">/</span>usr<span style="color: #000000; font-weight: bold;">/</span>sbin<span style="color: #000000; font-weight: bold;">/</span>httpd</pre></td></tr></table></div>




<p>This one-liner provided a list of all the httpd processes running, while filtering out the actual string from the <strong>grep httpd</strong> command.</p>

<h4>The Alternative Approach</h4>

<p>By using the command <a href="http://en.wikipedia.org/wiki/Pgrep">pgrep</a>, the same effect can be achieved and a lot more. For starters, you can get a list of all the <strong>httpd <acronym title="Process IDs">PID</acronym>s</strong>:</p>


<div class="wp_codebox"><table width="100%" ><tr id="p246158"><td class="line_numbers"><pre>1
2
3
4
5
6
7
8
9
</pre></td><td class="code" id="p2461code58"><pre class="bash" style="font-family:monospace;"><span style="color: #666666; font-style: italic;"># list of httpd PIDs</span>
&nbsp;
<span style="color: #000000; font-weight: bold;">%</span> pgrep httpd
<span style="color: #000000;">1608</span>
<span style="color: #000000;">7645</span>
<span style="color: #000000;">9739</span>
<span style="color: #000000;">10051</span>
<span style="color: #000000;">27712</span>
<span style="color: #000000;">27859</span></pre></td></tr></table></div>




<p>This could be useful in a <strong>shell script</strong>, if needed, to check for any running <strong>httpd</strong> processes. For example:</p>


<div class="wp_codebox"><table width="100%" ><tr id="p246159"><td class="line_numbers"><pre>1
2
3
4
</pre></td><td class="code" id="p2461code59"><pre class="bash" style="font-family:monospace;"><span style="color: #666666; font-style: italic;"># test for httpd processes</span>
&nbsp;
<span style="color: #000000; font-weight: bold;">%</span> <span style="color: #7a0874; font-weight: bold;">&#91;</span> <span style="color: #660033;">-z</span> <span style="color: #ff0000;">&quot;<span style="color: #780078;">`pgrep httpd`</span>&quot;</span> <span style="color: #7a0874; font-weight: bold;">&#93;</span> <span style="color: #000000; font-weight: bold;">||</span> <span style="color: #7a0874; font-weight: bold;">echo</span> <span style="color: #ff0000;">&quot;running&quot;</span>
running</pre></td></tr></table></div>




<p>Here are some other examples:</p>


<div class="wp_codebox"><table width="100%" ><tr id="p246160"><td class="line_numbers"><pre>1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
</pre></td><td class="code" id="p2461code60"><pre class="bash" style="font-family:monospace;"><span style="color: #666666; font-style: italic;"># list of PIDs with corresponding command name</span>
&nbsp;
<span style="color: #000000; font-weight: bold;">%</span> pgrep <span style="color: #660033;">-l</span> httpd
<span style="color: #000000;">1608</span> httpd
<span style="color: #000000;">7645</span> httpd
<span style="color: #000000;">9739</span> httpd
<span style="color: #000000;">10051</span> httpd
<span style="color: #000000;">27712</span> httpd
<span style="color: #000000;">27859</span> httpd
&nbsp;
<span style="color: #666666; font-style: italic;"># list of PIDs with corresponding command name owned by user root</span>
&nbsp;
<span style="color: #000000; font-weight: bold;">%</span> pgrep <span style="color: #660033;">-l</span> <span style="color: #660033;">-u</span> root httpd
<span style="color: #000000;">1608</span> httpd
&nbsp;
<span style="color: #666666; font-style: italic;"># list of PIDs, separated with a comma delimiter</span>
&nbsp;
<span style="color: #000000; font-weight: bold;">%</span> pgrep -d, httpd
<span style="color: #000000;">1608</span>,<span style="color: #000000;">7645</span>,<span style="color: #000000;">9739</span>,<span style="color: #000000;">14119</span>,<span style="color: #000000;">14162</span>,<span style="color: #000000;">27859</span>
&nbsp;
<span style="color: #666666; font-style: italic;"># detailed list of httpd PIDs via ps</span>
<span style="color: #666666; font-style: italic;"># NOTE: $(...) runs the command above, returning the list of PIDs to ps</span>
&nbsp;
<span style="color: #000000; font-weight: bold;">%</span> <span style="color: #c20cb9; font-weight: bold;">ps</span> <span style="color: #660033;">-fp</span> $<span style="color: #7a0874; font-weight: bold;">&#40;</span>pgrep -d, httpd<span style="color: #7a0874; font-weight: bold;">&#41;</span>
UID        PID  PPID  C STIME TTY          TIME CMD
root      <span style="color: #000000;">1608</span>     <span style="color: #000000;">1</span>  <span style="color: #000000;">0</span> Aug03 ?        00:00:05 <span style="color: #000000; font-weight: bold;">/</span>usr<span style="color: #000000; font-weight: bold;">/</span>sbin<span style="color: #000000; font-weight: bold;">/</span>httpd
apache    <span style="color: #000000;">7645</span>  <span style="color: #000000;">1608</span>  <span style="color: #000000;">0</span> Sep04 ?        00:00:<span style="color: #000000;">47</span> <span style="color: #000000; font-weight: bold;">/</span>usr<span style="color: #000000; font-weight: bold;">/</span>sbin<span style="color: #000000; font-weight: bold;">/</span>httpd
apache    <span style="color: #000000;">9739</span>  <span style="color: #000000;">1608</span>  <span style="color: #000000;">0</span> Sep04 ?        00:01:<span style="color: #000000;">50</span> <span style="color: #000000; font-weight: bold;">/</span>usr<span style="color: #000000; font-weight: bold;">/</span>sbin<span style="color: #000000; font-weight: bold;">/</span>httpd
apache   <span style="color: #000000;">14119</span>  <span style="color: #000000;">1608</span>  <span style="color: #000000;">0</span> Sep04 ?        00:00:<span style="color: #000000;">13</span> <span style="color: #000000; font-weight: bold;">/</span>usr<span style="color: #000000; font-weight: bold;">/</span>sbin<span style="color: #000000; font-weight: bold;">/</span>httpd
apache   <span style="color: #000000;">14162</span>  <span style="color: #000000;">1608</span>  <span style="color: #000000;">0</span> Sep04 ?        00:00:<span style="color: #000000;">13</span> <span style="color: #000000; font-weight: bold;">/</span>usr<span style="color: #000000; font-weight: bold;">/</span>sbin<span style="color: #000000; font-weight: bold;">/</span>httpd
apache   <span style="color: #000000;">27859</span>  <span style="color: #000000;">1608</span>  <span style="color: #000000;">0</span> Sep04 ?        00:07:<span style="color: #000000;">19</span> <span style="color: #000000; font-weight: bold;">/</span>usr<span style="color: #000000; font-weight: bold;">/</span>sbin<span style="color: #000000; font-weight: bold;">/</span>httpd</pre></td></tr></table></div>




<p>Thanks again to Christoph for pointing out this alternative.</p>

<p><strong><span class="caps">NOTE</span>:</strong> For further details regarding my <strong>one-liner</strong> blog posts, check out my <a href="http://www.lamolabs.org/blog/2147/intro-blogs-one-liner-code-block-style-guides/">one-liner style guide primer</a>.</p><div id="wherego_related"> </div>]]></content:encoded>
			<wfw:commentRss>http://www.lamolabs.org/blog/2461/one-liner-filtering-ps-from-ps-redux/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>[one-liner]: Filtering ps from ps</title>
		<link>http://www.lamolabs.org/blog/2425/one-liner-filtering-ps-from-ps/</link>
		<comments>http://www.lamolabs.org/blog/2425/one-liner-filtering-ps-from-ps/#comments</comments>
		<pubDate>Fri, 04 Sep 2009 17:13:26 +0000</pubDate>
		<dc:creator>slmingol</dc:creator>
				<category><![CDATA[tips & tricks]]></category>
		<category><![CDATA[one-liner]]></category>
		<category><![CDATA[shell]]></category>
		<category><![CDATA[sysadmin]]></category>

		<guid isPermaLink="false">http://www.lamolabs.org/blog/?p=2425</guid>
		<description><![CDATA[Background

<p>This is a pretty handy trick to know when you want to filter out the command you&#8217;re running, so that it&#8217;s not included in ps output. This proves handy when writing a shell script that needs to parse output from ps. </p>

<p>NOTE: The command ps, allows you to see all the processes being run on [...]]]></description>
			<content:encoded><![CDATA[<h4>Background</h4>

<p>This is a pretty handy trick to know when you want to filter out the command you&#8217;re running, so that it&#8217;s not included in <a href="http://en.wikipedia.org/wiki/Ps_%28Unix%29">ps</a> output. This proves handy when writing a shell script that needs to parse output from <a href="http://en.wikipedia.org/wiki/Ps_%28Unix%29">ps</a>. </p>

<p><strong><span class="caps">NOTE</span>:</strong> The command <a href="http://en.wikipedia.org/wiki/Ps_%28Unix%29">ps</a>, allows you to see all the processes being run on a <span class="caps">UNIX</span>/Linux system. You typically use it with the switches <strong>&#8220;-eaf&#8221;</strong> or <strong>&#8220;-ef&#8221;</strong>.</p>


<div class="wp_codebox"><table width="100%" ><tr id="p242566"><td class="line_numbers"><pre>1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
</pre></td><td class="code" id="p2425code66"><pre class="bash" style="font-family:monospace;"><span style="color: #666666; font-style: italic;"># columns description:</span>
<span style="color: #666666; font-style: italic;"># UID   = user who owns the process</span>
<span style="color: #666666; font-style: italic;"># PID   = process #</span>
<span style="color: #666666; font-style: italic;"># PPID  = parents' process #</span>
<span style="color: #666666; font-style: italic;"># C     = </span>
<span style="color: #666666; font-style: italic;"># STIME = start time</span>
<span style="color: #666666; font-style: italic;"># TTY   = terminal type (has to do with which shell command ran from)</span>
<span style="color: #666666; font-style: italic;"># TIME  = system time its run</span>
<span style="color: #666666; font-style: italic;"># CMD   = command (aka. program name)</span>
&nbsp;
<span style="color: #000000; font-weight: bold;">%</span> <span style="color: #c20cb9; font-weight: bold;">ps</span> <span style="color: #660033;">-eaf</span>
UID        PID  PPID  C STIME TTY          TIME CMD
root         <span style="color: #000000;">1</span>     <span style="color: #000000;">0</span>  <span style="color: #000000;">0</span>  <span style="color: #000000;">2008</span> ?        00:<span style="color: #000000;">24</span>:<span style="color: #000000;">46</span> init <span style="color: #7a0874; font-weight: bold;">&#91;</span><span style="color: #000000;">5</span><span style="color: #7a0874; font-weight: bold;">&#93;</span>
root         <span style="color: #000000;">2</span>     <span style="color: #000000;">0</span>  <span style="color: #000000;">0</span>  <span style="color: #000000;">2008</span> ?        00:00:00 <span style="color: #7a0874; font-weight: bold;">&#91;</span>kthreadd<span style="color: #7a0874; font-weight: bold;">&#93;</span>
root         <span style="color: #000000;">3</span>     <span style="color: #000000;">2</span>  <span style="color: #000000;">0</span>  <span style="color: #000000;">2008</span> ?        00:01:<span style="color: #000000;">58</span> <span style="color: #7a0874; font-weight: bold;">&#91;</span>migration<span style="color: #000000; font-weight: bold;">/</span><span style="color: #000000;">0</span><span style="color: #7a0874; font-weight: bold;">&#93;</span>
root         <span style="color: #000000;">4</span>     <span style="color: #000000;">2</span>  <span style="color: #000000;">0</span>  <span style="color: #000000;">2008</span> ?        00:<span style="color: #000000;">23</span>:<span style="color: #000000;">13</span> <span style="color: #7a0874; font-weight: bold;">&#91;</span>ksoftirqd<span style="color: #000000; font-weight: bold;">/</span><span style="color: #000000;">0</span><span style="color: #7a0874; font-weight: bold;">&#93;</span>
root         <span style="color: #000000;">5</span>     <span style="color: #000000;">2</span>  <span style="color: #000000;">0</span>  <span style="color: #000000;">2008</span> ?        00:00:00 <span style="color: #7a0874; font-weight: bold;">&#91;</span>watchdog<span style="color: #000000; font-weight: bold;">/</span><span style="color: #000000;">0</span><span style="color: #7a0874; font-weight: bold;">&#93;</span>
root         <span style="color: #000000;">6</span>     <span style="color: #000000;">2</span>  <span style="color: #000000;">0</span>  <span style="color: #000000;">2008</span> ?        00:02:<span style="color: #000000;">35</span> <span style="color: #7a0874; font-weight: bold;">&#91;</span>migration<span style="color: #000000; font-weight: bold;">/</span><span style="color: #000000;">1</span><span style="color: #7a0874; font-weight: bold;">&#93;</span>
root         <span style="color: #000000;">7</span>     <span style="color: #000000;">2</span>  <span style="color: #000000;">0</span>  <span style="color: #000000;">2008</span> ?        00:09:<span style="color: #000000;">55</span> <span style="color: #7a0874; font-weight: bold;">&#91;</span>ksoftirqd<span style="color: #000000; font-weight: bold;">/</span><span style="color: #000000;">1</span><span style="color: #7a0874; font-weight: bold;">&#93;</span>
root         <span style="color: #000000;">8</span>     <span style="color: #000000;">2</span>  <span style="color: #000000;">0</span>  <span style="color: #000000;">2008</span> ?        00:00:00 <span style="color: #7a0874; font-weight: bold;">&#91;</span>watchdog<span style="color: #000000; font-weight: bold;">/</span><span style="color: #000000;">1</span><span style="color: #7a0874; font-weight: bold;">&#93;</span>
root         <span style="color: #000000;">9</span>     <span style="color: #000000;">2</span>  <span style="color: #000000;">0</span>  <span style="color: #000000;">2008</span> ?        00:06:<span style="color: #000000;">36</span> <span style="color: #7a0874; font-weight: bold;">&#91;</span>events<span style="color: #000000; font-weight: bold;">/</span><span style="color: #000000;">0</span><span style="color: #7a0874; font-weight: bold;">&#93;</span>
root        <span style="color: #000000;">10</span>     <span style="color: #000000;">2</span>  <span style="color: #000000;">0</span>  <span style="color: #000000;">2008</span> ?        00:06:<span style="color: #000000;">16</span> <span style="color: #7a0874; font-weight: bold;">&#91;</span>events<span style="color: #000000; font-weight: bold;">/</span><span style="color: #000000;">1</span><span style="color: #7a0874; font-weight: bold;">&#93;</span>
root        <span style="color: #000000;">11</span>     <span style="color: #000000;">2</span>  <span style="color: #000000;">0</span>  <span style="color: #000000;">2008</span> ?        00:00:<span style="color: #000000;">10</span> <span style="color: #7a0874; font-weight: bold;">&#91;</span>khelper<span style="color: #7a0874; font-weight: bold;">&#93;</span>
root        <span style="color: #000000;">54</span>     <span style="color: #000000;">2</span>  <span style="color: #000000;">0</span>  <span style="color: #000000;">2008</span> ?        00:<span style="color: #000000;">19</span>:<span style="color: #000000;">26</span> <span style="color: #7a0874; font-weight: bold;">&#91;</span>kblockd<span style="color: #000000; font-weight: bold;">/</span><span style="color: #000000;">0</span><span style="color: #7a0874; font-weight: bold;">&#93;</span></pre></td></tr></table></div>




<h4>Problem</h4>

<p>Here&#8217;s an example where we want to see if a program is running, so we <a href="http://en.wikipedia.org/wiki/Grep_%28Unix%29">grep</a> the output of <a href="http://en.wikipedia.org/wiki/Ps_%28Unix%29">ps</a> like so:</p>


<div class="wp_codebox"><table width="100%" ><tr id="p242567"><td class="line_numbers"><pre>1
2
3
4
5
6
7
8
9
10
11
</pre></td><td class="code" id="p2425code67"><pre class="bash" style="font-family:monospace;"><span style="color: #000000; font-weight: bold;">%</span> <span style="color: #c20cb9; font-weight: bold;">ps</span> <span style="color: #660033;">-eaf</span> <span style="color: #000000; font-weight: bold;">|</span> <span style="color: #c20cb9; font-weight: bold;">grep</span> httpd
root      2683     1  0  2008 ?        00:20:31 <span style="color: #000000; font-weight: bold;">/</span>usr<span style="color: #000000; font-weight: bold;">/</span>sbin<span style="color: #000000; font-weight: bold;">/</span>httpd
user1    13188  3984  0 12:45 pts<span style="color: #000000; font-weight: bold;">/</span>1    00:00:00 <span style="color: #c20cb9; font-weight: bold;">grep</span> httpd
apache   <span style="color: #000000;">17146</span>  <span style="color: #000000;">2683</span>  <span style="color: #000000;">0</span> Aug30 ?        00:00:02 <span style="color: #000000; font-weight: bold;">/</span>usr<span style="color: #000000; font-weight: bold;">/</span>sbin<span style="color: #000000; font-weight: bold;">/</span>httpd
apache   <span style="color: #000000;">17147</span>  <span style="color: #000000;">2683</span>  <span style="color: #000000;">0</span> Aug30 ?        00:00:02 <span style="color: #000000; font-weight: bold;">/</span>usr<span style="color: #000000; font-weight: bold;">/</span>sbin<span style="color: #000000; font-weight: bold;">/</span>httpd
apache   <span style="color: #000000;">17149</span>  <span style="color: #000000;">2683</span>  <span style="color: #000000;">0</span> Aug30 ?        00:00:02 <span style="color: #000000; font-weight: bold;">/</span>usr<span style="color: #000000; font-weight: bold;">/</span>sbin<span style="color: #000000; font-weight: bold;">/</span>httpd
apache   <span style="color: #000000;">17150</span>  <span style="color: #000000;">2683</span>  <span style="color: #000000;">0</span> Aug30 ?        00:00:02 <span style="color: #000000; font-weight: bold;">/</span>usr<span style="color: #000000; font-weight: bold;">/</span>sbin<span style="color: #000000; font-weight: bold;">/</span>httpd
apache   <span style="color: #000000;">17151</span>  <span style="color: #000000;">2683</span>  <span style="color: #000000;">0</span> Aug30 ?        00:00:02 <span style="color: #000000; font-weight: bold;">/</span>usr<span style="color: #000000; font-weight: bold;">/</span>sbin<span style="color: #000000; font-weight: bold;">/</span>httpd
apache   <span style="color: #000000;">17152</span>  <span style="color: #000000;">2683</span>  <span style="color: #000000;">0</span> Aug30 ?        00:00:02 <span style="color: #000000; font-weight: bold;">/</span>usr<span style="color: #000000; font-weight: bold;">/</span>sbin<span style="color: #000000; font-weight: bold;">/</span>httpd
apache   <span style="color: #000000;">17153</span>  <span style="color: #000000;">2683</span>  <span style="color: #000000;">0</span> Aug30 ?        00:00:02 <span style="color: #000000; font-weight: bold;">/</span>usr<span style="color: #000000; font-weight: bold;">/</span>sbin<span style="color: #000000; font-weight: bold;">/</span>httpd
apache   <span style="color: #000000;">17154</span>  <span style="color: #000000;">2683</span>  <span style="color: #000000;">0</span> Aug30 ?        00:00:02 <span style="color: #000000; font-weight: bold;">/</span>usr<span style="color: #000000; font-weight: bold;">/</span>sbin<span style="color: #000000; font-weight: bold;">/</span>httpd</pre></td></tr></table></div>




<p>The problem? Notice that a portion of our command, <strong>&#8220;grep httpd&#8221;</strong> is polluting our <strong>ps</strong> output. How can we get rid of it?</p>

<h4>The Trick</h4>

<p>You can alter what you are grepping for, without actually altering the results, by using a benign <a href="http://en.wikipedia.org/wiki/Regular_expression">regular expression</a>.</p>


<div class="wp_codebox"><table width="100%" ><tr id="p242568"><td class="line_numbers"><pre>1
2
3
4
5
6
7
8
9
10
</pre></td><td class="code" id="p2425code68"><pre class="bash" style="font-family:monospace;"><span style="color: #000000; font-weight: bold;">%</span> <span style="color: #c20cb9; font-weight: bold;">ps</span> <span style="color: #660033;">-eaf</span> <span style="color: #000000; font-weight: bold;">|</span> <span style="color: #c20cb9; font-weight: bold;">grep</span> <span style="color: #ff0000;">&quot;[h]ttpd&quot;</span>
root      <span style="color: #000000;">2683</span>     <span style="color: #000000;">1</span>  <span style="color: #000000;">0</span>  <span style="color: #000000;">2008</span> ?        00:<span style="color: #000000;">20</span>:<span style="color: #000000;">31</span> <span style="color: #000000; font-weight: bold;">/</span>usr<span style="color: #000000; font-weight: bold;">/</span>sbin<span style="color: #000000; font-weight: bold;">/</span>httpd
apache   <span style="color: #000000;">17146</span>  <span style="color: #000000;">2683</span>  <span style="color: #000000;">0</span> Aug30 ?        00:00:02 <span style="color: #000000; font-weight: bold;">/</span>usr<span style="color: #000000; font-weight: bold;">/</span>sbin<span style="color: #000000; font-weight: bold;">/</span>httpd
apache   <span style="color: #000000;">17147</span>  <span style="color: #000000;">2683</span>  <span style="color: #000000;">0</span> Aug30 ?        00:00:02 <span style="color: #000000; font-weight: bold;">/</span>usr<span style="color: #000000; font-weight: bold;">/</span>sbin<span style="color: #000000; font-weight: bold;">/</span>httpd
apache   <span style="color: #000000;">17149</span>  <span style="color: #000000;">2683</span>  <span style="color: #000000;">0</span> Aug30 ?        00:00:02 <span style="color: #000000; font-weight: bold;">/</span>usr<span style="color: #000000; font-weight: bold;">/</span>sbin<span style="color: #000000; font-weight: bold;">/</span>httpd
apache   <span style="color: #000000;">17150</span>  <span style="color: #000000;">2683</span>  <span style="color: #000000;">0</span> Aug30 ?        00:00:02 <span style="color: #000000; font-weight: bold;">/</span>usr<span style="color: #000000; font-weight: bold;">/</span>sbin<span style="color: #000000; font-weight: bold;">/</span>httpd
apache   <span style="color: #000000;">17151</span>  <span style="color: #000000;">2683</span>  <span style="color: #000000;">0</span> Aug30 ?        00:00:02 <span style="color: #000000; font-weight: bold;">/</span>usr<span style="color: #000000; font-weight: bold;">/</span>sbin<span style="color: #000000; font-weight: bold;">/</span>httpd
apache   <span style="color: #000000;">17152</span>  <span style="color: #000000;">2683</span>  <span style="color: #000000;">0</span> Aug30 ?        00:00:02 <span style="color: #000000; font-weight: bold;">/</span>usr<span style="color: #000000; font-weight: bold;">/</span>sbin<span style="color: #000000; font-weight: bold;">/</span>httpd
apache   <span style="color: #000000;">17153</span>  <span style="color: #000000;">2683</span>  <span style="color: #000000;">0</span> Aug30 ?        00:00:02 <span style="color: #000000; font-weight: bold;">/</span>usr<span style="color: #000000; font-weight: bold;">/</span>sbin<span style="color: #000000; font-weight: bold;">/</span>httpd
apache   <span style="color: #000000;">17154</span>  <span style="color: #000000;">2683</span>  <span style="color: #000000;">0</span> Aug30 ?        00:00:02 <span style="color: #000000; font-weight: bold;">/</span>usr<span style="color: #000000; font-weight: bold;">/</span>sbin<span style="color: #000000; font-weight: bold;">/</span>httpd</pre></td></tr></table></div>




<h4>Explanation</h4>


<p><span id="more-2425"></span></p>

<p>By re-encoding what we are looking for from <strong>httpd</strong> to <strong>&#91;h&#93;ttpd</strong> we&#8217;ve altered it by utilizing <a href="http://en.wikipedia.org/wiki/Grep_%28Unix%29">grep&#8217;s</a> capability to use <a href="http://en.wikipedia.org/wiki/Regular_expression">regular expressions</a>. Regular expressions provide us the ability to use short-handed notations to represent patterns of text. Most people should be somewhat familiar with this capability when using the command <a href="http://en.wikipedia.org/wiki/Ls">ls</a> to list files. The command <a href="http://en.wikipedia.org/wiki/Ls">ls</a> can be used to find all the files that start with the letter <strong>a</strong> by utilizing a wildcard regular expression, aka. the <strong>asterisk</strong> character, aka. <strong>star</strong>, aka. <strong>&#42;</strong>.</p>


<div class="wp_codebox"><table width="100%" ><tr id="p242569"><td class="line_numbers"><pre>1
2
3
4
5
6
7
8
9
10
11
12
</pre></td><td class="code" id="p2425code69"><pre class="bash" style="font-family:monospace;"><span style="color: #666666; font-style: italic;"># wildcard examples</span>
&nbsp;
<span style="color: #000000; font-weight: bold;">%</span> <span style="color: #c20cb9; font-weight: bold;">ls</span>
afile1  afile2  afile3  bfile1  bfile2  bfile3  cfile1  cfile2  cfile3
&nbsp;
<span style="color: #666666; font-style: italic;"># example 1</span>
<span style="color: #000000; font-weight: bold;">%</span> <span style="color: #c20cb9; font-weight: bold;">ls</span> a<span style="color: #000000; font-weight: bold;">*</span>
afile1  afile2  afile3
&nbsp;
<span style="color: #666666; font-style: italic;"># example 2</span>
<span style="color: #000000; font-weight: bold;">%</span> <span style="color: #c20cb9; font-weight: bold;">ls</span> b<span style="color: #000000; font-weight: bold;">*</span>
bfile1  bfile2  bfile3</pre></td></tr></table></div>




<p>Now lets say we want to create a single command that provides us with the list of <strong><span class="caps">ALL</span></strong> the files that start with an <strong>a or b</strong>. The bracket notation <strong>&#91; &#93;</strong> regular expression allows you to list sets of characters. So for example:</p>


<div class="wp_codebox"><table width="100%" ><tr id="p242570"><td class="line_numbers"><pre>1
2
3
4
5
6
7
8
9
10
11
12
</pre></td><td class="code" id="p2425code70"><pre class="bash" style="font-family:monospace;"><span style="color: #666666; font-style: italic;"># set examples</span>
&nbsp;
<span style="color: #000000; font-weight: bold;">%</span> <span style="color: #c20cb9; font-weight: bold;">ls</span>
afile1  afile2  afile3  bfile1  bfile2  bfile3  cfile1  cfile2  cfile3
&nbsp;
<span style="color: #666666; font-style: italic;"># example 1</span>
<span style="color: #000000; font-weight: bold;">%</span> <span style="color: #c20cb9; font-weight: bold;">ls</span> <span style="color: #7a0874; font-weight: bold;">&#91;</span>ab<span style="color: #7a0874; font-weight: bold;">&#93;</span><span style="color: #000000; font-weight: bold;">*</span>
afile1  afile2  afile3  bfile1  bfile2  bfile3
&nbsp;
<span style="color: #666666; font-style: italic;"># example 2</span>
<span style="color: #000000; font-weight: bold;">%</span> <span style="color: #c20cb9; font-weight: bold;">ls</span> <span style="color: #7a0874; font-weight: bold;">&#91;</span><span style="color: #c20cb9; font-weight: bold;">bc</span><span style="color: #7a0874; font-weight: bold;">&#93;</span><span style="color: #000000; font-weight: bold;">*</span>
bfile1  bfile2  bfile3  cfile1  cfile2  cfile3</pre></td></tr></table></div>




<p>So back to our <strong>ps</strong> example. What does the bracket notation regular expression provide us here? This is the <strong>trick</strong>. By telling grep that we are looking for <strong>&#91;h&#93;ttpd</strong>, we&#8217;ve encoded our grep query to look like this: <strong>&#91;h&#93;ttpd</strong>, while what we&#8217;re actually grepping for to be: <strong>httpd</strong>. Since <strong>&#91;h&#93;ttpd</strong> doesn&#8217;t match <strong>httpd</strong> at the literal string level, grep will effectively ignore itself as being a match in the <strong>ps</strong> output.</p>

<p>A pretty neat <strong>&#91;t&#93;rick</strong>.</p>

<p><strong><span class="caps">NOTE</span>:</strong> For further details regarding my <strong>one-liner</strong> blog posts, check out my <a href="http://www.lamolabs.org/blog/2147/intro-blogs-one-liner-code-block-style-guides/">one-liner style guide primer</a>.</p><div id="wherego_related"> </div>]]></content:encoded>
			<wfw:commentRss>http://www.lamolabs.org/blog/2425/one-liner-filtering-ps-from-ps/feed/</wfw:commentRss>
		<slash:comments>1</slash:comments>
		</item>
		<item>
		<title>[one-liner]: Shell Builtins</title>
		<link>http://www.lamolabs.org/blog/2362/one-liner-shell-builtins/</link>
		<comments>http://www.lamolabs.org/blog/2362/one-liner-shell-builtins/#comments</comments>
		<pubDate>Tue, 25 Aug 2009 05:12:29 +0000</pubDate>
		<dc:creator>slmingol</dc:creator>
				<category><![CDATA[tips & tricks]]></category>
		<category><![CDATA[bash]]></category>
		<category><![CDATA[csh]]></category>
		<category><![CDATA[linux]]></category>
		<category><![CDATA[one-liner]]></category>
		<category><![CDATA[shell]]></category>
		<category><![CDATA[tip]]></category>

		<guid isPermaLink="false">http://www.lamolabs.org/blog/?p=2362</guid>
		<description><![CDATA[Background

<p>I recently worked a problem in a previous post where it wasn&#8217;t clear which version of the command nohup was getting used. The version that was built into the C-shell interpreter, or the executable sitting under /usr/bin/nohup. This brought up an interesting point, how do you know what commands are builtins to the shell itself?</p>

Enter [...]]]></description>
			<content:encoded><![CDATA[<h4>Background</h4>

<p>I recently worked a problem in a <a href="http://www.lamolabs.org/blog/2349/one-liner-how-to-properly-use-nohup/">previous post</a> where it wasn&#8217;t clear which version of the command <strong>nohup</strong> was getting used. The version that was <strong>built into</strong> the <strong>C-shell interpreter</strong>, or the executable sitting under <strong>/usr/bin/nohup</strong>. This brought up an interesting point, how do you know what commands are builtins to the shell itself?</p>

<h4>Enter the commands: builtins and enable</h4>

<p>These are 2 commands that I&#8217;d never even heard of until I started doing research for this post. They both basically do the same thing. They <strong>show</strong> you <strong>all</strong> the <strong>builtin commands</strong> for a particular shell, <strong>builtins</strong> for <strong>C-shell</strong>, and <strong>enable</strong> for <strong>Bourne Shell</strong>.</p>


<div class="wp_codebox"><table width="100%" ><tr id="p236273"><td class="line_numbers"><pre>1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
</pre></td><td class="code" id="p2362code73"><pre class="csh" style="font-family:monospace;"># C-shell builtins
&nbsp;
#NOTE: run this command within a C-shell (aka. csh or tcsh)
% csh
&nbsp;
% builtins
:          @          alias      alloc      bg         bindkey    break
breaksw    builtins   case       cd         chdir      complete   continue
default    dirs       echo       echotc     else       end        endif
endsw      eval       exec       exit       fg         filetest   foreach
glob       goto       hashstat   history    hup        if         jobs
kill       limit      log        login      logout     ls-F       nice
nohup      notify     onintr     popd       printenv   pushd      rehash
repeat     sched      set        setenv     settc      setty      shift
source     stop       suspend    switch     telltc     termname   time
umask      unalias    uncomplete unhash     unlimit    unset      unsetenv
wait       where      which      while</pre></td></tr></table></div>





<div class="wp_codebox"><table width="100%" ><tr id="p236274"><td class="line_numbers"><pre>1
2
3
4
5
6
7
</pre></td><td class="code" id="p2362code74"><pre class="bash" style="font-family:monospace;"><span style="color: #666666; font-style: italic;"># Bourne Again Shell builtins</span>
&nbsp;
<span style="color: #666666; font-style: italic;">#NOTE: run this command within Bourne Again Shell (aka. bash)</span>
<span style="color: #000000; font-weight: bold;">%</span> <span style="color: #c20cb9; font-weight: bold;">bash</span>
&nbsp;
<span style="color: #000000; font-weight: bold;">%</span> <span style="color: #7a0874; font-weight: bold;">enable</span> <span style="color: #000000; font-weight: bold;">|</span> <span style="color: #c20cb9; font-weight: bold;">cut</span> <span style="color: #660033;">-d</span><span style="color: #ff0000;">&quot; &quot;</span> <span style="color: #660033;">-f2</span> <span style="color: #000000; font-weight: bold;">|</span> <span style="color: #c20cb9; font-weight: bold;">sed</span> <span style="color: #660033;">-e</span> :a <span style="color: #660033;">-e</span> <span style="color: #ff0000;">'$!N;s/\n/ /;ta'</span>
. : <span style="color: #7a0874; font-weight: bold;">&#91;</span> <span style="color: #7a0874; font-weight: bold;">alias</span> <span style="color: #7a0874; font-weight: bold;">bg</span> <span style="color: #7a0874; font-weight: bold;">bind</span> <span style="color: #7a0874; font-weight: bold;">break</span> <span style="color: #7a0874; font-weight: bold;">builtin</span> caller <span style="color: #7a0874; font-weight: bold;">cd</span> <span style="color: #7a0874; font-weight: bold;">command</span> <span style="color: #7a0874; font-weight: bold;">compgen</span> <span style="color: #7a0874; font-weight: bold;">complete</span> <span style="color: #7a0874; font-weight: bold;">continue</span> <span style="color: #7a0874; font-weight: bold;">declare</span> <span style="color: #7a0874; font-weight: bold;">dirs</span> <span style="color: #7a0874; font-weight: bold;">disown</span> <span style="color: #7a0874; font-weight: bold;">echo</span> <span style="color: #7a0874; font-weight: bold;">enable</span> <span style="color: #7a0874; font-weight: bold;">eval</span> <span style="color: #7a0874; font-weight: bold;">exec</span> <span style="color: #7a0874; font-weight: bold;">exit</span> <span style="color: #7a0874; font-weight: bold;">export</span> <span style="color: #c20cb9; font-weight: bold;">false</span> <span style="color: #7a0874; font-weight: bold;">fc</span> <span style="color: #7a0874; font-weight: bold;">fg</span> <span style="color: #7a0874; font-weight: bold;">getopts</span> <span style="color: #7a0874; font-weight: bold;">hash</span> <span style="color: #7a0874; font-weight: bold;">help</span> <span style="color: #7a0874; font-weight: bold;">history</span> <span style="color: #7a0874; font-weight: bold;">jobs</span> <span style="color: #c20cb9; font-weight: bold;">kill</span> <span style="color: #7a0874; font-weight: bold;">let</span> <span style="color: #7a0874; font-weight: bold;">local</span> <span style="color: #7a0874; font-weight: bold;">logout</span> <span style="color: #7a0874; font-weight: bold;">popd</span> <span style="color: #7a0874; font-weight: bold;">printf</span> <span style="color: #7a0874; font-weight: bold;">pushd</span> <span style="color: #7a0874; font-weight: bold;">pwd</span> <span style="color: #c20cb9; font-weight: bold;">read</span> <span style="color: #7a0874; font-weight: bold;">readonly</span> <span style="color: #7a0874; font-weight: bold;">return</span> <span style="color: #000000; font-weight: bold;">set</span> <span style="color: #7a0874; font-weight: bold;">shift</span> <span style="color: #7a0874; font-weight: bold;">shopt</span> <span style="color: #7a0874; font-weight: bold;">source</span> <span style="color: #7a0874; font-weight: bold;">suspend</span> <span style="color: #7a0874; font-weight: bold;">test</span> <span style="color: #7a0874; font-weight: bold;">times</span> <span style="color: #7a0874; font-weight: bold;">trap</span> <span style="color: #c20cb9; font-weight: bold;">true</span> <span style="color: #7a0874; font-weight: bold;">type</span> <span style="color: #7a0874; font-weight: bold;">typeset</span> <span style="color: #7a0874; font-weight: bold;">ulimit</span> <span style="color: #7a0874; font-weight: bold;">umask</span> <span style="color: #7a0874; font-weight: bold;">unalias</span> <span style="color: #7a0874; font-weight: bold;">unset</span> <span style="color: #7a0874; font-weight: bold;">wait</span></pre></td></tr></table></div>




<p><strong><span class="caps">NOTE</span>:</strong> The command is <strong>enable</strong>. I&#8217;m simply running the <strong>output</strong> of <strong>enable</strong> through this <strong>cut &amp; sed filter</strong> so that the <strong>output</strong> of <strong>enable</strong> is more concise for this example.</p>

<h4>nohup</h4>

<p>As you can see, by looking at the output from <strong>builtins</strong>, the command <strong>nohup</strong> is listed there, which means that <strong>C-shell</strong> has it&#8217;s <strong>own</strong> implementation of <strong>nohup</strong>. Looking at the output from <strong>enable</strong>, you can see that <strong>Bourne Shell</strong>, doesn&#8217;t include it&#8217;s own builtin for <strong>nohup</strong>, so it would use <strong>/usr/bin/nohup</strong>. Pretty simple, eh?</p>

<p><strong><span class="caps">NOTE</span>:</strong> For further details regarding my <strong>one-liner</strong> blog posts, check out my <a href="http://www.lamolabs.org/blog/2147/intro-blogs-one-liner-code-block-style-guides/">one-liner style guide primer</a>.</p><div id="wherego_related"> </div>]]></content:encoded>
			<wfw:commentRss>http://www.lamolabs.org/blog/2362/one-liner-shell-builtins/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>[one-liner]: How to Properly Use nohup</title>
		<link>http://www.lamolabs.org/blog/2349/one-liner-how-to-properly-use-nohup/</link>
		<comments>http://www.lamolabs.org/blog/2349/one-liner-how-to-properly-use-nohup/#comments</comments>
		<pubDate>Tue, 25 Aug 2009 02:55:32 +0000</pubDate>
		<dc:creator>slmingol</dc:creator>
				<category><![CDATA[tips & tricks]]></category>
		<category><![CDATA[one-liner]]></category>
		<category><![CDATA[server]]></category>
		<category><![CDATA[shell]]></category>
		<category><![CDATA[tip]]></category>

		<guid isPermaLink="false">http://www.lamolabs.org/blog/?p=2349</guid>
		<description><![CDATA[Background

<p>Today a co-worker, let&#8217;s call him Evan, was trying to retrofit the UNIX command nohup into a script that I originally developed, and was not having much luck. He was trying to debug a unstable server daemon that&#8217;s called by my script. The server daemon had been crashing for the better part of 2 weeks, [...]]]></description>
			<content:encoded><![CDATA[<h4>Background</h4>

<p>Today a co-worker, let&#8217;s call him Evan, was trying to retrofit the <span class="caps">UNIX </span>command <strong>nohup</strong> into a script that I originally developed, and was not having much luck. He was trying to debug a unstable server daemon that&#8217;s called by my script. The server daemon had been crashing for the better part of 2 weeks, and the vendor of this particular daemon was directing Evan to prefix the launching of the daemon with nohup. Seemed like a simple request, but nohup wasn&#8217;t behaving as he expected. Both Evan and the vendor support specialist, let&#8217;s call him Sal, were looking for a <strong>nohup.out</strong> file to be deposited in the directory where they were running my script. I initially thought that my script might be changing directories behind the scenes, causing the files to show up some where else. After a quick check I could find no evidence of any nohup.out files anywhere. I really didn&#8217;t have much experience with nohup either so I figured this was the perfect excuse to better understand how nohup works.</p>

<h4>nohup</h4>

<p><strong>nohup</strong> stands for <strong>&#8220;no hangup&#8221;</strong>, and is a way to start a process up that is <strong>immune</strong> from being <strong>hungup</strong>. It&#8217;s typically invoked like this:</p>


<div class="wp_codebox"><table width="100%" ><tr id="p234981"><td class="line_numbers"><pre>1
</pre></td><td class="code" id="p2349code81"><pre class="bash" style="font-family:monospace;"><span style="color: #c20cb9; font-weight: bold;">nohup</span> <span style="color: #c20cb9; font-weight: bold;">sleep</span> <span style="color: #000000;">100</span> <span style="color: #000000; font-weight: bold;">&amp;</span></pre></td></tr></table></div>




<p>This means that within <span class="caps">UNIX, </span>when a <strong>parent process</strong> spawns a <strong>child process</strong>, and the parent process is then sent the <strong>signal</strong> to <strong>hangup</strong>, the child process has been instructed via <strong>nohup</strong> to <strong>ignore that signal</strong>. At which point the child process then becomes a <strong>child process</strong> of the <strong>init process</strong>, i.e. the process with the <strong><span class="caps">PID </span>of 1</strong>. The init process is the originating process of every process on a <span class="caps">UNIX </span>system. As always, an example can explain it better than I:</p>

<h5>example #1 (simple nohup, without killing the parent process)</h5>


<div class="wp_codebox"><table width="100%" ><tr id="p234982"><td class="line_numbers"><pre>1
2
3
4
5
6
7
8
9
10
11
12
13
14
</pre></td><td class="code" id="p2349code82"><pre class="bash" style="font-family:monospace;"><span style="color: #666666; font-style: italic;"># start the command &quot;sleep 5&quot; nohup'd and backgrounded</span>
&nbsp;
<span style="color: #000000; font-weight: bold;">%</span> <span style="color: #c20cb9; font-weight: bold;">nohup</span> <span style="color: #c20cb9; font-weight: bold;">sleep</span> 5 <span style="color: #000000; font-weight: bold;">&amp;</span>
<span style="color: #7a0874; font-weight: bold;">&#91;</span>1<span style="color: #7a0874; font-weight: bold;">&#93;</span> 32257
&nbsp;
<span style="color: #000000; font-weight: bold;">%</span> <span style="color: #c20cb9; font-weight: bold;">ps</span> <span style="color: #660033;">-eaf</span> <span style="color: #000000; font-weight: bold;">|</span> <span style="color: #c20cb9; font-weight: bold;">egrep</span> <span style="color: #ff0000;">&quot;[s]leep|[U]ID|[/]sbin/init&quot;</span>
UID        PID  PPID  C STIME TTY          TIME CMD
root         1     0  0 Jun20 ?        00:00:01 <span style="color: #000000; font-weight: bold;">/</span>sbin<span style="color: #000000; font-weight: bold;">/</span>init
root     32257 31977  0 21:36 pts<span style="color: #000000; font-weight: bold;">/</span>8    00:00:00 <span style="color: #c20cb9; font-weight: bold;">sleep</span> 5
&nbsp;
<span style="color: #000000; font-weight: bold;">%</span> <span style="color: #c20cb9; font-weight: bold;">ps</span> <span style="color: #660033;">-eaf</span> <span style="color: #000000; font-weight: bold;">|</span> <span style="color: #c20cb9; font-weight: bold;">egrep</span> <span style="color: #ff0000;">&quot;[s]leep|[U]ID|[/]sbin/init&quot;</span>
UID        PID  PPID  C STIME TTY          TIME CMD
root         1     0  0 Jun20 ?        00:00:01 <span style="color: #000000; font-weight: bold;">/</span>sbin<span style="color: #000000; font-weight: bold;">/</span>init
<span style="color: #7a0874; font-weight: bold;">&#91;</span>1<span style="color: #7a0874; font-weight: bold;">&#93;</span>+  Done                    <span style="color: #c20cb9; font-weight: bold;">nohup</span> <span style="color: #c20cb9; font-weight: bold;">sleep</span> <span style="color: #000000;">5</span></pre></td></tr></table></div>




<h5>example #2 (parent is killed, child remains)</h5>

<p><span id="more-2349"></span></p>


<div class="wp_codebox"><table width="100%" ><tr id="p234983"><td class="line_numbers"><pre>1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
</pre></td><td class="code" id="p2349code83"><pre class="bash" style="font-family:monospace;"><span style="color: #666666; font-style: italic;"># initially start 2 terminal windows up (termA &amp; termB)</span>
&nbsp;
<span style="color: #666666; font-style: italic;"># termA</span>
<span style="color: #000000; font-weight: bold;">%</span> <span style="color: #c20cb9; font-weight: bold;">nohup</span> <span style="color: #c20cb9; font-weight: bold;">sleep</span> 15 <span style="color: #000000; font-weight: bold;">&amp;</span>
<span style="color: #7a0874; font-weight: bold;">&#91;</span>1<span style="color: #7a0874; font-weight: bold;">&#93;</span> 32229
<span style="color: #000000; font-weight: bold;">%</span> <span style="color: #c20cb9; font-weight: bold;">ps</span> <span style="color: #660033;">-eaf</span> <span style="color: #000000; font-weight: bold;">|</span> <span style="color: #c20cb9; font-weight: bold;">egrep</span> <span style="color: #ff0000;">&quot;[s]leep|[U]ID|[/]sbin/init&quot;</span>
UID        PID  PPID  C STIME TTY          TIME CMD
root         1     0  0 Jun20 ?        00:00:01 <span style="color: #000000; font-weight: bold;">/</span>sbin<span style="color: #000000; font-weight: bold;">/</span>init
root     32229 32195  0 21:30 pts<span style="color: #000000; font-weight: bold;">/</span>9    00:00:00 <span style="color: #c20cb9; font-weight: bold;">sleep</span> 15
<span style="color: #000000; font-weight: bold;">%</span> <span style="color: #7a0874; font-weight: bold;">exit</span>
&nbsp;
<span style="color: #666666; font-style: italic;"># at this point termA has been destroyed (its PID was 32195)</span>
&nbsp;
<span style="color: #666666; font-style: italic;"># termB</span>
<span style="color: #000000; font-weight: bold;">%</span> <span style="color: #c20cb9; font-weight: bold;">ps</span> <span style="color: #660033;">-eaf</span> <span style="color: #000000; font-weight: bold;">|</span> <span style="color: #c20cb9; font-weight: bold;">egrep</span> <span style="color: #ff0000;">&quot;[s]leep|[U]ID|[/]sbin/init&quot;</span>
UID        PID  PPID  C STIME TTY          TIME CMD
root         1     0  0 Jun20 ?        00:00:01 <span style="color: #000000; font-weight: bold;">/</span>sbin<span style="color: #000000; font-weight: bold;">/</span>init
root     32229     1  0 21:30 ?        00:00:00 <span style="color: #c20cb9; font-weight: bold;">sleep</span> <span style="color: #000000;">15</span>
&nbsp;
<span style="color: #666666; font-style: italic;"># notice how the sleep process is now owned by the parent process init (PPID 1)</span>
&nbsp;
<span style="color: #000000; font-weight: bold;">%</span> <span style="color: #c20cb9; font-weight: bold;">ps</span> <span style="color: #660033;">-eaf</span> <span style="color: #000000; font-weight: bold;">|</span> <span style="color: #c20cb9; font-weight: bold;">egrep</span> <span style="color: #ff0000;">&quot;[s]leep|[U]ID|[/]sbin/init&quot;</span>
UID        PID  PPID  C STIME TTY          TIME CMD
root         1     0  0 Jun20 ?        00:00:01 <span style="color: #000000; font-weight: bold;">/</span>sbin<span style="color: #000000; font-weight: bold;">/</span>init
root     32229     1  0 21:30 ?        00:00:00 <span style="color: #c20cb9; font-weight: bold;">sleep</span> 15
&nbsp;
<span style="color: #000000; font-weight: bold;">%</span> <span style="color: #c20cb9; font-weight: bold;">ps</span> <span style="color: #660033;">-eaf</span> <span style="color: #000000; font-weight: bold;">|</span> <span style="color: #c20cb9; font-weight: bold;">egrep</span> <span style="color: #ff0000;">&quot;[s]leep|[U]ID|[/]sbin/init&quot;</span>
UID        PID  PPID  C STIME TTY          TIME CMD
root         <span style="color: #000000;">1</span>     <span style="color: #000000;">0</span>  <span style="color: #000000;">0</span> Jun20 ?        00:00:01 <span style="color: #000000; font-weight: bold;">/</span>sbin<span style="color: #000000; font-weight: bold;">/</span>init
<span style="color: #000000; font-weight: bold;">%</span></pre></td></tr></table></div>




<h4>So Where was the nohup.out File?</h4>

<p>After debugging this problem for a bit I realized that my co-worker Evan was attempting to use <strong>nohup</strong> in a <strong>C-shell</strong> (csh). Turns out that made all the difference. See if you can see it:</p>


<div class="wp_codebox"><table width="100%" ><tr id="p234984"><td class="line_numbers"><pre>1
2
3
4
5
6
7
8
9
10
11
12
</pre></td><td class="code" id="p2349code84"><pre class="bash" style="font-family:monospace;"><span style="color: #666666; font-style: italic;"># scenario 1</span>
<span style="color: #000000; font-weight: bold;">%</span> <span style="color: #c20cb9; font-weight: bold;">nohup</span> <span style="color: #c20cb9; font-weight: bold;">sleep</span> 1 <span style="color: #000000; font-weight: bold;">&amp;</span>
<span style="color: #7a0874; font-weight: bold;">&#91;</span>1<span style="color: #7a0874; font-weight: bold;">&#93;</span> 32583
<span style="color: #000000; font-weight: bold;">%</span> 
<span style="color: #7a0874; font-weight: bold;">&#91;</span>1<span style="color: #7a0874; font-weight: bold;">&#93;</span>    Done                          <span style="color: #c20cb9; font-weight: bold;">sleep</span> <span style="color: #000000;">1</span>
&nbsp;
<span style="color: #666666; font-style: italic;"># scenario 2</span>
<span style="color: #000000; font-weight: bold;">%</span> <span style="color: #c20cb9; font-weight: bold;">nohup</span> <span style="color: #c20cb9; font-weight: bold;">sleep</span> 1 <span style="color: #000000; font-weight: bold;">&amp;</span>
<span style="color: #c20cb9; font-weight: bold;">nohup</span>: ignoring input and appending output to <span style="color: #000000; font-weight: bold;">`</span>nohup.out<span style="color: #ff0000;">'
[1] 32584
% 
[1]+  Done                    nohup sleep 1</span></pre></td></tr></table></div>




<p>Which of the above <strong>scenarios</strong> do you think is the <strong>C-shell</strong>? Which is the <strong>Bourne Shell</strong>? Scenario #1 is the C-shell &amp; scenario #2 is the Bourne Shell. Why the difference? Using the commands <strong>which</strong> and <strong>whereis</strong> didn&#8217;t really help to answer why either, since they both turned up /usr/bin/nohup.</p>


<div class="wp_codebox"><table width="100%" ><tr id="p234985"><td class="line_numbers"><pre>1
2
3
4
5
6
7
8
9
10
11
</pre></td><td class="code" id="p2349code85"><pre class="bash" style="font-family:monospace;"><span style="color: #666666; font-style: italic;"># csh</span>
<span style="color: #000000; font-weight: bold;">%</span> <span style="color: #c20cb9; font-weight: bold;">which</span> <span style="color: #c20cb9; font-weight: bold;">nohup</span>
<span style="color: #000000; font-weight: bold;">/</span>usr<span style="color: #000000; font-weight: bold;">/</span>bin<span style="color: #000000; font-weight: bold;">/</span><span style="color: #c20cb9; font-weight: bold;">nohup</span>
<span style="color: #000000; font-weight: bold;">%</span> <span style="color: #c20cb9; font-weight: bold;">whereis</span> <span style="color: #c20cb9; font-weight: bold;">nohup</span>
<span style="color: #c20cb9; font-weight: bold;">nohup</span>: <span style="color: #000000; font-weight: bold;">/</span>usr<span style="color: #000000; font-weight: bold;">/</span>bin<span style="color: #000000; font-weight: bold;">/</span><span style="color: #c20cb9; font-weight: bold;">nohup</span> <span style="color: #000000; font-weight: bold;">/</span>usr<span style="color: #000000; font-weight: bold;">/</span>share<span style="color: #000000; font-weight: bold;">/</span>man<span style="color: #000000; font-weight: bold;">/</span>man1p<span style="color: #000000; font-weight: bold;">/</span>nohup.1p.gz <span style="color: #000000; font-weight: bold;">/</span>usr<span style="color: #000000; font-weight: bold;">/</span>share<span style="color: #000000; font-weight: bold;">/</span>man<span style="color: #000000; font-weight: bold;">/</span>man1<span style="color: #000000; font-weight: bold;">/</span>nohup.1.gz
&nbsp;
<span style="color: #666666; font-style: italic;"># sh</span>
<span style="color: #000000; font-weight: bold;">%</span> <span style="color: #c20cb9; font-weight: bold;">which</span> <span style="color: #c20cb9; font-weight: bold;">nohup</span>
<span style="color: #000000; font-weight: bold;">/</span>usr<span style="color: #000000; font-weight: bold;">/</span>bin<span style="color: #000000; font-weight: bold;">/</span><span style="color: #c20cb9; font-weight: bold;">nohup</span>
<span style="color: #000000; font-weight: bold;">%</span> <span style="color: #c20cb9; font-weight: bold;">whereis</span> <span style="color: #c20cb9; font-weight: bold;">nohup</span>
<span style="color: #c20cb9; font-weight: bold;">nohup</span>: <span style="color: #000000; font-weight: bold;">/</span>usr<span style="color: #000000; font-weight: bold;">/</span>bin<span style="color: #000000; font-weight: bold;">/</span><span style="color: #c20cb9; font-weight: bold;">nohup</span> <span style="color: #000000; font-weight: bold;">/</span>usr<span style="color: #000000; font-weight: bold;">/</span>share<span style="color: #000000; font-weight: bold;">/</span>man<span style="color: #000000; font-weight: bold;">/</span>man1p<span style="color: #000000; font-weight: bold;">/</span>nohup.1p.gz <span style="color: #000000; font-weight: bold;">/</span>usr<span style="color: #000000; font-weight: bold;">/</span>share<span style="color: #000000; font-weight: bold;">/</span>man<span style="color: #000000; font-weight: bold;">/</span>man1<span style="color: #000000; font-weight: bold;">/</span>nohup.1.gz</pre></td></tr></table></div>




<p>I knew it was <strong>shell dependent</strong> but I couldn&#8217;t see why. So I turned to the <strong>C-shell man page</strong>, and here was my first clue.</p>

<blockquote><p>       hup [command] (+)<br />
               With command, runs command such that it will exit on a hangup signal and arranges for the shell to send it a hangup signal when the<br />
               shell  exits.   Note  that  commands may set their own response to hangups, overriding hup.  Without an argument (allowed in only a<br />
               shell script), causes the shell to exit on a hangup for the remainder of the script.   See  also  Signal  handling  and  the  nohup<br />
               builtin command.</p></blockquote>

<p>The bit at the end mentioned that <strong>C-shell</strong> had its own <strong>builtin</strong> version of nohup. A light bulb immediately went off and I now knew why <strong>nohup</strong> was behaving <strong>differently</strong> between <strong>C-shell</strong> and <strong>Bourne shell</strong>. Because there were in fact <strong>2 different versions of nohup</strong>. One was built into <strong>C-shell</strong>, while the other was an executable on the system located here, <strong>/usr/bin/nohup</strong>. The version built into <strong>C-shell</strong> apparently <strong>doesn&#8217;t automatically create the nohup.out</strong> file, you have to do this yourself by redirecting <strong><span class="caps">STDOUT</span></strong> and <strong><span class="caps">STDERR</span></strong>. <strong>Bourne shell</strong>, on the other hand, doesn&#8217;t have its own version of nohup, and so it relies on the one located at <strong>/usr/bin/nohup</strong>. This version of nohup <strong>does automatically create</strong> the <strong>nohup.out</strong> file.</p>

<p>Additionally when I referred to the <strong>man page</strong> for <strong>/usr/bin/nohup</strong> I noticed this comment:</p>

<blockquote><p>       <span class="caps">NOTE</span>: your shell may have its own version of nohup, which usually supersedes the version described here.  Please refer to your shell&#8217;s doc-<br />
       umentation for details about the options it supports.</p></blockquote>

<p>So I told Evan that if he wants to get the nohup.out file he needs to call it explicitly with the full path like this:</p>


<div class="wp_codebox"><table width="100%" ><tr id="p234986"><td class="line_numbers"><pre>1
</pre></td><td class="code" id="p2349code86"><pre class="bash" style="font-family:monospace;"><span style="color: #000000; font-weight: bold;">/</span>usr<span style="color: #000000; font-weight: bold;">/</span>bin<span style="color: #000000; font-weight: bold;">/</span><span style="color: #c20cb9; font-weight: bold;">nohup</span> <span style="color: #c20cb9; font-weight: bold;">sleep</span> <span style="color: #000000;">10</span> <span style="color: #000000; font-weight: bold;">&amp;</span></pre></td></tr></table></div>



<p>  <br />
The following <span class="caps">URL</span>s proved useful in helping me to round out my understanding of nohup.</p>


<ol>
<li><a href="http://ubuntuforums.org/archive/index.php/t-35238.html">How to keep a process running when I log out?</a></li>
<li><a href="http://www.cyberciti.biz/tips/nohup-execute-commands-after-you-exit-from-a-shell-prompt.html">nohup Execute Commands After You Exit From a Shell Prompt</a></li>
<li><a href="http://web.mit.edu/ghudson/info/after-logout">How to make processes stay alive after a logout</a></li>
<li><a href="http://www.commandlinefu.com/commands/view/1417/nohup-that-doesnt-generate-nohup.out">nohup that doesn&#8217;t generate nohup.out</a></li>
</ol>



<p><strong><span class="caps">NOTE</span>:</strong> For further details regarding my <strong>one-liner</strong> blog posts, check out my <a href="http://www.lamolabs.org/blog/2147/intro-blogs-one-liner-code-block-style-guides/">one-liner style guide primer</a>.</p><div id="wherego_related"><h3>Readers who viewed this page, also viewed:</h3><ul><li><a href="http://www.lamolabs.org/blog/1766/pushing-pulling-files-around-using-tar-ssh-scp-rsync/" rel="bookmark" class="wherego_title">Pushing &#038; Pulling Files Around Using tar, ssh, scp, &#038; rsync</a></li><li><a href="http://www.lamolabs.org/blog/3520/one-liner-using-yum-to-download-rpms-under-fedora-centos-rhel/" rel="bookmark" class="wherego_title">[one-liner]: Using Yum to Download RPMs under Fedora, CentOS, and RHEL</a></li><li><a href="http://www.lamolabs.org/blog/3547/one-liner-getting-rid-of-googles-left-sidebar/" rel="bookmark" class="wherego_title">[one-liner]: Getting Rid of Google&#8217;s Left Sidebar (1st Attempt)</a></li><li><a href="http://www.lamolabs.org/blog/2147/intro-blogs-one-liner-code-block-style-guides/" rel="bookmark" class="wherego_title">Intro to my Blog&#8217;s [one-liner] Code Block Style Guides</a></li><li>Powered by <a href="http://ajaydsouza.com/wordpress/plugins/where-did-they-go-from-here/">Where did they go from here?</a></li></ul></div>]]></content:encoded>
			<wfw:commentRss>http://www.lamolabs.org/blog/2349/one-liner-how-to-properly-use-nohup/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>[one-liner]: Port Forwarding Using PuTTY</title>
		<link>http://www.lamolabs.org/blog/2234/one-liner-port-forwarding-using-putty/</link>
		<comments>http://www.lamolabs.org/blog/2234/one-liner-port-forwarding-using-putty/#comments</comments>
		<pubDate>Sat, 15 Aug 2009 06:02:56 +0000</pubDate>
		<dc:creator>slmingol</dc:creator>
				<category><![CDATA[tips & tricks]]></category>
		<category><![CDATA[networking]]></category>
		<category><![CDATA[one-liner]]></category>
		<category><![CDATA[server]]></category>
		<category><![CDATA[shell]]></category>
		<category><![CDATA[sysadmin]]></category>
		<category><![CDATA[tip]]></category>
		<category><![CDATA[tunnelling]]></category>

		<guid isPermaLink="false">http://www.lamolabs.org/blog/?p=2234</guid>
		<description><![CDATA[<p>Here&#8217;s a little trick that I learned how to do the other day while at work. I&#8217;ve done this before using ssh in a linux terminal but hadn&#8217;t had the chance to actually do it from a windows box using PuTTY.</p>

Objective

<p>To access a web server (on a remote linux box) which is listening on port [...]]]></description>
			<content:encoded><![CDATA[<p>Here&#8217;s a little trick that I learned how to do the other day while at work. I&#8217;ve done this before using ssh in a linux terminal but hadn&#8217;t had the chance to actually do it from a windows box using <a href="http://www.chiark.greenend.org.uk/~sgtatham/putty/">PuTTY</a>.</p>

<h4>Objective</h4>

<p>To access a <strong>web server</strong> (on a remote linux box) which is listening on <strong>port 80</strong> through an <strong>ssh connection</strong>. We want to access the web server through <strong>port 10001</strong> on the <strong>localhost</strong>. This can be accomplished by mapping localhost&#8217;s port 10001 to port 80 on the remote host.</p>

<p>Here&#8217;s a diagram that attempts to represent what&#8217;s going on with the ports, the hosts, and the ssh connection.</p>

<div id="attachment_2252" class="wp-caption aligncenter" style="width: 588px"><a href="http://www.lamolabs.org/blog/wp-content/uploads/2009/08/ss_putty_port_forward_6.png" rel="lightbox[2234]"><img src="http://www.lamolabs.org/blog/wp-content/uploads/2009/08/ss_putty_port_forward_6.png" alt="ports diagram" title="ss_putty_port_forward_6" width="578" height="208" class="size-full wp-image-2252" /></a><p class="wp-caption-text">ports diagram</p></div>

<h4>Setup</h4>

<p>In this scenario we have <strong>2 hosts</strong>, the <strong>local host</strong> (i.e. localhost) and the <strong>remote host</strong> (homer). Running PuTTY on the localhost, we specify that we want to login to homer as a user on that system, i.e. root, for example. It doesn&#8217;t have to be root, this just happens to be the user that I&#8217;m using in this example.<br />
 <br />
<div id="attachment_2240" class="wp-caption aligncenter" style="width: 464px"><a href="http://www.lamolabs.org/blog/wp-content/uploads/2009/08/ss_putty_port_forward_1.png" rel="lightbox[2234]"><img src="http://www.lamolabs.org/blog/wp-content/uploads/2009/08/ss_putty_port_forward_1.png" alt="putty dialog #1" title="ss_putty_port_forward_1" width="454" height="435" class="size-full wp-image-2240" /></a><p class="wp-caption-text">putty dialog #1</p></div></p>

<p>Next in the <strong>PuTTY Configuration dialog box</strong>, expand the <strong>category <span class="caps">SSH</span></strong>, and select the <strong>category Tunnels</strong>. In <strong>Tunnels&#8217;</strong> configuration, specify the <strong>Source port</strong>, 10001, and the <strong>Destination</strong>, homer:80. Then click the <strong>Add button</strong>.</p>

<div id="attachment_2241" class="wp-caption aligncenter" style="width: 466px"><a href="http://www.lamolabs.org/blog/wp-content/uploads/2009/08/ss_putty_port_forward_2.png" rel="lightbox[2234]"><img src="http://www.lamolabs.org/blog/wp-content/uploads/2009/08/ss_putty_port_forward_2.png" alt="putty dialog #2" title="ss_putty_port_forward_2" width="456" height="434" class="size-full wp-image-2241" /></a><p class="wp-caption-text">putty dialog #2</p></div>

<p><span id="more-2234"></span></p>

<p>At this point the <strong>PuTTY Configuration dialog</strong> should look like this, after clicking the <strong>Add button</strong>. Now go ahead and connect to the remote host by clicking the <strong>Open button</strong>, and login as you&#8217;d normally would to the remote host.</p>

<div id="attachment_2242" class="wp-caption aligncenter" style="width: 466px"><a href="http://www.lamolabs.org/blog/wp-content/uploads/2009/08/ss_putty_port_forward_3.png" rel="lightbox[2234]"><img src="http://www.lamolabs.org/blog/wp-content/uploads/2009/08/ss_putty_port_forward_3.png" alt="putty dialog #3" title="ss_putty_port_forward_3" width="456" height="434" class="size-full wp-image-2242" /></a><p class="wp-caption-text">putty dialog #3</p></div>

<p>After successfully logging in, you should be sitting at your shells prompt.</p>

<div id="attachment_2243" class="wp-caption aligncenter" style="width: 660px"><a href="http://www.lamolabs.org/blog/wp-content/uploads/2009/08/ss_putty_port_forward_4.png" rel="lightbox[2234]"><img src="http://www.lamolabs.org/blog/wp-content/uploads/2009/08/ss_putty_port_forward_4-650x403.png" alt="putty dialog #4" title="ss_putty_port_forward_4" width="650" height="403" class="size-medium wp-image-2243" /></a><p class="wp-caption-text">putty dialog #4</p></div>

<p>Now in your web browser put in the <span class="caps">URL </span><strong>http://localhost:10001/</strong>, and you should see the front page for the web server running on the remote host, homer.</p>

<div id="attachment_2244" class="wp-caption aligncenter" style="width: 860px"><a href="http://www.lamolabs.org/blog/wp-content/uploads/2009/08/ss_putty_port_forward_5.png" rel="lightbox[2234]"><img src="http://www.lamolabs.org/blog/wp-content/uploads/2009/08/ss_putty_port_forward_5-850x243.png" alt="firefox screenshot" title="ss_putty_port_forward_5" width="850" height="243" class="size-large wp-image-2244" /></a><p class="wp-caption-text">firefox screenshot</p></div>

<p><strong><span class="caps">NOTE</span>:</strong> For further details regarding my <strong>one-liner</strong> blog posts, check out my <a href="http://www.lamolabs.org/blog/2147/intro-blogs-one-liner-code-block-style-guides/">one-liner style guide primer</a>.</p><div id="wherego_related"><h3>Readers who viewed this page, also viewed:</h3><ul><li><a href="http://www.lamolabs.org/blog/2147/intro-blogs-one-liner-code-block-style-guides/" rel="bookmark" class="wherego_title">Intro to my Blog&#8217;s [one-liner] Code Block Style Guides</a></li><li>Powered by <a href="http://ajaydsouza.com/wordpress/plugins/where-did-they-go-from-here/">Where did they go from here?</a></li></ul></div>]]></content:encoded>
			<wfw:commentRss>http://www.lamolabs.org/blog/2234/one-liner-port-forwarding-using-putty/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Managing OpenVZ Instance Descriptions</title>
		<link>http://www.lamolabs.org/blog/2094/managing-openvz-instance-descriptions/</link>
		<comments>http://www.lamolabs.org/blog/2094/managing-openvz-instance-descriptions/#comments</comments>
		<pubDate>Mon, 27 Jul 2009 03:31:01 +0000</pubDate>
		<dc:creator>slmingol</dc:creator>
				<category><![CDATA[tutorials]]></category>
		<category><![CDATA[shell]]></category>
		<category><![CDATA[tip]]></category>
		<category><![CDATA[virtualization]]></category>

		<guid isPermaLink="false">http://www.lamolabs.org/blog/?p=2094</guid>
		<description><![CDATA[<p>I recently discovered that you can assign descriptions to your OpenVZ instances. This isn&#8217;t really that surprising but I never really took the the time until now to scan through the vzctl and vzlist man pages. Prior to discovering these more esoteric features of vzctl and vzlist, I&#8217;d normally just run this command to see [...]]]></description>
			<content:encoded><![CDATA[<p>I recently discovered that you can assign <strong>descriptions</strong> to your <a href="http://wiki.openvz.org/Main_Page">OpenVZ</a> instances. This isn&#8217;t really that surprising but I never really took the the time until now to scan through the <strong>vzctl</strong> and <strong>vzlist</strong> man pages. Prior to discovering these more esoteric features of <strong>vzctl</strong> and <strong>vzlist</strong>, I&#8217;d normally just run this command to see what&#8217;s what with my <strong>OpenVZ</strong> instances:</p>


<div class="wp_codebox"><table width="100%" ><tr id="p209491"><td class="line_numbers"><pre>1
2
3
4
5
6
7
8
9
10
11
12
13
14
</pre></td><td class="code" id="p2094code91"><pre class="bash" style="font-family:monospace;"><span style="color: #666666; font-style: italic;"># list of all VE instances</span>
<span style="color: #000000; font-weight: bold;">%</span> vzlist <span style="color: #660033;">-a</span>
      CTID      NPROC STATUS  IP_ADDR         HOSTNAME                        
       <span style="color: #000000;">101</span>         <span style="color: #000000;">32</span> running 10.1.1.101      flanders.mydom.net              
       <span style="color: #000000;">102</span>         <span style="color: #000000;">49</span> running 10.1.1.102      lisa.mydom.net                  
       <span style="color: #000000;">103</span>         <span style="color: #000000;">36</span> running -               bart.mydom.net                  
       <span style="color: #000000;">104</span>         <span style="color: #000000;">38</span> running 10.1.1.104      marge.mydom.net                 
       <span style="color: #000000;">105</span>         <span style="color: #000000;">34</span> running 10.1.1.105      homer.mydom.net                 
       <span style="color: #000000;">106</span>         <span style="color: #000000;">31</span> running 10.1.1.106      kang.mydom.net                  
       <span style="color: #000000;">107</span>         <span style="color: #000000;">27</span> running 10.1.1.107      kodos.mydom.net                 
       <span style="color: #000000;">108</span>         <span style="color: #000000;">32</span> running 10.1.1.108      maude.mydom.net                 
       <span style="color: #000000;">109</span>         <span style="color: #000000;">30</span> running 10.1.1.109      nelson.mydom.net                
       <span style="color: #000000;">110</span>         <span style="color: #000000;">32</span> running 10.1.1.110      ralphie.mydom.net               
       <span style="color: #000000;">111</span>         <span style="color: #000000;">30</span> running 10.1.1.111      martin.mydom.net</pre></td></tr></table></div>




<p>This had worked fine, but sometimes I&#8217;d draw a blank about what&#8217;s running in each instance, hence my need for the description column. You can use the following command to see the description of each instance.</p>


<div class="wp_codebox"><table width="100%" ><tr id="p209492"><td class="line_numbers"><pre>1
2
3
4
5
6
7
8
9
10
11
12
13
14
</pre></td><td class="code" id="p2094code92"><pre class="bash" style="font-family:monospace;"><span style="color: #666666; font-style: italic;"># initial description columns for VEs</span>
<span style="color: #000000; font-weight: bold;">%</span> vzlist <span style="color: #660033;">-o</span> ctid,numproc,status,ip,<span style="color: #c20cb9; font-weight: bold;">hostname</span>,description
      CTID      NPROC STATUS  IP_ADDR         HOSTNAME                         DESCRIPTION                     
       <span style="color: #000000;">101</span>         <span style="color: #000000;">34</span> running 10.1.1.101      flanders.mydom.net               -                               
       <span style="color: #000000;">102</span>         <span style="color: #000000;">49</span> running 10.1.1.102      lisa.mydom.net                   -                               
       <span style="color: #000000;">103</span>         <span style="color: #000000;">35</span> running -               bart.mydom.net                   -                               
       <span style="color: #000000;">104</span>         <span style="color: #000000;">38</span> running 10.1.1.104      marge.mydom.net                  -                               
       <span style="color: #000000;">105</span>         <span style="color: #000000;">34</span> running 10.1.1.105      homer.mydom.net                  -                               
       <span style="color: #000000;">106</span>         <span style="color: #000000;">31</span> running 10.1.1.106      kang.mydom.net                   -                               
       <span style="color: #000000;">107</span>         <span style="color: #000000;">27</span> running 10.1.1.107      kodos.mydom.net                  -                               
       <span style="color: #000000;">108</span>         <span style="color: #000000;">32</span> running 10.1.1.108      maude.mydom.net                  -                               
       <span style="color: #000000;">109</span>         <span style="color: #000000;">30</span> running 10.1.1.109      nelson.mydom.net                 -                               
       <span style="color: #000000;">110</span>         <span style="color: #000000;">32</span> running 10.1.1.110      ralphie.mydom.net                -                               
       <span style="color: #000000;">111</span>         <span style="color: #000000;">30</span> running 10.1.1.111      martin.mydom.net                 -</pre></td></tr></table></div>




<p>I used the following commands to set the description for each <acronym title="Virtual Environment">VE</acronym>.</p>


<div class="wp_codebox"><table width="100%" ><tr id="p209493"><td class="line_numbers"><pre>1
2
3
4
5
6
7
8
9
10
11
</pre></td><td class="code" id="p2094code93"><pre class="bash" style="font-family:monospace;">vzctl <span style="color: #000000; font-weight: bold;">set</span> 101 <span style="color: #660033;">--description</span> <span style="color: #ff0000;">&quot;nis server&quot;</span> <span style="color: #660033;">--save</span>
vzctl <span style="color: #000000; font-weight: bold;">set</span> 102 <span style="color: #660033;">--description</span> <span style="color: #ff0000;">&quot;mail,imap,smtp server&quot;</span> <span style="color: #660033;">--save</span>
vzctl <span style="color: #000000; font-weight: bold;">set</span> 103 <span style="color: #660033;">--description</span> <span style="color: #ff0000;">&quot;samba server&quot;</span> <span style="color: #660033;">--save</span>
vzctl <span style="color: #000000; font-weight: bold;">set</span> 104 <span style="color: #660033;">--description</span> <span style="color: #ff0000;">&quot;mysql server&quot;</span> <span style="color: #660033;">--save</span>
vzctl <span style="color: #000000; font-weight: bold;">set</span> 105 <span style="color: #660033;">--description</span> <span style="color: #ff0000;">&quot;www,blog,webmail server&quot;</span> <span style="color: #660033;">--save</span>
vzctl <span style="color: #000000; font-weight: bold;">set</span> 106 <span style="color: #660033;">--description</span> <span style="color: #ff0000;">&quot;trac,git,svn server&quot;</span> <span style="color: #660033;">--save</span>
vzctl <span style="color: #000000; font-weight: bold;">set</span> 107 <span style="color: #660033;">--description</span> <span style="color: #ff0000;">&quot;trac,git,svn server (dev)&quot;</span> <span style="color: #660033;">--save</span>
vzctl <span style="color: #000000; font-weight: bold;">set</span> 108 <span style="color: #660033;">--description</span> <span style="color: #ff0000;">&quot;ldap server (dev)&quot;</span> <span style="color: #660033;">--save</span>
vzctl <span style="color: #000000; font-weight: bold;">set</span> 109 <span style="color: #660033;">--description</span> <span style="color: #ff0000;">&quot;cacti server&quot;</span> <span style="color: #660033;">--save</span>
vzctl <span style="color: #000000; font-weight: bold;">set</span> 110 <span style="color: #660033;">--description</span> <span style="color: #ff0000;">&quot;tracks server&quot;</span> <span style="color: #660033;">--save</span>
vzctl <span style="color: #000000; font-weight: bold;">set</span> 111 <span style="color: #660033;">--description</span> <span style="color: #ff0000;">&quot;wiki server&quot;</span> <span style="color: #660033;">--save</span></pre></td></tr></table></div>




<p>Re-running the <strong>vzlist</strong> command from before now shows the newly added descriptions.</p>


<div class="wp_codebox"><table width="100%" ><tr id="p209494"><td class="line_numbers"><pre>1
2
3
4
5
6
7
8
9
10
11
12
13
14
</pre></td><td class="code" id="p2094code94"><pre class="bash" style="font-family:monospace;"><span style="color: #666666; font-style: italic;"># filled out description columns for VEs</span>
<span style="color: #000000; font-weight: bold;">%</span> vzlist <span style="color: #660033;">-o</span> ctid,numproc,status,ip,<span style="color: #c20cb9; font-weight: bold;">hostname</span>,description
      CTID      NPROC STATUS  IP_ADDR         HOSTNAME                         DESCRIPTION                     
       101         32 running 10.1.1.101      flanders.mydom.net               nis server                      
       102         49 running 10.1.1.102      lisa.mydom.net                   mail,imap,smtp server           
       103         36 running -               bart.mydom.net                   samba server                    
       104         38 running 10.1.1.104      marge.mydom.net                  mysql server                    
       105         34 running 10.1.1.105      homer.mydom.net                  www,blog,webmail server         
       106         31 running 10.1.1.106      kang.mydom.net                   trac,git,<span style="color: #c20cb9; font-weight: bold;">svn</span> server             
       107         27 running 10.1.1.107      kodos.mydom.net                  trac,git,<span style="color: #c20cb9; font-weight: bold;">svn</span> server <span style="color: #7a0874; font-weight: bold;">&#40;</span>dev<span style="color: #7a0874; font-weight: bold;">&#41;</span>       
       <span style="color: #000000;">108</span>         <span style="color: #000000;">32</span> running 10.1.1.108      maude.mydom.net                  ldap server <span style="color: #7a0874; font-weight: bold;">&#40;</span>dev<span style="color: #7a0874; font-weight: bold;">&#41;</span>               
       <span style="color: #000000;">109</span>         <span style="color: #000000;">30</span> running 10.1.1.109      nelson.mydom.net                 cacti server                    
       <span style="color: #000000;">110</span>         <span style="color: #000000;">32</span> running 10.1.1.110      ralphie.mydom.net                tracks server                   
       <span style="color: #000000;">111</span>         <span style="color: #000000;">30</span> running 10.1.1.111      martin.mydom.net                 wiki server</pre></td></tr></table></div>
<div id="wherego_related"> </div>]]></content:encoded>
			<wfw:commentRss>http://www.lamolabs.org/blog/2094/managing-openvz-instance-descriptions/feed/</wfw:commentRss>
		<slash:comments>1</slash:comments>
		</item>
	</channel>
</rss>
