<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"> <title>Redirections et Pipes - UNIX</title> <style type="text/css"> Q { font-style: italic; } TABLE { text-align: center; } </style> <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="redirPipes.eng.html"><link rel="up" href="index.fre.html" title="Tutorial UNIX / Shell"> <link rel="section" href="redirPipes.fre.html" title="Redirections"><link rel="section" href="commandesBase.fre.html" title="Base"><link rel="section" href="tpRappels.fre.html" title="TP"> <link rel="first" href="caracteristiques.fre.html" title="Caractéristiques d'UNIX"> <link rel="previous" href="X.fre.html#wm" title="X Window"> <link rel="next" href="commandesBase.fre.html" title="Base"> <link rel="last" href="tpRappels.fre.html" title="Travaux Pratiques"> </head> <body> <h1>Redirections et Pipes</h1> <div id="navcontainer"> <ul id="navlist"> <li><a href="caracteristiques.fre.html">UNIX</a> </li> <li><a>Bases</a> <ul id="subnavlist"> <li><a href="redirPipes.fre.html" id="subcurrent">Redirections</a></li> <li><a href="commandesBase.fre.html">Base</a></li> <li><a href="tpRappels.fre.html">TP</a></li> </ul> </li> <li><a href="newCommandsUser.fre.html">Commandes</a> </li> <li><a href="shellVars.fre.html">Shell</a> </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>Redirections</h2> <h3>Accès en lecture par redirection de l'entrée</h3> <center> <img src="../Tutorial/2/2/unixRdir.gif" alt="entrée et sortie standard d'une commande Unix"> </center> <h3>Entrées sorties standard</h3> <p>Toute commande Unix peut utiliser :</p> <ul> <li>l'entrée standard (stdin)</li> <li>la sortie standard (stdout)</li> </ul> <p>Valeurs implicites (pour un poste de travail) :</p> <ul> <li>stdin : le clavier</li> <li>stdout : l'écran</li> </ul> <h3>Redirection de l'entrée standard vers un fichier</h3> <center> <img src="../Tutorial/2/2/unixInputFile.gif" alt="redirection de l'entrée standard d'une commande unix"> </center> <p>Soit :</p> <ul> <li><i>aUnixCommand</i> = un programme Unix de type filtre</li> <li><i>myFile</i> = un fichier</li> </ul> <p>La commande shell suivante : <code>aUnixCommand < myFile</code></p> <p>appliquera le programme <i>aUnixCommand</i> au fichier <i>myFile</i> et affichera le résultat sur l'écran.</p> <hr> <h2>Redirections et quelques commandes Unix</h2> <ul> <li><h4>cat</h4> <p>Utilisée sans paramètres, recopie le fichier en entrée sur la sortie standard :</p> <table bgcolor="yellow" border="1" class="commands" style="margin-left:0;"> <tr><td>cat < Formation/TP1/exo.txt</td></tr> </table> <p>recopie sur l'écran le fichier "Formation/TP1/exo.txt".</p> </li> <li><h4>cat - <i>avec paramètres</i></h4> <p>La commande <code>cat</code> peut s'utiliser avec une liste de fichiers en paramètres. Ainsi :</p> <table bgcolor="yellow" border="1" class="commands" style="margin-left:0;"> <tr><td>cat Formation/TP1/exo1.txt Formation/TP1/exo2.txt</td></tr> </table> <p>"concatène" les fichiers sur stdout.</p> <p>A NOTER : avec un seul fichier les commandes :</p> <table bgcolor="yellow" border="1" class="commands" style="margin-left:0;"> <tr><td>cat Formation/TP1/exo.txt</td></tr> </table> et <table bgcolor="yellow" border="1" class="commands" style="margin-left:0;"> <tr><td>cat < Formation/TP1/exo.txt</td></tr> </table> <p>peuvent être considérées comme équivalentes.</p> </li> <li><h4>sort</h4> <p>Trie les lignes d'un fichier dans l'ordre lexicographique ascii.<br>Exemple :</p> <table bgcolor="yellow" border="1" class="commands" style="margin-left:0;"> <tr><td>sort < Formation/TP1/table.txt</td></tr> </table><br> </li> <li><h4>grep</h4> <p>Sélectionne les lignes qui contiennent une expression donnée en paramètre.<br>Syntaxe simplifiée :</p> <table bgcolor="yellow" border="1" class="commands" style="margin-left:0;"> <tr><td>grep <i>expression</i> < <i>file</i></td></tr> </table> <p>Exemple :</p> <table bgcolor="yellow" border="1" class="commands" style="margin-left:0;"> <tr><td>grep 152X < Formation/TP1/issn.txt</td></tr> </table> <br> </li> <li><h4>head</h4> <p>Syntaxe simplifiée :</p> <table bgcolor="yellow" border="1" class="commands" style="margin-left:0;"> <tr><td>head [ -<i>number</i> ] < <i>file</i></td></tr> </table> <ul> <li>sans paramètre, sélectionne les 10 premières lignes</li> <li>avec paramètre, sélectionne le nombre de lignes désignées par le paramètre</li> </ul> <p>Exemple :</p> <table bgcolor="yellow" border="1" class="commands" style="margin-left:0;"> <tr><td>head -5 < Formation/TP1/file.xml</td></tr> </table> <br> </li> </ul> <hr> <h2>Redirection de la sortie standard vers un fichier</h2> <p>La sortie standard peut à son tour être redirigée vers un fichier.<br>2 possibilités :</p> <ul> <li> <table bgcolor="yellow" border="1" class="commands" style="margin-left:0;"> <tr><td><i>commande</i> > <i>fichier</i></td></tr> </table> <p>le résultat de la commande va servir à créer ou à remplacer (par écrasement) le contenu du fichier donné.</p> </li> <li> <table bgcolor="yellow" border="1" class="commands" style="margin-left:0;"> <tr><td><i>commande</i> >> <i>fichier</i></td></tr> </table> <p>le résultat de la commande va servir à créer (la première fois) ou à rallonger le contenu du fichier donné (concaténation).</p> </li> </ul> <br> <hr> <h2>Combinaison simple des redirections</h2> <p>Entrée et sortie standard peuvent être rédirigées simultanément. <br>ainsi :</p> <table bgcolor="yellow" border="1" class="commands" style="margin-left:0;"> <tr><td>cat < fA > fB</td></tr> </table> <p>copie le contenu du fichier fA pour créer le fichier fB.</p> <br> <hr> <h2>Combinaison de fitres en cascade - le pipe</h2> <center> <img src="../Tutorial/2/2/pipe.gif" alt="pipe de deux commandes unix"> </center> <p>Le mécanisme appelé "pipe" permet d'enchaîner des commandes en affectant la sortie de l'une à l'entrée de la suivante. Sa forme générale est :</p> <table bgcolor="yellow" border="1" class="commands" style="margin-left:0;"> <tr><td>c0 < fA | c1 | c2 | ... | c<i>n</i> > fB</td></tr> </table> <p>Le fichier fA est lu et traité par la commande c0, le résultat est alors traité par c1, et ainsi de suite jusqu'à la commande c<i>n</i> pour laquelle le résultat est écrit dans le fichier fB.</p> <br> </div> <hr> <address><a href="mailto:philippe.houdry@inist.fr">Philippe.Houdry@inist.fr</a></address> </body> </html>