Outils pour utilisateurs

Outils du site


issue176:c_c

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
issue176:c_c [2022/01/02 14:24] – créée auntieeissue176:c_c [2022/01/05 15:36] (Version actuelle) andre_domenech
Ligne 1: Ligne 1:
-Most of us don’t think twice when typing at a terminal, it’s just there. What is actually happening is that you are typing commands into a shell that is interpreting your commands. By default, Ubuntu ships with bash, but you can install another if you like. I will focus on bash. The reason being, that if you ever get to use servers or containers on the internet, chances are good that it is bash.+**Most of us don’t think twice when typing at a terminal, it’s just there. What is actually happening is that you are typing commands into a shell that is interpreting your commands. By default, Ubuntu ships with bash, but you can install another if you like. I will focus on bash. The reason being, that if you ever get to use servers or containers on the internet, chances are good that it is bash.
  
 The default terminal is boring. There, I said it. One of the first things people do is customize it. I often find that a lot of people simply search the internet for cryptic scripts and do the copy/paste thing without understanding what they just did. I thought I could try to shed some light on the subject, as it is scary to copy/paste something and you don’t know what it does. The default terminal is boring. There, I said it. One of the first things people do is customize it. I often find that a lot of people simply search the internet for cryptic scripts and do the copy/paste thing without understanding what they just did. I thought I could try to shed some light on the subject, as it is scary to copy/paste something and you don’t know what it does.
  
-First, let us talk about variables. You can think of variables as substitutes for something that may change. Please open a terminal and let’s play and learn. +First, let us talk about variables. You can think of variables as substitutes for something that may change. Please open a terminal and let’s play and learn. **
  
-We will use the echo command firstso it can echo what you enter on the screen  Type echo “My name is Jack” and press enter and you will see whatever is in “ ” on the screen. This is what echo doesnothing fancy or hidden hereWe will use echo to see what is inside our variables. To assign a value to a variablewe simply use the assignment operator – the good old equals signType: var1=123 and press enter to assign the value 123 to the variable named var1. Spaces matterso var1 = 123 will not work. There are also no spaces in the variable name. Remember that and you should be golden.+La plupart d'entre nous ne réfléchissent pas quand ils écrivent sur un terminal ; il existec'est toutEn faitvous saisissez des commandes dans un shell qui les interprètePar défautUbuntu est livrée avec bash, mais vous pouvez installer un autre shell si vous voulezJe vais me concentrer sur bashparce que, si vous avez jamais l'occasion d'utiliser des serveurs ou des conteneurs sur l'Internet, il y a de bonnes chances que ce soit bash.
  
-To reference any variable in bash, we need to use the $ - dollar signType: echo “$var1” and press enter. This will simply echo the value of var1 to the screen. Just to make suretype: echo “var1” and press enterDo you see what I meant? Now type echo “$var2” and press enterWhat result did you get? Do you understand why? If not, you know where to send your questionsWe can also combine variables with other variables and even non-variables. Type: “the numbers are $var1” and press enter. All you did was substitute the $var1 for whatever you put inside it. So let’s say we don’t need var1 any more. To get rid of itsimply use “unset”. Type: unset var1 and press enter. Now try to echo it to the screen again to confirm it is really gone.+Le terminal par défaut est ennuyeuxBonje l'ai ditL'une des premières choses que des gens font est de le personnaliserJe trouve souvent que beaucoup de gens font des recherches sur le Net pour des scripts mystérieux et font du copier/coller sans comprendre ce qu'ils viennent de faireJe pensais que je pouvais essayer d'éclairer le sujetcar copier/coller un truc dont vous ignorez tout fait peur.
  
-A good practice is to use curly braces when referencing a variable, this makes scripting a lot easier. The format is: ${variable} ie: ${var3} +Nous allons parler d'abord de variables. Vous pouvez les considérer comme des substituts pour quelque chose qui peut changer. Ouvrez un terminal, s'il vous plaît, pour que nous puissions jouer et apprendre. 
 + 
 +**We will use the echo command first, so it can echo what you enter on the screen.   Type echo “My name is Jack” and press enter and you will see whatever is in “ ” on the screen. This is what echo does, nothing fancy or hidden here. We will use echo to see what is inside our variables. To assign a value to a variable, we simply use the assignment operator – the good old equals sign. Type: var1=123 and press enter to assign the value 123 to the variable named var1. Spaces matter, so var1 = 123 will not work. There are also no spaces in the variable name. Remember that and you should be golden. 
 + 
 +To reference any variable in bash, we need to use the $ - dollar sign. Type: echo “$var1” and press enter. This will simply echo the value of var1 to the screen. Just to make sure, type: echo “var1” and press enter. Do you see what I meant? Now type echo “$var2” and press enter. What result did you get? Do you understand why? If not, you know where to send your questions. We can also combine variables with other variables and even non-variables. Type: “the numbers are $var1” and press enter. All you did was substitute the $var1 for whatever you put inside it. So let’s say we don’t need var1 any more. To get rid of it, simply use “unset”. Type: unset var1 and press enter. Now try to echo it to the screen again to confirm it is really gone.** 
 + 
 +Nous utiliserons la commande echo d'abord et le terminal fera echo à tout ce que vous saisissez à l'écran. Tapez echo "Mon nom est Jacques" et appuyez sur entrée et vous verrez ce qui est entre les "" à l'écran. C'est ce que fait echo, rien de sophistiqué ni de caché ici. Nous utiliserons echo pour voir ce qui se trouve à l'intérieur de nos variables. Pour assigner une valeur à une variable, nous utilisons tout simplement l'opérateur d'affectation - le bon vieux caractère égal. Tapez : var1=123 et appuyez sur entrée pour assigner la valeur 123 à la variable appelée var1. Puisque les espaces comptent, var1 = 123 ne fonctionnera pas. Il n'y a pas non plus d'espaces dans le nom de la variable. Souvenez-vous de cela et tout se passera très bien. 
 + 
 +Pour faire référence à n'importe quelle variable dans bash, il faut utiliser le caractère dollar - $. Tapez : echo "$var1" et appuyez sur entrée. Cela reproduira tout simplement la valeur de var1 à l'écran. Pour vous en convaincre, tapez: echo "var1" et appuyez sur entrée. Comprenez-vous ce que je voulais dire ? Maintenant, tapez echo "$var2" et appuyez sur entrée. Quel est le résultat ? Comprenez-vous pourquoi ? Sinon, vous savez où envoyer vos questions. On peut aussi combiner des variables avec d'autres variables et même des non-variables. Tapez : "les chiffres sont $var1" et appuyez sur entrée. Tout ce que vous avez fait a été de remplacer $var1 par ce que vous avez mis à son intérieur. Bon. Disons que nous n'avons plus besoin de var1. Pour le supprimer, il suffit d'utiliser la commande « unset ». Tapez : unset var1 et appuyez sur entrée. Maintenant, essayez à nouveau d'en recevoir l'écho à l'écran pour confirmer qu'elle est vraiment supprimée. 
 + 
 +**A good practice is to use curly braces when referencing a variable, this makes scripting a lot easier. The format is: ${variable} ie: ${var3} 
  
 This allows you to add something to the variable, consider: This allows you to add something to the variable, consider:
Ligne 15: Ligne 25:
 Type: var1=gametype and press enter. Now type echo ${var1}AAA and press enter.  Type: var1=gametype and press enter. Now type echo ${var1}AAA and press enter. 
  
-Now repeat that without the curly braces and see what happens. You can do more with your numbers and strings than we have until now. A variable can contain a command in text, for example, type: space=”df -h” and press enter. Now type $space and press enter. When you echo the variable, it does not ’run’, but when you ‘call’ it with $space, it does (see image below).+Now repeat that without the curly braces and see what happens. You can do more with your numbers and strings than we have until now. A variable can contain a command in text, for example, type: space=”df -h” and press enter. Now type $space and press enter. When you echo the variable, it does not ’run’, but when you ‘call’ it with $space, it does (see image below).**
  
-The text was converted to a command. While this is cute and all, I would rather you use an alias if you wanted to do this sort of thing, but you can see what is possibleshould you want to script something+Une bonne pratique est d'utiliser des accolades quand vous référencez une variablecar cela rend la création des scripts beaucoup plus facileLe format est : ${variable} par exemple : ${var3}
  
-We can even use the output of a command as the variable. For that we use brackets instead of braces. Typex=$(pwd) and press enter. Like before, echo $x and press enter to see what happened. This can be useful when you need to check up on a log in a folder that you don’t want to type out the full path again or use it multiple times in your script. However, in some of the scripts you may see backticks, that ` usually sit on the same key as tilde. Type: echo "`uptime`" and press enter and see what you get. If you get an error, you used single quotes instead of backticks. And just like that, you can use it in your own scripts once you “get” it. See those scripts don’t seem so alien now.+Cela vous permet d'ajouter quelque chose à la variable :
  
-So how do we look at those variables we would like to change?+Tapez : var1=typedejeu et appuyez sur entrée. Maintenant tapez echo ${var1}AAA et appuyez sur entrée. 
 + 
 +Maintenant, faites-le à nouveau, mais sans les accolades, pour voir ce qui se passe. Vous pouvez faire plus avec vos chiffres et chaînes que ce que nous avons fait jusqu'ici. Une variable peut contenir une commande en texte, par exemple, tapez : space="df -h" et appuyez sur entrée. Maintenant, tapez $space et appuyez sur entrée. Quand vous faites echo à la variable, elle ne se « lance » pas, mais quand vous « l'appelez » avec $space, elle le fait (voir l'image ci-dessous). 
 + 
 +**The text was converted to a command. While this is cute and all, I would rather you use an alias if you wanted to do this sort of thing, but you can see what is possible, should you want to script something.  
 + 
 +We can even use the output of a command as the variable. For that we use brackets instead of braces. Type: x=$(pwd) and press enter. Like before, echo $x and press enter to see what happened. This can be useful when you need to check up on a log in a folder that you don’t want to type out the full path again or use it multiple times in your script. However, in some of the scripts you may see backticks, that ` usually sit on the same key as tilde. Type: echo "`uptime`" and press enter and see what you get. If you get an error, you used single quotes instead of backticks. And just like that, you can use it in your own scripts once you “get” it. See those scripts don’t seem so alien now.** 
 + 
 +Le texte a été converti en commande. Alors que c'est sympa et tout, je préférerais que vous utilisiez un alias si vous vouliez faire ce genre de chose, mais vous pouvez voir les possibilités, si vous voulez scripter quelque chose. 
 + 
 +Nous pouvons même utiliser la sortie d'une commande comme variable. Pour cela, nous utilisons des parenthèses à la place d'accolades. Tapez x=$(pwd) et appuyez sur entrée. Comme avant, tapez echo $x et appuyez sur entrée pour voir ce qui s'est passé. Cela peut être utile quand vous devez vérifier un journal dans un dossier vers lequel vous ne voulez pas taper le chemin complet encore une fois ou l'utiliser de multiples fois dans votre script. Cependant, dans certains scripts, vous pouvez voir des quotes inverses (`) qui se trouvent habituellement sur la même touche que l'è/7. Tapez : echo "`uptime`" et appuyez sur entrée pour voir le résultat. S'il y a un message d'erreur, vous avez sans doute utilisé des apostrophes au lieu de quotes inverses. Et, comme par magie, vous pouvez l'utiliser dans vos propres scripts, une fois que vous l'avez compris. Ces scripts ne semblent pas si mystérieux maintenant, non ? 
 + 
 +**So how do we look at those variables we would like to change?
  
 Just like you have been doing it up to now. Type: echo $PATH and press enter. Just like you have been doing it up to now. Type: echo $PATH and press enter.
Ligne 32: Ligne 54:
  
 See if you can say what this prompt replacement does and why it does it: PS1="\`if [ \$? = 0 ]; then echo \[\e[33m\]^_^\[\e[0m\]; else echo \[\e[31m\]O_o\[\e[0m\]; fi\`[\u@\h:\w]\<strong>\$</strong> " See if you can say what this prompt replacement does and why it does it: PS1="\`if [ \$? = 0 ]; then echo \[\e[33m\]^_^\[\e[0m\]; else echo \[\e[31m\]O_o\[\e[0m\]; fi\`[\u@\h:\w]\<strong>\$</strong> "
 +**
 +
 +Et comment voir les variables que nous aimerions changer ?
 +
 +Exactement comme vous l'avez fait jusqu'ici. Tapez : echo $PATH et appuyez sur entrée.
 +
 +Vous remarquerez que PATH est en majuscules. Essayez-le en lettres minuscules pour voir ce qui se passe. Maintenant tapez : echo $USER et appuyez sur entrée. Vous pouvez faire la même chose avec la variable $HOME. La variable PATH est importante. C'est là où le système recherche des fichiers à lancer. Il faut, donc, faire très attention à ce que vous ajoutez à ce chemin. Une bonne idée est de copier et coller le résultat dans un fichier texte AVANT de commencer à bricoler avec les variables de votre utilisateur. Tapez : echo $0 et appuyez sur entrée. À présent tapez : echo $PS1 et appuyez sur entrée.
 +
 +Maintenant, examinez les scripts que vous avez récupérés sur le Net à nouveau. Commencent-ils à devenir plus clairs ? Regardez attentivement les caractères dollar et quote inverse quand vous lisez une ligne et si vous n'êtes pas certain de ce qu'ils représentent, il suffit simplement d'y faire echo.
 +
 +Vos devoirs
  
 +Pouvez-vous dire ce que fait ce remplacement d'invite et pourquoi : PS1="\`if [ \$? = 0 ]; then echo \[\e[33m\]^_^\[\e[0m\]; else echo \[\e[31m\]O_o\[\e[0m\]; fi\`[\u@\h:\w]\<strong>\$</strong> "
issue176/c_c.1641129858.txt.gz · Dernière modification : 2022/01/02 14:24 de auntiee