issue176:c_c
Différences
Ci-dessous, les différences entre deux révisions de la page.
Les deux révisions précédentesRévision précédenteProchaine révision | Révision précédente | ||
issue176:c_c [2022/01/04 14:05] – auntiee | issue176:c_c [2022/01/05 15:36] (Version actuelle) – andre_domenech | ||
---|---|---|---|
Ligne 5: | Ligne 5: | ||
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. ** | ||
- | La plupart d' | + | La plupart d' |
- | Le terminal par défaut est ennuyeux. Bon, je l'ai dit. L'une des premières choses que des gens font est de le personnaliser. Je trouve souvent que beaucoup de gens font des recherches sur le Net pour des scripts mystérieux et font du copier/ | + | Le terminal par défaut est ennuyeux. Bon, je l'ai dit. L'une des premières choses que des gens font est de le personnaliser. Je trouve souvent que beaucoup de gens font des recherches sur le Net pour des scripts mystérieux et font du copier/ |
- | Nous allons parler d'aord 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. | + | 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. | **We will use the echo command first, so it can echo what you enter on the screen. | ||
Ligne 15: | Ligne 15: | ||
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.** | 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' | + | Nous utiliserons la commande echo d' |
- | Pour référencer | + | Pour faire référence à n' |
- | A good practice is to use curly braces when referencing a variable, this makes scripting a lot easier. The format is: ${variable} ie: ${var3} | + | **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 25: | 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 possible, should you want to script something. | + | Une bonne pratique est d' |
- | 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 " | + | Cela vous permet d' |
- | 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' | ||
+ | |||
+ | **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 " | ||
+ | |||
+ | 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, | ||
+ | |||
+ | Nous pouvons même utiliser la sortie d'une commande comme variable. Pour cela, nous utilisons des parenthèses à la place d' | ||
+ | |||
+ | **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 42: | Ligne 54: | ||
See if you can say what this prompt replacement does and why it does it: PS1=" | See if you can say what this prompt replacement does and why it does it: PS1=" | ||
+ | ** | ||
+ | |||
+ | Et comment voir les variables que nous aimerions changer ? | ||
+ | |||
+ | Exactement comme vous l'avez fait jusqu' | ||
+ | |||
+ | 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' | ||
+ | |||
+ | Vos devoirs | ||
+ | Pouvez-vous dire ce que fait ce remplacement d' |
issue176/c_c.1641301527.txt.gz · Dernière modification : 2022/01/04 14:05 de auntiee