<?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>Eliezer Rodrigues &#187; bash</title>
	<atom:link href="http://www.eliezer.com.br/post/tag/bash/feed/" rel="self" type="application/rss+xml" />
	<link>http://www.eliezer.com.br</link>
	<description>Desenvolvimento de software, linguagens de programação, artigos, utils, scrum, testes, linux e tecnologia de um modo geral</description>
	<lastBuildDate>Thu, 08 Apr 2010 12:42:03 +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>Lendo arquivo de propriedades com Shell Script &#8211; IFS</title>
		<link>http://www.eliezer.com.br/post/lendo-arquivo-de-propriedades-com-shell-script-ifs/</link>
		<comments>http://www.eliezer.com.br/post/lendo-arquivo-de-propriedades-com-shell-script-ifs/#comments</comments>
		<pubDate>Mon, 21 Sep 2009 17:30:42 +0000</pubDate>
		<dc:creator>eliezer</dc:creator>
				<category><![CDATA[Shell Script]]></category>
		<category><![CDATA[bash]]></category>
		<category><![CDATA[boas praticas]]></category>

		<guid isPermaLink="false">http://www.eliezer.com.br/?p=87</guid>
		<description><![CDATA[Bom, pode parecer simples, mas quando a velocidade é o seu primeiro quesito, isso pode mudar um pouco. Mas espera aí, é apenas ler um arquivo e fazer um split pegar a chave e o valor. Simples assim mesmo, mas quando o assunto é shell script um simples split não é tão simples. A primera [...]]]></description>
			<content:encoded><![CDATA[<p>Bom, pode parecer simples, mas quando a velocidade é o seu primeiro quesito, isso pode mudar um pouco. Mas espera aí, é apenas ler um arquivo e fazer um split pegar a chave e o valor. Simples assim mesmo, mas quando o assunto é shell script um simples split não é tão simples.</p>
<p>A primera forma de se fazer seria fazer com um cut, veja:</p>

<div class="wp_syntax"><table><tr><td class="line_numbers"><pre>1
</pre></td><td class="code"><pre class="bash" style="font-family:monospace;"><span style="color: #7a0874; font-weight: bold;">echo</span> <span style="color: #ff0000;">&quot;chave=valor&quot;</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: #660033;">-f</span> <span style="color: #000000;">1</span></pre></td></tr></table></div>

<p>Legal, funciona, mas quando seu arquivo de properties é grande, ou que esse script rode toda hora, a velocidade do um simples cut que funciona passa a ser um problema.</p>
<p>Uma outra forma de se fazer isso é usar o IFS – Internal Field Separator para fazer isso de uma forma decente.</p>
<p>Então, vamos lá. Pimeiro meu arquivo de propriedades:</p>
<p><strong>sample.properties</strong></p>

<div class="wp_syntax"><table><tr><td class="line_numbers"><pre>1
2
</pre></td><td class="code"><pre class="properties" style="font-family:monospace;"><span style="color: #000080; font-weight:bold;">caminho</span><span style="color: #000000;">=</span><span style="color: #008000; font-weight:bold;">/tmp</span>
<span style="color: #000080; font-weight:bold;">version</span><span style="color: #000000;">=</span><span style="color: #008000; font-weight:bold;">1.0</span></pre></td></tr></table></div>

<p>Fiz um script simples para representar o problema. Função rox com IFS e função sux com CUT.</p>
<p><strong>sample.sh</strong></p>

<div class="wp_syntax"><table><tr><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"><pre class="bash" style="font-family:monospace;"><span style="color: #666666; font-style: italic;">#!/bin/bash</span>
&nbsp;
rox<span style="color: #7a0874; font-weight: bold;">&#40;</span><span style="color: #7a0874; font-weight: bold;">&#41;</span><span style="color: #7a0874; font-weight: bold;">&#123;</span>
	<span style="color: #007800;">IFS_OLD</span>=<span style="color: #007800;">$IFS</span>
	<span style="color: #007800;">IFS</span>=<span style="color: #ff0000;">&quot;=&quot;</span>
	<span style="color: #000000; font-weight: bold;">while</span> <span style="color: #c20cb9; font-weight: bold;">read</span> key value
	<span style="color: #000000; font-weight: bold;">do</span>
		<span style="color: #7a0874; font-weight: bold;">echo</span> <span style="color: #ff0000;">&quot;key = <span style="color: #007800;">$key</span>&quot;</span>
		<span style="color: #7a0874; font-weight: bold;">echo</span> <span style="color: #ff0000;">&quot;Value = <span style="color: #007800;">$value</span>&quot;</span> 
	<span style="color: #000000; font-weight: bold;">done</span> <span style="color: #000000; font-weight: bold;">&lt;</span> sample.properties 
	<span style="color: #007800;">IFS</span>=<span style="color: #007800;">$IFS_OLD</span>
<span style="color: #7a0874; font-weight: bold;">&#125;</span>
&nbsp;
sux<span style="color: #7a0874; font-weight: bold;">&#40;</span><span style="color: #7a0874; font-weight: bold;">&#41;</span><span style="color: #7a0874; font-weight: bold;">&#123;</span>
	<span style="color: #000000; font-weight: bold;">while</span> <span style="color: #c20cb9; font-weight: bold;">read</span> line
	<span style="color: #000000; font-weight: bold;">do</span>
		<span style="color: #007800;">key</span>=<span style="color: #000000; font-weight: bold;">`</span><span style="color: #7a0874; font-weight: bold;">echo</span> <span style="color: #007800;">$line</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: #660033;">-f</span> <span style="color: #000000;">1</span><span style="color: #000000; font-weight: bold;">`</span>
		<span style="color: #007800;">value</span>=<span style="color: #000000; font-weight: bold;">`</span><span style="color: #7a0874; font-weight: bold;">echo</span> <span style="color: #007800;">$line</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: #660033;">-f</span> <span style="color: #000000;">2</span><span style="color: #000000; font-weight: bold;">`</span>
		<span style="color: #7a0874; font-weight: bold;">echo</span> <span style="color: #ff0000;">&quot;key = <span style="color: #007800;">$key</span>&quot;</span>
		<span style="color: #7a0874; font-weight: bold;">echo</span> <span style="color: #ff0000;">&quot;Value = <span style="color: #007800;">$value</span>&quot;</span>
	<span style="color: #000000; font-weight: bold;">done</span> <span style="color: #000000; font-weight: bold;">&lt;</span> sample.properties 
<span style="color: #7a0874; font-weight: bold;">&#125;</span>
<span style="color: #c20cb9; font-weight: bold;">clear</span>
<span style="color: #7a0874; font-weight: bold;">echo</span> <span style="color: #ff0000;">&quot;rox--------------------------------------&quot;</span>
<span style="color: #000000; font-weight: bold;">time</span> rox
<span style="color: #7a0874; font-weight: bold;">echo</span> <span style="color: #ff0000;">&quot;sux--------------------------------------&quot;</span>
<span style="color: #000000; font-weight: bold;">time</span> sux</pre></td></tr></table></div>

<p>Veja o resultado:</p>

<div class="wp_syntax"><table><tr><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"><pre class="bash" style="font-family:monospace;">rox--------------------------------------
key = caminho
Value = <span style="color: #000000; font-weight: bold;">/</span>tmp
key = version
Value = <span style="color: #000000;">1.0</span>
&nbsp;
real	0m0.001s
user	0m0.000s
sys	0m0.000s
&nbsp;
sux--------------------------------------
key = caminho
Value = <span style="color: #000000; font-weight: bold;">/</span>tmp
key = version
Value = <span style="color: #000000;">1.0</span>
&nbsp;
real	0m0.024s
user	0m0.007s
sys	0m0.019s</pre></td></tr></table></div>

<p>Notem a velocidade do script. Não é ser xiita, mas sim fazer da forma correta. <img src='http://www.eliezer.com.br/wp-includes/images/smilies/icon_smile.gif' alt=':)' class='wp-smiley' /> </p>
]]></content:encoded>
			<wfw:commentRss>http://www.eliezer.com.br/post/lendo-arquivo-de-propriedades-com-shell-script-ifs/feed/</wfw:commentRss>
		<slash:comments>2</slash:comments>
		</item>
	</channel>
</rss>
