Newer
Older
cours-unix-shell / UnixShell_cours2018 / shellScript.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>Scripts - 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="shellScript.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&egrave;tres"><link rel="section" href="testShell.fre.html" title="Tests"><link rel="section" href="structureShell.fre.html" title="Structures de contr&ocirc;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&eacute;ristiques d'UNIX">
    <link rel="previous" href="shellVars.fre.html" title="Variables d'environnement">
    <link rel="next" href="paramShell.fre.html" title="Param&egrave;tres">
    <link rel="last" href="tpShell.fre.html" title="Travaux Pratiques">
  </head>

  <body>
    <h1>Scripts - 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" id="subcurrent">Scripts</a></li>
	    <li><a href="paramShell.fre.html">Param&egrave;tres</a></li>
	    <li><a href="testShell.fre.html">Tests</a></li>
	    <li><a href="structureShell.fre.html">Structures de contr&ocirc;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>
      Un fichier script contenant des commandes shell (par exemple des
      initialisations de variables, une automatisation de t&acirc;ches,...) peut &ecirc;tre
      ex&eacute;cut&eacute;&nbsp;:
    </p>
    <ul>
      <li>soit par <code>sh nom_fichier_shell</code></li> 
      <li>
	soit par <code>. nom_fichier_shell</code> (la diff&eacute;rence avec
	la mani&egrave;re pr&eacute;c&eacute;dente est que c'est comme
	si on tapait le contenu du fichier dans le shell courant, et
	non comme si on lan&ccedil;ait un autre shell, c'est important
	par exemple pour les d&eacute;finitions de variables)
      </li>
      <li>
	soit en rendant le fichier ex&eacute;cutable (<code>chmod u+x
	nom_fichier_shell</code>), puis en tapant le nom du fichier (cela
	fonctionne uniquement quand le r&eacute;pertoire courant,
	<code>.</code>, est dans <code>$PATH</code>, sinon il faut
	dire au shell que c'est le fichier qui est dans le
	r&eacute;pertoire courant qu'on veut ex&eacute;cuter&nbsp;:
	<code>./nom_fichier_shell</code>).
      </li>
    </ul>
    <p>
      Pour &ecirc;tre s&ucirc;r du shell qui est en train de
      s'ex&eacute;cuter dans un fichier de script shell, il est plus
      prudent d'ajouter son nom en d&eacute;but de fichier la ligne (par exemple, ici <code>sh</code>)&nbsp;:
    </p>
    <pre>#!/bin/sh</pre>
    <p>
      Cette ligne a la forme d'un commentaire (elle commence par
      <code>#</code>), on l'appelle un shebang, et signale au shell appelant 
      qu'il faut utiliser le shell <code>/bin/sh</code> pour ex&eacute;cuter le
      fichier. Ce shebang, qui est un en-t&ecirc;te, doit se mettre sur la
      premi&egrave;re ligne.
    </p>
    <p>
      Une seconde ligne peut &ecirc;tre int&eacute;ressante car elle peut 
      influer sur l'ordre de tri avec <code>sort</code> par exemple. Pour garantir 
      un ordre de tri lexicographique, on peut positionner la variable 
      d'environnement suivante comme suit <code>export LC_COLLATE=C</code>.
    </p>
    </div>
    <hr>
    <address><a href="mailto:philippe.houdry@inist.fr">Philippe.Houdry@inist.fr</a></address>
  </body>
</html>