issue176:c_c
Différences
Ci-dessous, les différences entre deux révisions de la page.
Prochaine révision | Révision précédente | ||
issue176:c_c [2022/01/02 14:24] – créée auntiee | issue176: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 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. | + | La plupart d' |
- | 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. | + | 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/ |
- | 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' |
+ | |||
+ | **We will use the echo command first, so it can echo what you enter on the screen. | ||
+ | |||
+ | 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' | ||
+ | |||
+ | 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} | ||
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 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 32: | 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.1641129858.txt.gz · Dernière modification : 2022/01/02 14:24 de auntiee