<?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>Level Pawa &#187; bash library</title>
	<atom:link href="https://blog.developpez.com/pawamoy/ptag/bash-library/feed" rel="self" type="application/rss+xml" />
	<link>https://blog.developpez.com/pawamoy</link>
	<description></description>
	<lastBuildDate>Wed, 12 Mar 2014 09:06:56 +0000</lastBuildDate>
	<language>fr-FR</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>https://wordpress.org/?v=4.1.42</generator>
	<item>
		<title>Bibliothèque C-like en bash</title>
		<link>https://blog.developpez.com/pawamoy/p12381/bash/bibliotheque-c-like-en-bash</link>
		<comments>https://blog.developpez.com/pawamoy/p12381/bash/bibliotheque-c-like-en-bash#comments</comments>
		<pubDate>Mon, 09 Dec 2013 21:51:58 +0000</pubDate>
		<dc:creator><![CDATA[Pawamoy]]></dc:creator>
				<category><![CDATA[Bash]]></category>
		<category><![CDATA[bash library]]></category>

		<guid isPermaLink="false">http://blog.developpez.com/pawamoy/?p=26</guid>
		<description><![CDATA[Je sais pas vous, mais moi je suis un fervent utilisateur de scripts écrits en bash, et j&#8217;ai parfois les mêmes fonctions qui reviennent dans plusieurs script indépendants. Alors je me suis dit, pourquoi ne pas écrire une fois pour toutes ces fonctions, et les inclure dans l&#8217;environnement (commande source) à chaque ouverture d&#8217;un terminal [&#8230;]]]></description>
				<content:encoded><![CDATA[<p>Je sais pas vous, mais moi je suis un fervent utilisateur de scripts écrits en bash, et j&rsquo;ai parfois les mêmes fonctions qui reviennent dans plusieurs script indépendants. Alors je me suis dit, pourquoi ne pas écrire une fois pour toutes ces fonctions, et les inclure dans l&rsquo;environnement (commande <strong>source</strong>) à chaque ouverture d&rsquo;un terminal ?</p>
<p>.bashrc:</p>
<div class="codecolorer-container bash geshi" 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">...<br />
<span style="color: #7a0874; font-weight: bold;">source</span> <span style="color: #ff0000;">&quot;mon_fichier_de_fonctions&quot;</span><br />
...</div></div>
<p>Sauf qu&rsquo;à force, on entasse pas mal de fonctions, et on a pas trop envie de polluer l&rsquo;environnement de bash, et encore moins de ralentir son exécution, n&rsquo;est-ce pas ? Alors j&rsquo;ai codé 3 fonctions toutes simples qui imite quelque peu le comportement des directives pré-processeur <strong>#include</strong>, <strong>#ifndef</strong>, et <strong>#define</strong> du C.<br />
Ainsi, en admettant que le chemin vers votre bibliothèque de fonctions soit contenu dans la variable <strong>library</strong>, ça donne :</p>
<div class="codecolorer-container bash geshi" 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">define<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><br />
&nbsp; &nbsp; <span style="color: #666666; font-style: italic;"># usage: define VAR [VALUE]</span><br />
&nbsp; &nbsp; <span style="color: #7a0874; font-weight: bold;">&#91;</span> <span style="color: #007800;">$#</span> <span style="color: #660033;">-ge</span> <span style="color: #000000;">1</span> <span style="color: #7a0874; font-weight: bold;">&#93;</span> <span style="color: #000000; font-weight: bold;">&amp;&amp;</span> <span style="color: #7a0874; font-weight: bold;">eval</span> <span style="color: #007800;">$1</span>=<span style="color: #800000;">${2:-def}</span> <span style="color: #000000; font-weight: bold;">||</span> <span style="color: #7a0874; font-weight: bold;">return</span> <span style="color: #000000;">1</span><br />
<span style="color: #7a0874; font-weight: bold;">&#125;</span><br />
<br />
ndef<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><br />
&nbsp; &nbsp; <span style="color: #666666; font-style: italic;"># usage: if ndef VAR; then ...; fi</span><br />
&nbsp; &nbsp; <span style="color: #7a0874; font-weight: bold;">&#91;</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;">1</span> <span style="color: #000000; font-weight: bold;">&amp;&amp;</span> <span style="color: #660033;">-z</span> <span style="color: #ff0000;">&quot;<span style="color: #007800;">$(eval echo \$$1)</span>&quot;</span> <span style="color: #7a0874; font-weight: bold;">&#93;</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;">return</span> <span style="color: #000000;">1</span><br />
<span style="color: #7a0874; font-weight: bold;">&#125;</span><br />
<br />
include<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><br />
&nbsp; &nbsp; <span style="color: #666666; font-style: italic;"># usage: include FILE_IN_LIBRARY</span><br />
&nbsp; &nbsp; <span style="color: #7a0874; font-weight: bold;">&#91;</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;">1</span> <span style="color: #000000; font-weight: bold;">&amp;&amp;</span> <span style="color: #660033;">-f</span> <span style="color: #ff0000;">&quot;<span style="color: #007800;">$library</span>/$1&quot;</span> <span style="color: #7a0874; font-weight: bold;">&#93;</span><span style="color: #7a0874; font-weight: bold;">&#93;</span> <span style="color: #000000; font-weight: bold;">&amp;&amp;</span> . <span style="color: #ff0000;">&quot;<span style="color: #007800;">$library</span>/$1&quot;</span><br />
<span style="color: #7a0874; font-weight: bold;">&#125;</span><br />
<br />
<span style="color: #7a0874; font-weight: bold;">export</span> <span style="color: #660033;">-f</span> define<br />
<span style="color: #7a0874; font-weight: bold;">export</span> <span style="color: #660033;">-f</span> ndef<br />
<span style="color: #7a0874; font-weight: bold;">export</span> <span style="color: #660033;">-f</span> include</div></div>
<p>Vous n&rsquo;avez plus qu&rsquo;à rajouter 2 lignes dans .bashrc du genre :</p>
<div class="codecolorer-container bash geshi" 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: #7a0874; font-weight: bold;">export</span> <span style="color: #007800;">library</span>=<span style="color: #ff0000;">&quot;ma_bib&quot;</span><br />
<span style="color: #7a0874; font-weight: bold;">&#91;</span> <span style="color: #660033;">-f</span> <span style="color: #ff0000;">&quot;<span style="color: #007800;">$library</span>/include&quot;</span> <span style="color: #7a0874; font-weight: bold;">&#93;</span> <span style="color: #000000; font-weight: bold;">&amp;&amp;</span> <span style="color: #7a0874; font-weight: bold;">source</span> <span style="color: #ff0000;">&quot;<span style="color: #007800;">$library</span>/include&quot;</span></div></div>
<p>&#8230; le fichier <strong>include</strong> contenant bien sûr ces trois fonctions (vous pouvez aussi mettre les 3 fonctions dans .bashrc, à vous de voir).</p>
<p>Exemple d&rsquo;utilisation dans un fichier de votre bibliothèque :</p>
<p>Fichier <strong>blabla</strong> situé dans <em>$library/io</em></p>
<div class="codecolorer-container bash geshi" 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: #000000; font-weight: bold;">if</span> ndef __FONCTION; <span style="color: #000000; font-weight: bold;">then</span><br />
define __FONCTION<br />
<br />
fonction_blabla<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><br />
&nbsp; &nbsp; <span style="color: #7a0874; font-weight: bold;">echo</span> <span style="color: #ff0000;">&quot;blabla&quot;</span><br />
<span style="color: #7a0874; font-weight: bold;">&#125;</span><br />
<br />
<span style="color: #000000; font-weight: bold;">fi</span> <span style="color: #666666; font-style: italic;"># __FONCTION</span></div></div>
<p>Fichier <strong>is_int</strong> situé dans <em>$library/math</em></p>
<div class="codecolorer-container bash geshi" 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: #000000; font-weight: bold;">if</span> ndef is_int; <span style="color: #000000; font-weight: bold;">then</span><br />
define is_int is_integer<br />
<br />
is_integer<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><br />
&nbsp; &nbsp; <span style="color: #7a0874; font-weight: bold;">printf</span> <span style="color: #ff0000;">&quot;%d&quot;</span> <span style="color: #007800;">$1</span> <span style="color: #000000;">2</span><span style="color: #000000; font-weight: bold;">&gt;/</span>dev<span style="color: #000000; font-weight: bold;">/</span>null <span style="color: #000000; font-weight: bold;">&gt;&amp;</span><span style="color: #000000;">2</span><br />
&nbsp; &nbsp; <span style="color: #7a0874; font-weight: bold;">return</span> <span style="color: #007800;">$?</span><br />
<span style="color: #7a0874; font-weight: bold;">&#125;</span><br />
<br />
<span style="color: #000000; font-weight: bold;">fi</span> <span style="color: #666666; font-style: italic;"># is_int</span></div></div>
<p>Exemple d&rsquo;utilisation dans un script :</p>
<div class="codecolorer-container bash geshi" 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: #666666; font-style: italic;">#!/bin/bash</span><br />
include io<span style="color: #000000; font-weight: bold;">/</span>blabla<br />
include math<span style="color: #000000; font-weight: bold;">/</span>is_int<br />
<br />
<span style="color: #000000; font-weight: bold;">if</span> <span style="color: #007800;">$is_int</span> $<span style="color: #7a0874; font-weight: bold;">&#40;</span>fonction_blabla<span style="color: #7a0874; font-weight: bold;">&#41;</span>; <span style="color: #000000; font-weight: bold;">then</span><br />
&nbsp; &nbsp; <span style="color: #7a0874; font-weight: bold;">echo</span> <span style="color: #ff0000;">&quot;Ca m'étonnerait&quot;</span><br />
<span style="color: #000000; font-weight: bold;">else</span><br />
&nbsp; &nbsp; <span style="color: #7a0874; font-weight: bold;">echo</span> <span style="color: #ff0000;">&quot;Y'a plus de chances ici&quot;</span><br />
<span style="color: #000000; font-weight: bold;">fi</span></div></div>
<p>Petite précision au cas où : il est bien évidemment possible d&rsquo;imbriquer les inclusions entre fichiers !</p>
]]></content:encoded>
			<wfw:commentRss></wfw:commentRss>
		<slash:comments>2</slash:comments>
		</item>
	</channel>
</rss>
