Outils pour utilisateurs

Outils du site


issue176:c_c

Ceci est une ancienne révision du document !


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. 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'entre nous ne réfléchit pas quand on écrit sur un terminal ; il existe, c'est tout. En fait, vous saississez des commandes dans un shell qui qui les interprète. Par défaut, Ubuntu est livrée avec bash, mais vous pouvez installer un autre shell si vous voulez. Je vais me concentrer sur bash, parce 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.

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/coller sans compréhendre ce qu'ils venait de faire. Je pensais que je pouvais essayer d'éclairer le sujet, car copier/coller un truc dont vous ignorez tout fait peur.

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.

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 saississez à 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 de tâche - le bon vieux caractère égale. 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 référencer 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. Toute ce que vous avez fait était de substituer $var1 pour 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 de le faire echo à l'écran à nouveaut 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:

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).

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.

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.

You will notice that PATH is in capitals. Try it in lowercase and see what happens. Now type: echo $USER and press enter. You can do the same with the $HOME variable. The $PATH variable is an important one. This is where the system looks for files to execute. So be careful what you add to this path. A good idea is to copy and paste this output into a text file BEFORE you start messing with your user’s variables. Type: echo $0 and press enter. Now type: echo $PS1 and press enter.

Now look at those scripts you grabbed from the internet again. Are they starting to make sense? Pay attention to those dollar signs and backticks when reading through the line and if you are unsure what they represent, simply echo it out.

Your homework

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> ”

issue176/c_c.1641301527.txt.gz · Dernière modification : 2022/01/04 14:05 de auntiee