<?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>No Znx! &#187; shell</title>
	<atom:link href="http://znx.no/tag/shell/feed/" rel="self" type="application/rss+xml" />
	<link>http://znx.no</link>
	<description>the pigeons!!!!</description>
	<lastBuildDate>Fri, 21 May 2010 16:05:24 +0000</lastBuildDate>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.org/?v=3.0</generator>
		<item>
		<title>Unrar Bug</title>
		<link>http://znx.no/2010/02/unrar-bu/</link>
		<comments>http://znx.no/2010/02/unrar-bu/#comments</comments>
		<pubDate>Mon, 22 Feb 2010 01:29:50 +0000</pubDate>
		<dc:creator>znx</dc:creator>
				<category><![CDATA[Linux]]></category>
		<category><![CDATA[rpm]]></category>
		<category><![CDATA[shell]]></category>

		<guid isPermaLink="false">http://znx.no/?p=274</guid>
		<description><![CDATA[So I was recently scripting with the unrar tool and discovered something stupid: unrar t file.rar if &#91; $? -eq 0 &#93;; then echo &#34;Rar file is good?&#34; fi However it was returning zero all the time, even when the file wasn&#8217;t a rar: # unrar t file.rar &#160; UNRAR 3.80 freeware Copyright &#40;c&#41; 1993-2008 [...]]]></description>
			<content:encoded><![CDATA[<p>So I was recently scripting with the unrar tool and discovered something stupid:</p>

<div class="wp_syntax"><div class="code"><pre class="bash" style="font-family:monospace;">unrar t file.rar
<span style="color: #000000; font-weight: bold;">if</span> <span style="color: #7a0874; font-weight: bold;">&#91;</span> <span style="color: #007800;">$?</span> <span style="color: #660033;">-eq</span> <span style="color: #000000;">0</span> <span style="color: #7a0874; font-weight: bold;">&#93;</span>; <span style="color: #000000; font-weight: bold;">then</span>
  <span style="color: #7a0874; font-weight: bold;">echo</span> <span style="color: #ff0000;">&quot;Rar file is good?&quot;</span>
<span style="color: #000000; font-weight: bold;">fi</span></pre></div></div>

<p>However it was returning zero all the time, even when the file wasn&#8217;t a rar:</p>

<div class="wp_syntax"><div class="code"><pre class="bash" style="font-family:monospace;"><span style="color: #666666; font-style: italic;"># unrar t file.rar</span>
&nbsp;
UNRAR <span style="color: #000000;">3.80</span> freeware      Copyright <span style="color: #7a0874; font-weight: bold;">&#40;</span>c<span style="color: #7a0874; font-weight: bold;">&#41;</span> <span style="color: #000000;">1993</span>-<span style="color: #000000;">2008</span> Alexander Roshal
&nbsp;
file.rar is not RAR archive
<span style="color: #666666; font-style: italic;"># echo $?</span>
<span style="color: #000000;">0</span></pre></div></div>

<p>So it fails the test but returns zero regardless. This makes it very unhelpful for using in scripting. Fortunately enough a mate on IRC discovered that his version did.</p>
<p>So I first download the existing SRPM and installed it:</p>

<div class="wp_syntax"><div class="code"><pre class="bash" style="font-family:monospace;"><span style="color: #666666; font-style: italic;"># yumdownloader --source unrar</span>
<span style="color: #666666; font-style: italic;"># rpm -i unrar*.srpm</span></pre></div></div>

<p>Then I installed that and simply modified  so I downloaded the latest, created a RPM and installed.</p>
<p>I have submitted the updated spec file to <a href='https://bugzilla.rpmfusion.org/show_bug.cgi?id=1091'>RPMfusion</a>.</p>
]]></content:encoded>
			<wfw:commentRss>http://znx.no/2010/02/unrar-bu/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Import Missing GPG Keys</title>
		<link>http://znx.no/2010/01/import-missing-gpg-keys/</link>
		<comments>http://znx.no/2010/01/import-missing-gpg-keys/#comments</comments>
		<pubDate>Sun, 17 Jan 2010 17:58:39 +0000</pubDate>
		<dc:creator>znx</dc:creator>
				<category><![CDATA[General]]></category>
		<category><![CDATA[centos]]></category>
		<category><![CDATA[gpg]]></category>
		<category><![CDATA[install]]></category>
		<category><![CDATA[shell]]></category>
		<category><![CDATA[tips]]></category>

		<guid isPermaLink="false">http://znx.no/?p=231</guid>
		<description><![CDATA[Sometimes when people are using Linux they will find that they come across a simple security check that confirms the authenticity of the RPM/Deb files that they are installing. Every so often the key is missing but that is easy to remedy. Here I will use an example with the YUM install of Adobe Flash: [...]]]></description>
			<content:encoded><![CDATA[<p>Sometimes when people are using Linux they will find that they come across a simple security check that confirms the authenticity of the RPM/Deb files that they are installing. Every so often the key is missing but that is easy to remedy. Here I will use an example with the YUM install of Adobe Flash:</p>
<pre>[root@marine]# ls
adobe-release-i386-1.0-1.noarch.rpm
[root@marine]# yum localinstall adobe-release-i386-1.0-1.noarch.rpm
Loaded plugins: fastestmirror, priorities
Setting up Local Package Process
Examining adobe-release-i386-1.0-1.noarch.rpm: adobe-release-i386-1.0-1.noarch
Marking adobe-release-i386-1.0-1.noarch.rpm to be installed
Loading mirror speeds from cached hostfile
* addons: anorien.csc.warwick.ac.uk
* base: anorien.csc.warwick.ac.uk
* centosplus: anorien.csc.warwick.ac.uk
* contrib: anorien.csc.warwick.ac.uk
* extras: anorien.csc.warwick.ac.uk
* rpmforge: fr2.rpmfind.net
* updates: anorien.csc.warwick.ac.uk
588 packages excluded due to repository priority protections
Resolving Dependencies
--&gt; Running transaction check
---&gt; Package adobe-release-i386.noarch 0:1.0-1 set to be updated
--&gt; Finished Dependency Resolution

Dependencies Resolved

================================================================================
Package              Arch     Version Repository                          Size
================================================================================
Installing:
adobe-release-i386   noarch   1.0-1   /adobe-release-i386-1.0-1.noarch   1.9 k

Transaction Summary
================================================================================
Install      1 Package(s)
Update       0 Package(s)
Remove       0 Package(s)

Total size: 1.9 k
Is this ok [y/N]: y
Downloading Packages:
warning: rpmts_HdrFromFdno: Header V3 DSA signature: NOKEY, key ID f6777c67

Public key for adobe-release-i386-1.0-1.noarch.rpm is not installed</pre>
<p>As you can see it attempted to install but because the key was missing it refused to install. So what do we do to find that key? First we must download the key from somewhere, there is various key servers around the world, some of the most popular are (note hkp is the HTTP keyserver protocol):</p>
<ul>
<li>hkp://subkeys.pgp.net</li>
<li>hkp://pgp.mit.edu</li>
<li>hkp://keys.gnupg.net</li>
<li>hkp://wwwkeys.uk.pgp.net  (where UK can be replaced by any country code)</li>
</ul>
<p>So lets search for the missing key:</p>
<pre>[root@marine]# gpg --keyserver hkp://wwwkeys.uk.pgp.net --recv-keys f6777c67
gpg: requesting key F6777C67 from hkp server wwwkeys.uk.pgp.net
gpg: /root/.gnupg/trustdb.gpg: trustdb created
gpg: key F6777C67: public key "Adobe Systems Incorporated (Linux RPM
  Signing Key) <secure@adobe.com>" imported
gpg: no ultimately trusted keys found
gpg: Total number processed: 1
gpg:               imported: 1</pre>
<p>So now we have the key and see that it is indeed Adobe&#8217;s key. Now all we need to do is add into RPM:</p>
<pre>[root@marine]# gpg --armor --export f6777c67 >tmp-gpg.key
[root@marine]# rpm --import tmp-gpg.key</pre>
<p>Simple as that, the install will work now!</p>
<p>This post is a refreshed look at a <a href='http://znx.no/2009/08/gpg-errors/'>previous</a> post on the same matter.</p>
]]></content:encoded>
			<wfw:commentRss>http://znx.no/2010/01/import-missing-gpg-keys/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Skinning A Cat</title>
		<link>http://znx.no/2009/05/skinning-a-cat/</link>
		<comments>http://znx.no/2009/05/skinning-a-cat/#comments</comments>
		<pubDate>Sun, 17 May 2009 02:02:36 +0000</pubDate>
		<dc:creator>znx</dc:creator>
				<category><![CDATA[Linux]]></category>
		<category><![CDATA[Scripts]]></category>
		<category><![CDATA[cli]]></category>
		<category><![CDATA[shell]]></category>

		<guid isPermaLink="false">http://znx.no/?p=116</guid>
		<description><![CDATA[I was recently discussing with someone about sed usage. They were having difficultly creating an appropriate regex to handle their problem: (Orlando) sed regex kicks my ass. I like to remove the second : in the line. So that in 123:456:6789 it will only returns 123:456. I can find the first :, but I have [...]]]></description>
			<content:encoded><![CDATA[<p>I was recently discussing with someone about <code>sed</code> usage. They were having difficultly creating an appropriate regex to handle their problem:</p>
<pre>(Orlando) sed regex kicks my ass.  I like to remove the second : in the
line. So that in 123:456:6789 it will only returns 123:456.  I can find the
first :, but I have not been able to use s/:{2}// to find the second
one, and remove the rest.</pre>
<p>I enjoy regex (I know I&#8217;m weird, leave me alone), so I was able to provide an answer to this problem:</p>
<pre>(@znx) Orlando: the trick is to use [^:] and \1 ..
(@znx) like:   s/\(:[^:]*\):.*/\1/</pre>
<p>Now obviously at first glance this regex could be a bit intimating to someone who is still picking up the skills but as with most things if we break it down it becomes easier.</p>
<p>Working out to in, <code>\(    \):.*</code>, that says match something with <strong>:</strong> at the end and all the character after it. The <strong>.</strong> is a special meaning &#8220;any character&#8221; and <strong>*</strong> to match multiple characters. The first match will be stored by <code>sed</code> and assigned into the <strong>\1</strong> for the replacement (that is what the brackets do). Inside the brackets we have <code>:[^:]*</code>. The sequence <strong>[^ ]</strong> is a negated list, that means that we are asking it to match everything that is NOT inside the list, in this case <strong>:</strong>.</p>
<p>Putting it altogether we are saying: Match a leading : and a trailing : with any characters after it. Placing the contents between the two <strong>:</strong> in memory. Then finally we replace the contents.</p>

<div class="wp_syntax"><div class="code"><pre class="bash" style="font-family:monospace;"><span style="color: #000000; font-weight: bold;">%</span> <span style="color: #7a0874; font-weight: bold;">echo</span> <span style="color: #000000;">123</span>:<span style="color: #000000;">456</span>:<span style="color: #000000;">6789</span> <span style="color: #000000; font-weight: bold;">|</span> <span style="color: #c20cb9; font-weight: bold;">sed</span> <span style="color: #ff0000;">'s/\(:[^:]*\):.*/\1/'</span>
<span style="color: #000000;">123</span>:<span style="color: #000000;">456</span></pre></div></div>

<p>Sucess, however as with most things, there is more than one way to skin a cat and regex is rarely the prettiest method. So what other ways can we solve this problem?</p>
<p>With AWK:</p>

<div class="wp_syntax"><div class="code"><pre class="bash" style="font-family:monospace;"><span style="color: #000000; font-weight: bold;">%</span> <span style="color: #7a0874; font-weight: bold;">echo</span> <span style="color: #000000;">123</span>:<span style="color: #000000;">456</span>:<span style="color: #000000;">6789</span> <span style="color: #000000; font-weight: bold;">|</span> <span style="color: #c20cb9; font-weight: bold;">awk</span> -F: <span style="color: #ff0000;">'{print $1&quot;:&quot;$2}'</span>
<span style="color: #000000;">123</span>:<span style="color: #000000;">456</span></pre></div></div>

<p>With <code>cut</code>:</p>

<div class="wp_syntax"><div class="code"><pre class="bash" style="font-family:monospace;"><span style="color: #000000; font-weight: bold;">%</span> <span style="color: #7a0874; font-weight: bold;">echo</span> <span style="color: #000000;">123</span>:<span style="color: #000000;">456</span>:<span style="color: #000000;">6789</span> <span style="color: #000000; font-weight: bold;">|</span> <span style="color: #c20cb9; font-weight: bold;">cut</span> -d: -f1,<span style="color: #000000;">2</span>
<span style="color: #000000;">123</span>:<span style="color: #000000;">456</span></pre></div></div>

<p>As always, experimentation with the mass of GNU tools you can find on your system will bring a greater deal of power to your tool chest. Mind you, then I wouldn&#8217;t get complements for helping would I?</p>
<pre>(Orlando) Wow, When I grow up, I like to remember this thing like you do. <img src='http://znx.no/wp-includes/images/smilies/icon_smile.gif' alt=':-)' class='wp-smiley' /> </pre>
<p>Haha, till next time!</p>
]]></content:encoded>
			<wfw:commentRss>http://znx.no/2009/05/skinning-a-cat/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>File Oddity</title>
		<link>http://znx.no/2009/02/file-oddity/</link>
		<comments>http://znx.no/2009/02/file-oddity/#comments</comments>
		<pubDate>Fri, 13 Feb 2009 18:10:38 +0000</pubDate>
		<dc:creator>znx</dc:creator>
				<category><![CDATA[Linux]]></category>
		<category><![CDATA[Scripts]]></category>
		<category><![CDATA[shell]]></category>
		<category><![CDATA[tips]]></category>

		<guid isPermaLink="false">http://znx.no/?p=102</guid>
		<description><![CDATA[Today at work I was attempting to parse a file and discovered something odd happening. When I simply viewed the file with cat, I could see this: &#60;html&#62;&#60;head&#62;&#60;title&#62;Status&#60;/title&#62;&#60;/head&#62; &#60;table&#62; &#60;tr&#62;&#60;td&#62;Failed&#60;/td&#62;&#60;td&#62;Backup Group&#60;/td&#62;&#60;/tr&#62; &#60;tr&#62;&#60;td&#62;Success&#60;/td&#62;&#60;td&#62;Another Backup Group&#60;/td&#62;&#60;/tr&#62; &#60;/table&#62; &#60;/body&#62; &#60;/html&#62; Nothing odd there, the file is normal but when I tried this command: $ grep -i failed status.html [...]]]></description>
			<content:encoded><![CDATA[<p>Today at work I was attempting to parse a file and discovered something odd happening. When I simply viewed the file with cat, I could see this:</p>
<pre>&lt;html&gt;&lt;head&gt;&lt;title&gt;Status&lt;/title&gt;&lt;/head&gt;
&lt;table&gt;
&lt;tr&gt;&lt;td&gt;Failed&lt;/td&gt;&lt;td&gt;Backup Group&lt;/td&gt;&lt;/tr&gt;
&lt;tr&gt;&lt;td&gt;Success&lt;/td&gt;&lt;td&gt;Another Backup Group&lt;/td&gt;&lt;/tr&gt;
&lt;/table&gt;
&lt;/body&gt;
&lt;/html&gt;</pre>
<p>Nothing odd there, the file is normal but when I tried this command:</p>
<pre>$ grep -i failed status.html
$</pre>
<p>Huh? No output, suggesting that there is no lines with the words failed on them. The same occurs with awk and sed, indeed I could not find any tool to be able to grep out the status. So the next step was to check what was odd about the file:</p>
<pre>$ file status.html
status.html: HTML document text</pre>
<p>Still, nothing unusual. So now I am in full head scratching mode, I open up the file with vim to see if I can discover anything strange about the file but nothing. At this point I happened to switch to more rather than cat and the result was the start of how I solved it.</p>
<pre>$ more status.html
��&lt;

$</pre>
<p>Ah, so now I can see why grep and the others cannot view anything in the file. So this time I switch to vim again and check what file encoding we have:</p>
<pre>:set fileencoding
fileencoding=ucs-2le</pre>
<p>For those that are unaware, this is UCS-2 (little endian), also know as <a href="http://en.wikipedia.org/wiki/UTF-16">UTF-16</a>. So the issue was simply that we had UTF-16 characters, now for the trick to get around it:</p>
<pre>$ iconv -f UTF-16 -t UTF-8 status.html | grep -i failed
&lt;tr&gt;&lt;td&gt;Failed&lt;/td&gt;&lt;td&gt;Backup Group&lt;/td&gt;&lt;/tr&gt;
$</pre>
<p>Tada. Once more a solution!</p>
]]></content:encoded>
			<wfw:commentRss>http://znx.no/2009/02/file-oddity/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
	</channel>
</rss>
