Newer
Older
cours-unix-shell / UnixShell_cours2018 / redirPipes.fre.html
<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&eacute;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&egrave;s en lecture par redirection de l'entr&eacute;e</h3>
    <center>
      <img src="../Tutorial/2/2/unixRdir.gif" alt="entr&eacute;e et sortie standard d'une commande Unix">
    </center>

    <h3>Entr&eacute;es sorties standard</h3>
    <p>Toute commande Unix peut utiliser&nbsp;:</p>
    <ul>
	<li>l'entr&eacute;e standard (stdin)</li>
	<li>la sortie standard (stdout)</li>
    </ul>
    <p>Valeurs implicites (pour un poste de travail)&nbsp;:</p>
    <ul>
	<li>stdin&nbsp;: le clavier</li>
	<li>stdout&nbsp;: l'&eacute;cran</li>
    </ul>

    <h3>Redirection de l'entr&eacute;e standard vers un fichier</h3>
    <center>
      <img src="../Tutorial/2/2/unixInputFile.gif" alt="redirection de l'entr&eacute;e standard d'une commande unix">
    </center>
    <p>Soit&nbsp;:</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&nbsp;:&nbsp;<code>aUnixCommand &lt; myFile</code></p>
    <p>appliquera le programme <i>aUnixCommand</i> au fichier <i>myFile</i> et affichera le r&eacute;sultat sur l'&eacute;cran.</p>

    <hr>
    <h2>Redirections et quelques commandes Unix</h2>

    <ul>
      <li><h4>cat</h4>

	<p>Utilis&eacute;e sans param&egrave;tres, recopie le fichier en entr&eacute;e sur la sortie standard&nbsp;:</p>
	<table bgcolor="yellow" border="1" class="commands" style="margin-left:0;">
	    <tr><td>cat &lt; Formation/TP1/exo.txt</td></tr>
        </table>
        <p>recopie sur l'&eacute;cran le fichier "Formation/TP1/exo.txt".</p>
      </li>

      <li><h4>cat - <i>avec param&egrave;tres</i></h4>

        <p>La commande <code>cat</code> peut s'utiliser avec une liste de fichiers en param&egrave;tres. Ainsi&nbsp;:</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&egrave;ne" les fichiers sur stdout.</p>

        <p>A NOTER : avec un seul fichier les commandes&nbsp;:</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 &lt; Formation/TP1/exo.txt</td></tr>
        </table>
        <p>peuvent &ecirc;tre consid&eacute;r&eacute;es comme &eacute;quivalentes.</p>
      </li>

      <li><h4>sort</h4>
        <p>Trie les lignes d'un fichier dans l'ordre lexicographique ascii.<br>Exemple&nbsp;:</p>
        <table bgcolor="yellow" border="1" class="commands" style="margin-left:0;">
	<tr><td>sort &lt; Formation/TP1/table.txt</td></tr>
        </table><br>
      </li>

      <li><h4>grep</h4>
        <p>S&eacute;lectionne les lignes qui contiennent une expression donn&eacute;e en param&egrave;tre.<br>Syntaxe simplifi&eacute;e&nbsp;:</p>
        <table bgcolor="yellow" border="1" class="commands" style="margin-left:0;">
	<tr><td>grep <i>expression</i> &lt; <i>file</i></td></tr>
        </table>
        <p>Exemple&nbsp;:</p>
        <table bgcolor="yellow" border="1" class="commands" style="margin-left:0;">
	<tr><td>grep 152X &lt; Formation/TP1/issn.txt</td></tr>
        </table>
        <br>
      </li>

      <li><h4>head</h4>
        <p>Syntaxe simplifi&eacute;e&nbsp;:</p>
        <table bgcolor="yellow" border="1" class="commands" style="margin-left:0;">
	<tr><td>head [ -<i>number</i> ] &lt; <i>file</i></td></tr>
        </table>
        <ul>
           <li>sans param&egrave;tre, s&eacute;lectionne les 10 premi&egrave;res lignes</li>
           <li>avec param&egrave;tre, s&eacute;lectionne le nombre de lignes d&eacute;sign&eacute;es par le param&egrave;tre</li>
        </ul>
        <p>Exemple&nbsp;:</p>
           <table bgcolor="yellow" border="1" class="commands" style="margin-left:0;">
             <tr><td>head -5  &lt; 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 &agrave; son tour &ecirc;tre redirig&eacute;e vers un fichier.<br>2 possibilit&eacute;s&nbsp;:</p>

    <ul>
      <li>
	<table bgcolor="yellow" border="1" class="commands" style="margin-left:0;">
	  <tr><td><i>commande</i> &gt;  <i>fichier</i></td></tr>
        </table>
	<p>le r&eacute;sultat de la commande va servir &agrave; cr&eacute;er ou &agrave; remplacer (par &eacute;crasement) le contenu du fichier donn&eacute;.</p>
      </li>
      <li>
	<table bgcolor="yellow" border="1" class="commands" style="margin-left:0;">
	  <tr><td><i>commande</i> &gt;&gt;   <i>fichier</i></td></tr>
	</table>
	<p>le r&eacute;sultat de la commande va servir &agrave; cr&eacute;er (la premi&egrave;re fois) ou &agrave; rallonger le contenu du fichier donn&eacute; (concat&eacute;nation).</p>
      </li>
    </ul>
    <br>

    <hr>
    <h2>Combinaison simple des redirections</h2>

    <p>Entr&eacute;e et sortie standard peuvent &ecirc;tre r&eacute;dirig&eacute;es simultan&eacute;ment.
    <br>ainsi&nbsp;:</p>
    <table bgcolor="yellow" border="1" class="commands" style="margin-left:0;">
      <tr><td>cat &lt; fA &gt; fB</td></tr>
    </table>
    <p>copie le contenu du fichier fA pour cr&eacute;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&eacute;canisme appel&eacute; "pipe" permet d'encha&icirc;ner des commandes en affectant la sortie de l'une &agrave; l'entr&eacute;e de la suivante. Sa forme g&eacute;n&eacute;rale est&nbsp;:</p>
    <table bgcolor="yellow" border="1" class="commands" style="margin-left:0;">
      <tr><td>c0 &lt; fA | c1 | c2 | ... | c<i>n</i> &gt; fB</td></tr>
    </table>

    <p>Le fichier fA est lu et trait&eacute; par la commande c0, le r&eacute;sultat est alors trait&eacute; par c1, et ainsi de suite jusqu'&agrave; la commande c<i>n</i> pour laquelle le r&eacute;sultat est &eacute;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>