Outils pour utilisateurs

Outils du site


issue130:labolinux2

Différences

Ci-dessous, les différences entre deux révisions de la page.

Lien vers cette vue comparative

Prochaine révision
Révision précédente
issue130:labolinux2 [2018/02/24 12:09] – créée auntieeissue130:labolinux2 [2018/03/01 16:26] (Version actuelle) andre_domenech
Ligne 1: Ligne 1:
-When you're putting Bash scripts together, and portability with older systems and other types of shells isn't that important to you, then you might be pleased to discover that it's easy to make light work of your scripts by using a feature called Parameter Expansion. Over time, I've compiled a little list of the most common uses I've had for this feature, and I thought some people might find them helpful. I have to admit these examples are a little erratically listed, so, if you find that they're of interest, I'd suggest collecting your own notes.+// Suggestion de titre : Gagner du temps avec des scripts Bash //
  
-In case you're new to Shell Scriptingmany of the examples below are a modern shorthand, if you like. Just avoid using them on ten-year-old Bash and KornShell versions, and the chances are that they will work as expected on your friendlyneighbourhood Linux systemNonetheless, Unix purists should probably stop reading at this point! +**When you're putting Bash scripts together, and portability with older systems and other types of shells isn't that important to youthen you might be pleased to discover that it's easy to make light work of your scripts by using a feature called Parameter Expansion. Over time, I've compiled a little list of the most common uses I've had for this feature, and I thought some people might find them helpful. I have to admit these examples are a little erratically listed, so, if you find that they're of interestI'd suggest collecting your own notes.
  
-I seem to remember that this functionality was first introduced in Bash version 4.2and is available in versions from that point release onwardbut I might be wrongYou will probably receive an odd-looking error if your shell version doesn't support this functionalitybut it's unlikely that anything will break horribly. Figure One shows how to find out which version of Bash you're running on a machineexecuted as any unprivileged user – as are the other commands.+In case you're new to Shell Scriptingmany of the examples below are a modern shorthandif you likeJust avoid using them on ten-year-old Bash and KornShell versionsand the chances are that they will work as expected on your friendlyneighbourhood Linux systemNonetheless, Unix purists should probably stop reading at this point! **
  
 +Quand vous essayez de créer des scripts Bash, et si la portabilité vers des systèmes antérieurs et d'autres types de shell vous importe peu, vous serez heureux de découvrir la facilité avec laquelle vous pouvez faire des scripts en utilisant une fonctionnalité appelée Parameter Expansion. Au fil du temps, j'ai compilé une courte liste de mes usages les plus courants de cette fonctionnalité et je pensais que certains pourraient les trouver utiles. Je dois avouer que ces exemples sont listés de façon erratique ; aussi, si vous les trouvez intéressants, je suggère de compiler vos propres notes.
  
-Let's not beat about the bush any longer. These examples are hopefully self-explanatory. There's plenty more reading online such as on the GNU site (https://www.gnu.org/software/bash/manual/html_node/Shell-Parameter-Expansion.html)and the Bash Hackers Wiki (http://wiki.bash-hackers.org/syntax/pe). The following examples all start simply with the command you would typeand underneath is what your shell will output (in most casesversion dependent) having hit the Enter keyIntegrate these snippets into your scripts in any way that you see fit+Si vous ne connaissez pas encore le Shell Scriptingbeaucoup des exemples ci-dessous sonten quelque sortede la sténographie moderneIl suffit d'éviter de les utiliser sur des versions de Bash et de Kornshell âgées de 10 ans ou plus, et il est probable qu'ils fonctionnent comme attendu sur un système Linux « normal »Cela étant dit, les puristes Unix devraient sans doute arrêter de lire tout de suite !
  
-Food For Thought+**I seem to remember that this functionality was first introduced in Bash version 4.2, and is available in versions from that point release onward, but I might be wrong. You will probably receive an odd-looking error if your shell version doesn't support this functionality, but it's unlikely that anything will break horribly. Figure One shows how to find out which version of Bash you're running on a machine, executed as any unprivileged user – as are the other commands. 
 + 
 + 
 +Let's not beat about the bush any longer. These examples are hopefully self-explanatory. There's plenty more reading online such as on the GNU site (https://www.gnu.org/software/bash/manual/html_node/Shell-Parameter-Expansion.html), and the Bash Hackers Wiki (http://wiki.bash-hackers.org/syntax/pe). The following examples all start simply with the command you would type, and underneath is what your shell will output (in most cases, version dependent) having hit the Enter key. Integrate these snippets into your scripts in any way that you see fit. ** 
 + 
 +Il me semble me souvenir que cette fonctionnalité fut d'abord introduite dans la version 4.2 de Bash et est disponible dans les versions ultérieures, mais j'ai peut-être tort. Vous recevrez vraisemblablement un message d'erreur bizarroïde si la version de votre shell ne la supporte pas, mais il y a peu de chances que quelque chose se casse irrémédiablement. L'image ci-dessous montre comment trouver la version de Bash sur votre machine, lancé comme un quelconque utilisateur sans droits, tout comme les autres commandes. 
 + 
 +Ne tournons plus autour du pot. Ces exemples sont, je l'espère, explicites. Il existe beaucoup plus de lecture en ligne, notamment sur le site GNU (https://www.gnu.org/software/bash/manual/html_node/Shell-Parameter-Expansion.html) et le wiki des Bash Hackers (http://wiki.bash-hackers.org/syntax/pe). Tous les exemples qui suivent commencent simplement par la commande que vous saisiriez, avec, en dessous, ce que votre shell sortira (dans la plupart des cas, cela dépend de votre version) une fois que vous aurez appuyé sur la touche Entrée. Vous pouvez intégrer ces segments dans vos scripts comme vous le désirez. 
 + 
 +**Food For Thought
  
 This short list is really just a taster of this powerful functionality to whet your appetite. This short list is really just a taster of this powerful functionality to whet your appetite.
Ligne 34: Ligne 44:
 $ blue="1234567890"; echo ${blue/456} $ blue="1234567890"; echo ${blue/456}
  
-1237890+1237890**
  
 +Matière à réflexion
  
 +Cette brève liste n'est en fait qu'un avant-goût de cette fonctionnalité puissante, pour vous mettre en appétit.
  
-- Display chars up to a delimiter:+- Trouver les deux caractères après la position trois : 
 + 
 +$ me="1234567890"; echo  
 +${me:3:2} 
 + 
 +45 
 + 
 +- Ignorer les trois premiers caractères : 
 + 
 +$ you="1234567890"; echo  
 +${you:3} 
 + 
 +4567890 
 + 
 +- Remplacer des caractères après les avoir trouvés : 
 + 
 +$ them="1234567890"; echo  
 +${them/456/XXX} 
 + 
 +123XXX7890 
 + 
 +- Enlever des caractères d'une chaîne : 
 + 
 +$ blue="1234567890"; echo  
 +${blue/456} 
 + 
 +1237890 
 + 
 +**- Display chars up to a delimiter:
  
 $ red="1234567890"; echo ${red%5*} $ red="1234567890"; echo ${red%5*}
Ligne 59: Ligne 99:
    
 $ green="/etc/resolv.conf"; echo ${green#/etc/} $ green="/etc/resolv.conf"; echo ${green#/etc/}
 +
 +resolv.conf**
 +
 +- Afficher des caractères avant un délimiteur :
 +
 +$ red="1234567890"; echo 
 +${red%5*}
 +
 +1234
 +
 +- Afficher des caractères après un délimiteur :
 +
 +$ yellow="1234567890"; echo 
 +${yellow#*5}
 +
 +67890
 +
 +- Apprendre la longueur d'une variable (notez l'usage inhabituel du # dans ce cas) :
 + 
 +$ orange=123; length=${#orange}; echo 
 +$length
 +
 +
 +
 +- Enlever la première partie d'une variable :
 + 
 +$ green="/etc/resolv.conf"; echo 
 +${green#/etc/}
  
 resolv.conf resolv.conf
  
-- Remove pattern (the end of a variable):+**- Remove pattern (the end of a variable):
    
 $ pink="chris_secret_file.tar.gz"; echo ${pink%.tar.gz} $ pink="chris_secret_file.tar.gz"; echo ${pink%.tar.gz}
Ligne 78: Ligne 146:
 $ chrisbinnie="www.devsecops.cc"; echo ${chrisbinnie:4:9} $ chrisbinnie="www.devsecops.cc"; echo ${chrisbinnie:4:9}
  
-devsecops +devsecops ** 
 + 
 +- Enlever la fin d'une variable : 
 +  
 +$ pink="chris_secret_file.tar.gz"; echo  
 +${pink%.tar.gz} 
 + 
 +chris_secret_file 
 + 
 +- Chercher et remplacer : 
 +  
 +$ chris="Containers are great !"; echo  
 +${chris/great/fantastic} 
 + 
 +Containers are fantastic ! 
 + 
 +- Découvrir une sous-chaîne : 
 +  
 +$ chrisbinnie="www.devsecops.cc"; echo  
 +${chrisbinnie:4:9} 
 + 
 +devsecops 
 + 
 +**The End Is Nigh 
 + 
 +Hopefully you agree that, with the addition of some of the examples mentioned on the URLs above, these commands make it possible to significantly speed up your scripting. I hope you enjoy putting them to good use either as one-liners on your command-line, or in shell scripts (as I mostly use them).**
  
-The End Is Nigh+La fin approche
  
-Hopefully you agree that, with the addition of some of the examples mentioned on the URLs abovethese commands make it possible to significantly speed up your scriptingI hope you enjoy putting them to good use either as one-liners on your command-lineor in shell scripts (as I mostly use them).+J'espère que vous êtes d'accord avec moi : avec l'ajout de quelques-uns des exemples mentionnés sur les URL ci-dessusces commandes rendent possible l'accélération significative de l'écriture de vos scriptsJe vous souhaite beaucoup de plaisir en les utilisantsoit en tant que ligne dans votre interface en ligne de commande, ou dans des scripts shell (leur usage principal pour moi).
  
issue130/labolinux2.1519470548.txt.gz · Dernière modification : 2018/02/24 12:09 de auntiee