<?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>Fabio Falcinelli &#187; UNIX</title>
	<atom:link href="http://www.fabiofalcinelli.it/tag/unix/feed/" rel="self" type="application/rss+xml" />
	<link>http://www.fabiofalcinelli.it</link>
	<description>Interessi e appunti</description>
	<lastBuildDate>Fri, 03 Sep 2010 18:42:13 +0000</lastBuildDate>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.org/?v=3.0.1</generator>
		<item>
		<title>Unix Guide</title>
		<link>http://www.fabiofalcinelli.it/2009/04/unix-guide/</link>
		<comments>http://www.fabiofalcinelli.it/2009/04/unix-guide/#comments</comments>
		<pubDate>Wed, 15 Apr 2009 17:39:59 +0000</pubDate>
		<dc:creator>Fabio</dc:creator>
				<category><![CDATA[Information Technology]]></category>
		<category><![CDATA[HP-UX]]></category>
		<category><![CDATA[IBM-AIX]]></category>
		<category><![CDATA[UNIX]]></category>

		<guid isPermaLink="false">http://www.fabiofalcinelli.it/?p=157</guid>
		<description><![CDATA[Spesso quando ci si trova a dover utilizzare più sistemi operativi diversi, benché della famiglia UNIX, capita di confondere i comandi, o di non ricordasi alcune strutture particolari di un sistema rispetto ad un altro, per esempio il nome della variabile LD_LIBRARY_PATH è diverso su AIX, sul quale è LIBPATH, mentre su HP-UX è SHLIB_PATH&#8230; [...]]]></description>
			<content:encoded><![CDATA[<p>Spesso quando ci si trova a dover utilizzare più sistemi operativi diversi, benché della famiglia UNIX, capita di confondere i comandi, o di non ricordasi alcune strutture particolari di un sistema rispetto ad un altro, per esempio il nome della variabile LD_LIBRARY_PATH è diverso su AIX, sul quale è LIBPATH, mentre su HP-UX è SHLIB_PATH&#8230;</p>
<p>Molto spesso <a href="http://unixguide.net" target="_blank">questo sito (http://unixguide.net)</a> mi è utile per trovare il comando che non ricordo mentre uso il sistema. Sebbene qua e là ci sia qualche inesattezza e qualcosa che è stato aggiornato nel tempo, trovo questa tabella molto utile e la consiglio a tutti coloro che ogni tanto hanno il vuoto di memoria come me :)</p>
<p>Di seguito il <a href="http://unixguide.net/unixguide.pdf" target="_blank">link diretto alla versione PDF</a></p>
]]></content:encoded>
			<wfw:commentRss>http://www.fabiofalcinelli.it/2009/04/unix-guide/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Aggiungere il timestamp ad ogni riga in output</title>
		<link>http://www.fabiofalcinelli.it/2009/04/aggiungere-il-timestamp-ad-ogni-riga-in-output/</link>
		<comments>http://www.fabiofalcinelli.it/2009/04/aggiungere-il-timestamp-ad-ogni-riga-in-output/#comments</comments>
		<pubDate>Fri, 10 Apr 2009 17:21:41 +0000</pubDate>
		<dc:creator>Fabio</dc:creator>
				<category><![CDATA[Development]]></category>
		<category><![CDATA[Information Technology]]></category>
		<category><![CDATA[Awk]]></category>
		<category><![CDATA[Bash]]></category>
		<category><![CDATA[SVN]]></category>
		<category><![CDATA[UNIX]]></category>

		<guid isPermaLink="false">http://www.fabiofalcinelli.it/?p=150</guid>
		<description><![CDATA[Eccomi di ritorno! È passato un po&#8217; di tempo dall&#8217;ultimo post, un po&#8217; per impegni vari, un po&#8217; per pigrizia ed un po&#8217; per i tempi di attivazione del dominio (fabiofalcinelli.it). Come va? Come state? :) Vi descrivo di cosa avevo bisogno e di come ho risolto. Supponiamo di avere un programma che generi output, [...]]]></description>
			<content:encoded><![CDATA[<p>Eccomi di ritorno! È passato un po&#8217; di tempo dall&#8217;ultimo post, un po&#8217; per impegni vari, un po&#8217; per pigrizia ed un po&#8217; per i tempi di attivazione del dominio (fabiofalcinelli.it). Come va? Come state? :)</p>
<p>Vi descrivo di cosa avevo bisogno e di come ho risolto. Supponiamo di avere un programma che generi output, ma che questo output sia sprovvisto di timestamp. Se il programma deve essere eseguito ad intervalli regolari diventa un po&#8217; difficile capire cosa sta succedendo analizzandone l&#8217;output che ne è stato eventualmente redirezionato su un file.</p>
<p>Se invece di redirezionarlo direttamente al file, lo diamo prima in pasto ad <em>awk</em> sarà però possibile aggiungere, per ogni riga, il timestamp di generazione della riga stessa. Ecco di seguito come, utilizzando il tool <em>svnsync</em> come esempio:</p>
<div class="codecolorer-container bash twitlight" style="overflow:auto;white-space:nowrap;border:1px solid #9F9F9F;width:435px;"><div class="bash codecolorer" style="padding:5px;font:normal 12px/1.4em Monaco, Lucida Console, monospace;white-space:nowrap"><span style="color: #c20cb9; font-weight: bold;">svnsync</span> synchronize <span style="color: #c20cb9; font-weight: bold;">svn</span>:<span style="color: #000000; font-weight: bold;">//</span>127.0.0.1:<span style="color: #000000;">8081</span> <span style="color: #000000; font-weight: bold;">|</span> <span style="color: #c20cb9; font-weight: bold;">nawk</span> <span style="color: #ff0000;">'{ <br />
&quot;date \&quot;+%a %b %d %Y %H:%M:%S -\&quot;&quot; | <br />
getline systemDate; <br />
close (&quot;date \&quot;+%a %b %d %Y %H:%M:%S -\&quot;&quot;) ; <br />
print systemDate, $O;<br />
}'</span> <span style="color: #000000; font-weight: bold;">&gt;&gt;</span> svnsync.log</div></div>
<p>In realtà ho utilizzato <em>nawk</em> la cui implementazione supporta alcune funzionalità in più. Non ci dovrebbero essere problemi usando la stessa procedura con <em>gawk</em>.</p>
<p>Come al solito, spero che possa essere d&#8217;aiuto a qualcuno facendogli risparmiare qualche minuto del suo tempo :)</p>
]]></content:encoded>
			<wfw:commentRss>http://www.fabiofalcinelli.it/2009/04/aggiungere-il-timestamp-ad-ogni-riga-in-output/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Java &#8211; Esecuzione comandi su CLI parte II</title>
		<link>http://www.fabiofalcinelli.it/2009/02/java-esecuzione-comandi-su-cli-part-ii/</link>
		<comments>http://www.fabiofalcinelli.it/2009/02/java-esecuzione-comandi-su-cli-part-ii/#comments</comments>
		<pubDate>Wed, 04 Feb 2009 20:24:08 +0000</pubDate>
		<dc:creator>Fabio</dc:creator>
				<category><![CDATA[Development]]></category>
		<category><![CDATA[Information Technology]]></category>
		<category><![CDATA[Deadlock]]></category>
		<category><![CDATA[Java]]></category>
		<category><![CDATA[UNIX]]></category>

		<guid isPermaLink="false">http://fabiofalcinelli.netsons.org/?p=132</guid>
		<description><![CDATA[Se vi ricordate di un pezzo di codice scritto da me e che funzionava particolarmente bene per invocare comandi su UNIX&#8230; Beh&#8230; Scordatevelo! O meglio, dovrete fare qualche piccola modifica. Il codice in questione sembra, infatti, causare un piccolo problema chiamato deadlock. La cosa non si presenta sempre, ma dipende fortemente dalla quantità di output [...]]]></description>
			<content:encoded><![CDATA[<p style="text-align: justify;">Se vi ricordate di un <a title="Java - Esecuzione comandi su CLI" href="http://fabiofalcinelli.netsons.org/2009/01/java-esecuzione-comandi-su-cli/" target="_blank">pezzo di codice scritto da me</a> e che funzionava particolarmente bene per invocare comandi su UNIX&#8230; Beh&#8230; Scordatevelo! O meglio, dovrete fare qualche piccola modifica.</p>
<p style="text-align: justify;">Il codice in questione sembra, infatti, causare un piccolo problema chiamato <em>deadlock</em>.</p>
<p style="text-align: justify;">La cosa non si presenta sempre, ma dipende fortemente dalla quantità di output che genera il comando e da come il sistema operativo gestisce il buffering della system call &#8220;write&#8221;. Lanciando <em>truss</em> da dentro al codice java, infatti, notavo che l&#8217;esecuzione delle system call si bloccava in attesa:</p>
<div class="codecolorer-container bash twitlight" style="overflow:auto;white-space:nowrap;border:1px solid #9F9F9F;width:435px;"><div class="bash codecolorer" style="padding:5px;font:normal 12px/1.4em Monaco, Lucida Console, monospace;white-space:nowrap">ioctl<span style="color: #7a0874; font-weight: bold;">&#40;</span><span style="color: #000000;">1</span>, TCGETA, 0xFFBEDF34<span style="color: #7a0874; font-weight: bold;">&#41;</span>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; Err<span style="color: #666666; font-style: italic;">#22 EINVAL</span><br />
fstat64<span style="color: #7a0874; font-weight: bold;">&#40;</span><span style="color: #000000;">1</span>, 0xFFBEDFA8<span style="color: #7a0874; font-weight: bold;">&#41;</span>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; = <span style="color: #000000;">0</span><br />
brk<span style="color: #7a0874; font-weight: bold;">&#40;</span>0x00068A70<span style="color: #7a0874; font-weight: bold;">&#41;</span> &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; = <span style="color: #000000;">0</span><br />
brk<span style="color: #7a0874; font-weight: bold;">&#40;</span>0x0006AA70<span style="color: #7a0874; font-weight: bold;">&#41;</span> &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; = <span style="color: #000000;">0</span><br />
<span style="color: #c20cb9; font-weight: bold;">write</span><span style="color: #7a0874; font-weight: bold;">&#40;</span><span style="color: #000000;">1</span>, <span style="color: #ff0000;">&quot; D A T A &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;&quot;</span>.., <span style="color: #000000;">5120</span><span style="color: #7a0874; font-weight: bold;">&#41;</span>&nbsp; &nbsp; = <span style="color: #000000;">5120</span><br />
<span style="color: #c20cb9; font-weight: bold;">write</span><span style="color: #7a0874; font-weight: bold;">&#40;</span><span style="color: #000000;">1</span>, <span style="color: #ff0000;">&quot; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; * - &gt; *&quot;</span>.., <span style="color: #000000;">5120</span><span style="color: #7a0874; font-weight: bold;">&#41;</span>&nbsp; &nbsp; = <span style="color: #000000;">5120</span><br />
llseek<span style="color: #7a0874; font-weight: bold;">&#40;</span><span style="color: #000000;">0</span>, <span style="color: #000000;">0</span>, SEEK_CUR<span style="color: #7a0874; font-weight: bold;">&#41;</span>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; Err<span style="color: #666666; font-style: italic;">#29 ESPIPE</span><br />
<span style="color: #c20cb9; font-weight: bold;">write</span><span style="color: #7a0874; font-weight: bold;">&#40;</span><span style="color: #000000;">1</span>, <span style="color: #ff0000;">&quot; * - &gt; * * * * * &nbsp; &nbsp; &nbsp; &nbsp;&quot;</span>.., <span style="color: #000000;">635</span><span style="color: #7a0874; font-weight: bold;">&#41;</span> <span style="color: #7a0874; font-weight: bold;">&#40;</span>sleeping...<span style="color: #7a0874; font-weight: bold;">&#41;</span></div></div>
<p style="text-align: justify;">Non è stato facilissimo arrivare alla causa, il metodo è infatti molto semplice, ma adesso che ho identificato il problema l&#8217;errore appare quasi evidente. Il problema risiede infatti nel canale di <em>standard error </em>letto prima dello <em>standard output</em>:</p>
<ol style="text-align: justify;">
<li>Prima di poter leggere lo standard error è necessario che il processo che esegue il comando termini, e non è detto che vi siano errori;</li>
<li>Prima di poter avere tutto l&#8217;output disponibile c&#8217;è bisogno di qualcuno che consumi i dati nel buffer, ma quel qualcuno è in attesa sullo standard error;</li>
<li>Deadlock!</li>
</ol>
<p style="text-align: justify;">Sinceramente, questa spiegazione, seppur sensata non mi convince ancora appieno e appena avrò più tempo analizzerò meglio i trace delle system call, nonostante ciò riporto di seguito la versione del metodo che risolve il problema.</p>
<div class="codecolorer-container java twitlight" style="overflow:auto;white-space:nowrap;border:1px solid #9F9F9F;width:435px;height:300px;"><div class="java codecolorer" style="padding:5px;font:normal 12px/1.4em Monaco, Lucida Console, monospace;white-space:nowrap"><span style="color: #000000; font-weight: bold;">public</span> <span style="color: #000000; font-weight: bold;">static</span> <span style="color: #003399;">String</span> execCommand<span style="color: #009900;">&#40;</span><span style="color: #003399;">String</span> cmd<span style="color: #009900;">&#41;</span> <span style="color: #000000; font-weight: bold;">throws</span> <span style="color: #003399;">IOException</span><span style="color: #009900;">&#123;</span><br />
&nbsp; &nbsp;<span style="color: #003399;">String</span> result <span style="color: #339933;">=</span> <span style="color: #000066; font-weight: bold;">null</span><span style="color: #339933;">;</span><br />
&nbsp; &nbsp;<span style="color: #003399;">String</span> temp <span style="color: #339933;">=</span> <span style="color: #000066; font-weight: bold;">null</span><span style="color: #339933;">;</span><br />
&nbsp; &nbsp;<span style="color: #003399;">BufferedReader</span> stdout <span style="color: #339933;">=</span> <span style="color: #000066; font-weight: bold;">null</span><span style="color: #339933;">;</span><br />
&nbsp; &nbsp;<span style="color: #003399;">BufferedReader</span> stderr <span style="color: #339933;">=</span> <span style="color: #000066; font-weight: bold;">null</span><span style="color: #339933;">;</span><br />
<br />
&nbsp; &nbsp;<span style="color: #003399;">Process</span> proc <span style="color: #339933;">=</span> <span style="color: #003399;">Runtime</span>.<span style="color: #006633;">getRuntime</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span>.<span style="color: #006633;">exec</span><span style="color: #009900;">&#40;</span>cmd<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><br />
<br />
&nbsp; &nbsp;<span style="color: #666666; font-style: italic;">/*consume the output*/</span><br />
&nbsp; &nbsp;stdout <span style="color: #339933;">=</span> <span style="color: #000000; font-weight: bold;">new</span> <span style="color: #003399;">BufferedReader</span><span style="color: #009900;">&#40;</span><span style="color: #000000; font-weight: bold;">new</span> <span style="color: #003399;">InputStreamReader</span><span style="color: #009900;">&#40;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;proc.<span style="color: #006633;">getInputStream</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><br />
&nbsp; &nbsp;temp <span style="color: #339933;">=</span> stdout.<span style="color: #006633;">readLine</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><br />
<br />
&nbsp; &nbsp;<span style="color: #000000; font-weight: bold;">while</span><span style="color: #009900;">&#40;</span>temp<span style="color: #339933;">!=</span><span style="color: #000066; font-weight: bold;">null</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#123;</span><br />
&nbsp; &nbsp; &nbsp; <span style="color: #000000; font-weight: bold;">if</span><span style="color: #009900;">&#40;</span>result<span style="color: #339933;">==</span><span style="color: #000066; font-weight: bold;">null</span><span style="color: #009900;">&#41;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp;result <span style="color: #339933;">=</span> temp<span style="color: #339933;">+</span><span style="color: #0000ff;">&quot;<span style="color: #000099; font-weight: bold;">\n</span>&quot;</span><span style="color: #339933;">;</span><br />
&nbsp; &nbsp; &nbsp; <span style="color: #000000; font-weight: bold;">else</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp;result <span style="color: #339933;">+=</span> temp<span style="color: #339933;">+</span><span style="color: #0000ff;">&quot;<span style="color: #000099; font-weight: bold;">\n</span>&quot;</span><span style="color: #339933;">;</span><br />
&nbsp; &nbsp; &nbsp; temp <span style="color: #339933;">=</span> stdout.<span style="color: #006633;">readLine</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><br />
&nbsp; &nbsp;<span style="color: #009900;">&#125;</span><br />
<br />
&nbsp; &nbsp;stdout.<span style="color: #006633;">close</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><br />
<br />
&nbsp; &nbsp;<span style="color: #666666; font-style: italic;">/*check if errors occurred*/</span><br />
&nbsp; &nbsp;stderr <span style="color: #339933;">=</span> <span style="color: #000000; font-weight: bold;">new</span> <span style="color: #003399;">BufferedReader</span><span style="color: #009900;">&#40;</span><span style="color: #000000; font-weight: bold;">new</span> <span style="color: #003399;">InputStreamReader</span><span style="color: #009900;">&#40;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; proc.<span style="color: #006633;">getErrorStream</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><br />
&nbsp; &nbsp;temp <span style="color: #339933;">=</span> stderr.<span style="color: #006633;">readLine</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><br />
<br />
&nbsp; &nbsp;<span style="color: #000000; font-weight: bold;">if</span><span style="color: #009900;">&#40;</span>temp<span style="color: #339933;">!=</span><span style="color: #000066; font-weight: bold;">null</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#123;</span><br />
&nbsp; &nbsp; &nbsp; result <span style="color: #339933;">=</span> temp<span style="color: #339933;">+</span><span style="color: #0000ff;">&quot;<span style="color: #000099; font-weight: bold;">\n</span>&quot;</span><span style="color: #339933;">;</span><br />
&nbsp; &nbsp; &nbsp; temp <span style="color: #339933;">=</span> stderr.<span style="color: #006633;">readLine</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><br />
&nbsp; &nbsp; &nbsp; <span style="color: #000000; font-weight: bold;">while</span><span style="color: #009900;">&#40;</span>temp<span style="color: #339933;">!=</span><span style="color: #000066; font-weight: bold;">null</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#123;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp;result <span style="color: #339933;">+=</span> temp<span style="color: #339933;">+</span><span style="color: #0000ff;">&quot;<span style="color: #000099; font-weight: bold;">\n</span>&quot;</span><span style="color: #339933;">;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp;temp <span style="color: #339933;">=</span> stderr.<span style="color: #006633;">readLine</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><br />
&nbsp; &nbsp; &nbsp; <span style="color: #009900;">&#125;</span><br />
<br />
&nbsp; &nbsp; &nbsp; stderr.<span style="color: #006633;">close</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><br />
&nbsp; &nbsp; &nbsp; <span style="color: #000000; font-weight: bold;">throw</span> <span style="color: #000000; font-weight: bold;">new</span> <span style="color: #003399;">IOException</span><span style="color: #009900;">&#40;</span>result<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><br />
&nbsp; &nbsp;<span style="color: #009900;">&#125;</span><br />
<br />
&nbsp; &nbsp;stderr.<span style="color: #006633;">close</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><br />
&nbsp; &nbsp;<span style="color: #666666; font-style: italic;">/*if no exceptions occurred then proceed to return the output*/</span><br />
&nbsp; &nbsp;<span style="color: #000000; font-weight: bold;">return</span> result<span style="color: #339933;">;</span><br />
<span style="color: #009900;">&#125;</span></div></div>
<p style="text-align: justify;">Questo è quanto, il metodo main d&#8217;esempio del post precedente però è ancora valido :)</p>
]]></content:encoded>
			<wfw:commentRss>http://www.fabiofalcinelli.it/2009/02/java-esecuzione-comandi-su-cli-part-ii/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Java &#8211; Esecuzione comandi su CLI</title>
		<link>http://www.fabiofalcinelli.it/2009/01/java-esecuzione-comandi-su-cli/</link>
		<comments>http://www.fabiofalcinelli.it/2009/01/java-esecuzione-comandi-su-cli/#comments</comments>
		<pubDate>Wed, 28 Jan 2009 20:50:49 +0000</pubDate>
		<dc:creator>Fabio</dc:creator>
				<category><![CDATA[Development]]></category>
		<category><![CDATA[Information Technology]]></category>
		<category><![CDATA[Java]]></category>
		<category><![CDATA[UNIX]]></category>

		<guid isPermaLink="false">http://fabiofalcinelli.netsons.org/?p=85</guid>
		<description><![CDATA[Prima o poi può essere utile astrarre un po&#8217; meno dalla macchina e tornare a interfacciarsi con il Sistema Operativo sottostante. Quello che può tornare utile è quindi un metodo per eseguire comandi di sistema dalla VirtualMachine Java. Il seguente è un metodo che ho scritto in fretta e furia, ma che su sistemi UNIX [...]]]></description>
			<content:encoded><![CDATA[<p style="text-align: justify;">Prima o poi può essere utile astrarre un po&#8217; meno dalla macchina e tornare a interfacciarsi con il Sistema Operativo sottostante. Quello che può tornare utile è quindi un metodo per eseguire comandi di sistema dalla VirtualMachine Java. Il seguente è un metodo che ho scritto in fretta e furia, ma che su sistemi UNIX sembra funzionare molto molto bene:</p>
<div class="codecolorer-container java twitlight" style="overflow:auto;white-space:nowrap;border:1px solid #9F9F9F;width:435px;height:300px;"><div class="java codecolorer" style="padding:5px;font:normal 12px/1.4em Monaco, Lucida Console, monospace;white-space:nowrap"><span style="color: #000000; font-weight: bold;">public</span> <span style="color: #000000; font-weight: bold;">static</span> <span style="color: #003399;">String</span> execCommand<span style="color: #009900;">&#40;</span><span style="color: #003399;">String</span> cmd<span style="color: #009900;">&#41;</span> <span style="color: #000000; font-weight: bold;">throws</span> <span style="color: #003399;">IOException</span><span style="color: #009900;">&#123;</span><br />
&nbsp; &nbsp;<span style="color: #003399;">String</span> result <span style="color: #339933;">=</span> <span style="color: #000066; font-weight: bold;">null</span><span style="color: #339933;">;</span><br />
&nbsp; &nbsp;<span style="color: #003399;">BufferedReader</span> stdout <span style="color: #339933;">=</span> <span style="color: #000066; font-weight: bold;">null</span><span style="color: #339933;">;</span><br />
&nbsp; &nbsp;<span style="color: #003399;">BufferedReader</span> stderr <span style="color: #339933;">=</span> <span style="color: #000066; font-weight: bold;">null</span><span style="color: #339933;">;</span><br />
&nbsp; &nbsp;<span style="color: #003399;">Runtime</span> runtime <span style="color: #339933;">=</span> <span style="color: #000066; font-weight: bold;">null</span><span style="color: #339933;">;</span><br />
&nbsp; &nbsp;<span style="color: #003399;">Process</span> proc <span style="color: #339933;">=</span> <span style="color: #000066; font-weight: bold;">null</span><span style="color: #339933;">;</span><br />
<br />
&nbsp; &nbsp;runtime <span style="color: #339933;">=</span> <span style="color: #003399;">Runtime</span>.<span style="color: #006633;">getRuntime</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><br />
&nbsp; &nbsp;proc <span style="color: #339933;">=</span> runtime.<span style="color: #006633;">exec</span><span style="color: #009900;">&#40;</span>cmd<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><br />
&nbsp; &nbsp;stderr <span style="color: #339933;">=</span> <span style="color: #000000; font-weight: bold;">new</span> <span style="color: #003399;">BufferedReader</span><span style="color: #009900;">&#40;</span><span style="color: #000000; font-weight: bold;">new</span> <span style="color: #003399;">InputStreamReader</span><span style="color: #009900;">&#40;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; proc.<span style="color: #006633;">getErrorStream</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><br />
&nbsp; &nbsp;<span style="color: #003399;">String</span> temp <span style="color: #339933;">=</span> stderr.<span style="color: #006633;">readLine</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><br />
<br />
&nbsp; &nbsp;<span style="color: #666666; font-style: italic;">/*check if errors occurred*/</span><br />
&nbsp; &nbsp;<span style="color: #000000; font-weight: bold;">if</span><span style="color: #009900;">&#40;</span>temp<span style="color: #339933;">!=</span><span style="color: #000066; font-weight: bold;">null</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#123;</span><br />
&nbsp; &nbsp; &nbsp; <span style="color: #000000; font-weight: bold;">do</span><span style="color: #009900;">&#123;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp;<span style="color: #000000; font-weight: bold;">if</span><span style="color: #009900;">&#40;</span>result<span style="color: #339933;">==</span><span style="color: #000066; font-weight: bold;">null</span><span style="color: #009900;">&#41;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; result <span style="color: #339933;">=</span> temp<span style="color: #339933;">+</span><span style="color: #0000ff;">&quot;<span style="color: #000099; font-weight: bold;">\n</span>&quot;</span><span style="color: #339933;">;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp;<span style="color: #000000; font-weight: bold;">else</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; result <span style="color: #339933;">+=</span> temp<span style="color: #339933;">+</span><span style="color: #0000ff;">&quot;<span style="color: #000099; font-weight: bold;">\n</span>&quot;</span><span style="color: #339933;">;</span><br />
<br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp;temp <span style="color: #339933;">=</span> stderr.<span style="color: #006633;">readLine</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><br />
&nbsp; &nbsp; &nbsp; <span style="color: #009900;">&#125;</span><span style="color: #000000; font-weight: bold;">while</span><span style="color: #009900;">&#40;</span>temp<span style="color: #339933;">!=</span><span style="color: #000066; font-weight: bold;">null</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><br />
<br />
&nbsp; &nbsp; &nbsp; stderr.<span style="color: #006633;">close</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><br />
&nbsp; &nbsp; &nbsp; <span style="color: #000000; font-weight: bold;">throw</span> <span style="color: #000000; font-weight: bold;">new</span> <span style="color: #003399;">IOException</span><span style="color: #009900;">&#40;</span>result<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><br />
&nbsp; &nbsp;<span style="color: #009900;">&#125;</span><br />
<br />
&nbsp; &nbsp;<span style="color: #666666; font-style: italic;">/*if no exceptions occurred then proceed to return the output*/</span><br />
&nbsp; &nbsp;stdout <span style="color: #339933;">=</span> <span style="color: #000000; font-weight: bold;">new</span> <span style="color: #003399;">BufferedReader</span><span style="color: #009900;">&#40;</span><span style="color: #000000; font-weight: bold;">new</span> <span style="color: #003399;">InputStreamReader</span><span style="color: #009900;">&#40;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;proc.<span style="color: #006633;">getInputStream</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><br />
&nbsp; &nbsp;temp <span style="color: #339933;">=</span> stdout.<span style="color: #006633;">readLine</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><br />
<br />
&nbsp; &nbsp;<span style="color: #000000; font-weight: bold;">while</span><span style="color: #009900;">&#40;</span>temp<span style="color: #339933;">!=</span><span style="color: #000066; font-weight: bold;">null</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#123;</span><br />
&nbsp; &nbsp; &nbsp; <span style="color: #000000; font-weight: bold;">if</span><span style="color: #009900;">&#40;</span>result<span style="color: #339933;">==</span><span style="color: #000066; font-weight: bold;">null</span><span style="color: #009900;">&#41;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp;result <span style="color: #339933;">=</span> temp<span style="color: #339933;">+</span><span style="color: #0000ff;">&quot;<span style="color: #000099; font-weight: bold;">\n</span>&quot;</span><span style="color: #339933;">;</span><br />
&nbsp; &nbsp; &nbsp; <span style="color: #000000; font-weight: bold;">else</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp;result <span style="color: #339933;">+=</span> temp<span style="color: #339933;">+</span><span style="color: #0000ff;">&quot;<span style="color: #000099; font-weight: bold;">\n</span>&quot;</span><span style="color: #339933;">;</span><br />
&nbsp; &nbsp; &nbsp; temp <span style="color: #339933;">=</span> stdout.<span style="color: #006633;">readLine</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><br />
&nbsp; <span style="color: #009900;">&#125;</span><br />
<br />
&nbsp; stdout.<span style="color: #006633;">close</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><br />
&nbsp; <span style="color: #000000; font-weight: bold;">return</span> result<span style="color: #339933;">;</span><br />
<span style="color: #009900;">&#125;</span></div></div>
<p style="text-align: justify;">L&#8217;idea è semplice: se il comando fallisce, restituisco l&#8217;esito del comando (lo standard error) come un&#8217;eccezione di I/O, altrimenti il metodo torna l&#8217;esito del comando (lo standard output).</p>
<p style="text-align: justify;">A questo punto possiamo, per esempio, scrivere il seguente main, un po&#8217; inutile, ma rende l&#8217;idea</p>
<div class="codecolorer-container java twitlight" style="overflow:auto;white-space:nowrap;border:1px solid #9F9F9F;width:435px;"><div class="java codecolorer" style="padding:5px;font:normal 12px/1.4em Monaco, Lucida Console, monospace;white-space:nowrap"><span style="color: #000000; font-weight: bold;">public</span> <span style="color: #000000; font-weight: bold;">static</span> <span style="color: #000066; font-weight: bold;">void</span> main<span style="color: #009900;">&#40;</span><span style="color: #003399;">String</span> args<span style="color: #009900;">&#91;</span><span style="color: #009900;">&#93;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#123;</span><br />
&nbsp; &nbsp;<span style="color: #000000; font-weight: bold;">try</span><span style="color: #009900;">&#123;</span><br />
&nbsp; &nbsp; &nbsp; <span style="color: #003399;">String</span> result <span style="color: #339933;">=</span> execCommand<span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;ls -lrt&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><br />
&nbsp; &nbsp; &nbsp; <span style="color: #003399;">System</span>.<span style="color: #006633;">out</span>.<span style="color: #006633;">println</span><span style="color: #009900;">&#40;</span>result<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><br />
&nbsp; &nbsp;<span style="color: #009900;">&#125;</span><span style="color: #000000; font-weight: bold;">catch</span><span style="color: #009900;">&#40;</span><span style="color: #003399;">IOException</span> e<span style="color: #009900;">&#41;</span><span style="color: #009900;">&#123;</span><br />
&nbsp; &nbsp; &nbsp; <span style="color: #003399;">System</span>.<span style="color: #006633;">err</span>.<span style="color: #006633;">println</span><span style="color: #009900;">&#40;</span>e.<span style="color: #006633;">getMessage</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><br />
&nbsp; &nbsp;<span style="color: #009900;">&#125;</span><br />
<span style="color: #009900;">&#125;</span></div></div>
<p style="text-align: justify;">Ovviamente il codice non è ottimizzato essendo la prima stesura di getto, alcune cose potevano essere evitate (i due BufferedReader per esempio), ma spero che a qualcuno questo metodo torni utile.</p>
]]></content:encoded>
			<wfw:commentRss>http://www.fabiofalcinelli.it/2009/01/java-esecuzione-comandi-su-cli/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
	</channel>
</rss>
