<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1">
<link REL="STYLESHEET" TYPE="text/css" TITLE="Style DILIB" HREF="../../dilib.css">
<style type="text/css">
.remarque, .exemple { font-size: small; }
</style>
<title>Tests - Shell - UNIX</title>
<link rel="copyright" href="http://www.inist.fr/"><link rel="author" href="http://dilib.inist.fr/membres.html"><link rel="chapter" href="caracteristiques.fre.html" title="UNIX"><link rel="chapter" href="X.fre.html" title="X Window"><link rel="chapter" href="redirPipes.fre.html" title="Rappels"><link rel="chapter" href="newCommandsUser.fre.html" title="Commandes UNIX"><link rel="chapter" href="shellVars.fre.html" title="Shell"><link rel="chapter" href="sed.fre.html" title="Sed"><link rel="chapter" href="make.fre.html" title="Make"><link rel="index" href="http://dilib.inist.fr/dilib/v04Main/IndexSite/Server/FR.resource.index.html"><link rel="top" title="Accueil de DILIB" href="http://dilib.inist.fr/dilib/v04Main/"><link rel="alternate" title="English version" href="testShell.eng.html"><link rel="up" href="index.fre.html" title="Tutorial UNIX / Shell">
<link rel="section" href="shellVars.fre.html" title="Variables d'environnement"><link rel="section" href="shellScript.fre.html" title="Scripts"><link rel="section" href="paramShell.fre.html" title="Paramètres"><link rel="section" href="testShell.fre.html" title="Tests"><link rel="section" href="structureShell.fre.html" title="Structures de contrôle"><link rel="section" href="commandeShell.fre.html" title="Commandes diverses"><link rel="section" href="tpShell.fre.html" title="Travaux Pratiques">
<link rel="first" href="caracteristiques.fre.html" title="Caractéristiques d'UNIX">
<link rel="previous" href="paramShell.fre.html" title="Paramètres">
<link rel="next" href="structureShell.fre.html" title="Structures de contrôle">
<link rel="last" href="tpShell.fre.html" title="Travaux Pratiques">
</head>
<body>
<h1>Tests - Shell - UNIX</h1>
<div id="navcontainer">
<ul id="navlist">
<li><a href="caracteristiques.fre.html">UNIX</a>
</li>
<li><a href="redirPipes.fre.html">Bases</a>
</li>
<li><a href="newCommandsUser.fre.html">Commandes</a>
</li>
<li><a>Shell</a>
<ul id="subnavlist">
<li><a href="shellVars.fre.html">Variables d'environnement</a></li>
<li><a href="shellScript.fre.html">Scripts</a></li>
<li><a href="paramShell.fre.html">Paramètres</a></li>
<li><a href="testShell.fre.html" id="subcurrent">Tests</a></li>
<li><a href="structureShell.fre.html">Structures de contrôle</a></li>
<li><a href="commandeShell.fre.html">Commandes</a></li>
<li><a href="tpShell.fre.html">TP</a></li>
</ul>
</li>
<li><a href="sed.fre.html">Sed</a>
</li>
<li><a href="make.fre.html">Make</a>
</li>
</ul>
</div>
<div class="content">
<p>
Les commandes <a href="#if"><code>if</code></a>,
<a href="structureShell.fre.html#while"><code>while</code></a>, et
<a href="structureShell.fre.html#until"><code>until</code></a> testent
le statut de la commande qu'ils ont en paramètre.
</p>
<h2><a name="if"></a>
if
</h2>
<h3>
Syntaxe
</h3>
<pre>if commande
then
liste_commandes
[elif commande
then
liste_commandes] ...
[else liste_commandes]
fi</pre>
<h3>
Exemples
</h3>
<pre class="exemple">if test -f $1
then
cat $1
else
echo "Le fichier \"$1\" n'existe pas"
fi</pre>
<hr>
<h2><a name="test"></a>
test
</h2>
<p>
<code>test</code> est une commande qui renvoie une valeur vraie
ou fausse (0 ou 1). Elle a différentes options permettant de
tester différentes conditions sur des fichiers, des valeurs
(chaînes de caractères), etc.
</p>
<p>
Sa syntaxe est <code>test expr</code> ou <code>[ expr ]</code>
(attention, il faut un espace après <code>[</code> et avant
<code>]</code>).
</p>
<table border="1" class="var">
<tbody>
<tr>
<th>expr</th>
<th>signification</th>
</tr>
<tr>
<th>-r fichier</th>
<td>vrai si le fichier existe et est accessible en lecture (r)</td>
</tr>
<tr>
<th>-w fichier</th>
<td>vrai si le fichier existe et est accessible en écriture (w)</td>
</tr>
<tr>
<th>-x fichier</th>
<td>vrai si le fichier existe et est exécutable (x)</td>
</tr>
<tr>
<th>-f fichier</th>
<td>vrai si le fichier existe et est un fichier "régulier" (<strong>f</strong>ile)</td>
</tr>
<tr>
<th>-d fichier</th>
<td>vrai si le "fichier" existe et est un répertoire (<strong>d</strong>irectory)</td>
</tr>
<tr>
<th>-s fichier</th>
<td>vrai si le fichier existe et a une taille non nulle (<strong>s</strong>ize)</td>
</tr>
<tr>
<th>c1 = c2</th>
<td>vrai si les deux expressions sont égales (des chaînes, en sh)</td>
</tr>
<tr>
<th>c1 != c2</th>
<td>vrai si les deux expressions sont différentes (des chaînes, en sh)</td>
</tr>
<tr>
<th>c1</th>
<td>vrai si c1 n'est pas la chaîne nulle (vide)</td>
</tr>
<tr>
<th>e1 -eq e2</th>
<td>vrai si les deux entiers e1 et e2 sont algébriquement égaux (<strong>eq</strong>ual)</td>
</tr>
<tr>
<th>e1 -ne e2</th>
<td>vrai si les deux entiers e1 et e2 sont algébriquement différents (<strong>n</strong>ot <strong>e</strong>qual)</td>
</tr>
<tr>
<th>e1 -gt e2</th>
<td>vrai si l'entier e1 est plus grand que l'entier e2 (<strong>g</strong>reater <strong>t</strong>han)</td>
</tr>
<tr>
<th>e1 -lt e2</th>
<td>vrai si l'entier e1 est plus petit que e2 (<strong>l</strong>ower <strong>t</strong>han)</td>
</tr>
<tr>
<th>! expr</th>
<td>négation de l'expression booléenne expr</td>
</tr>
<tr>
<th>expr1 -a expr2</th>
<td>et logique entre les deux expressions booléennes expr1 et expr2 (<strong>a</strong>nd)</td>
</tr>
<tr>
<th>expr1 -o expr2</th>
<td>ou logique entre les deux expressions booléennes expr1 et expr2 (<strong>o</strong>r)</td>
</tr>
</tbody>
</table>
<h3>
Exemples
</h3>
<p>
Quand <code>script.sh</code> est un fichier qui existe dans le
répertoire courant.
</p>
<pre class="exemple">>test -f script.sh
>echo <a href="paramShell.fre.html#speciales">$?</a>
0</pre>
<p>
Quand <code>inexistant.sh</code> est un fichier qui n'est pas
dans le répertoire courant.
</p>
<pre class="exemple">>test -f inexistant.sh
>echo <a href="paramShell.fre.html#speciales">$?</a>
1</pre>
</div>
<hr>
<address><a href="mailto:philippe.houdry@inist.fr">Philippe.Houdry@inist.fr</a></address>
</body>
</html>