<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>Structures de contrôle - 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="structureShell.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="testShell.fre.html" title="Tests">
<link rel="next" href="commandeShell.fre.html" title="Commandes diverses">
<link rel="last" href="tpShell.fre.html" title="Travaux Pratiques">
</head>
<body>
<h1>Structures de contrôle - 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">Tests</a></li>
<li><a href="structureShell.fre.html" id="subcurrent">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">
<h2><a name="for"></a>
for
</h2>
<h3>
Syntaxe
</h3>
<pre>for var [in liste]
do
liste_commandes
done</pre>
<p>
La variable <em>var</em> prend successivement les valeurs de la
<em>liste</em>.<br>
Si la liste est omise, <em>var</em> prend alors les valeurs
passées en paramètres du script
(<a href="paramShell.fre.html#param"><code>$*</code></a>).
</p>
<h3>
Exemple
</h3>
<pre class="exemple">for i
do
echo $i
done</pre>
<pre class="exemple">for i in <a href="paramShell.fre.html#speciaux">`</a>ls<a href="paramShell.fre.html#speciaux">`</a>
do
cp $i /dir/$i
echo "$i copié"
done</pre>
<p>
Il ne faut pas oublier les apostrophes inversées (ou quotes inverses)
<a href="paramShell.fre.html#speciaux"><code>`</code></a> qui forcent
l'exécution du <code>ls</code>.
</p>
<hr>
<h2><a name="while"></a>
while
</h2>
<h3>
Syntaxe
</h3>
<pre>while commande
do
liste_commandes
done</pre>
<h3>
Exemple
</h3>
<pre class="exemple">while read a
do
echo $a >>resultat.txt
echo Appuyez sur Ctrl-D pour arrêter.
done</pre>
<hr>
<h2><a name="until"></a>
until
</h2>
<h3>
Syntaxe
</h3>
<pre>until commande
do
liste_commandes
done</pre>
<h3>
Exemple
</h3>
<pre class="exemple">until ! read a
do
echo $a >>resultat.txt
echo Appuyez sur Ctrl-D pour arrêter.
done</pre>
<hr>
<h2><a name="case"></a>
case
</h2>
<h3>
Syntaxe
</h3>
<pre>case para in
choix1[|choix2] ... ) liste_commandes ;;
esac</pre>
<h3>
Exemple
</h3>
<pre class="exemple">case $1 in
bonjour ) echo "Bonjour aussi." ;;
A+ | a+ ) echo "À bientôt." ;;
* ) echo "Je n'ai pas bien compris le paramètre \"$1\".";;
esac</pre>
</div>
<hr>
<address><a href="mailto:philippe.houdry@inist.fr">Philippe.Houdry@inist.fr</a></address>
</body>
</html>