Skip to content
GitLab
Explorer
Connexion
S'inscrire
Navigation principale
Rechercher ou aller à…
Projet
Learning Bash
Gestion
Activité
Membres
Labels
Programmation
Tickets
Tableaux des tickets
Jalons
Wiki
Code
Requêtes de fusion
Dépôt
Branches
Validations
Étiquettes
Graphe du dépôt
Comparer les révisions
Extraits de code
Compilation
Pipelines
Jobs
Planifications de pipeline
Artéfacts
Déploiement
Releases
Registre de paquets
Registre de conteneur
Registre de modèles
Opération
Environnements
Modules Terraform
Surveillance
Incidents
Analyse
Données d'analyse des chaînes de valeur
Analyse des contributeurs
Données d'analyse CI/CD
Données d'analyse du dépôt
Expériences du modèle
Aide
Aide
Support
Documentation de GitLab
Comparer les forfaits GitLab
Forum de la communauté
Contribuer à GitLab
Donner votre avis
Conditions générales et politique de confidentialité
Raccourcis clavier
?
Extraits de code
Groupes
Projets
Afficher davantage de fils d'Ariane
Pierre-Yves Barriat
Learning Bash
Validations
fdc3835a
Valider
fdc3835a
rédigé
Il y a 8 mois
par
Jérôme de Favereau de Jeneret
Parcourir les fichiers
Options
Téléchargements
Correctifs
Plain Diff
petits changements de formulations principalement
parent
23d332b9
Aucune branche associée trouvée
Aucune étiquette associée trouvée
1 requête de fusion
!4
Exercice 5 + quelques changements de formulation
Modifications
1
Masquer les modifications d'espaces
En ligne
Côte à côte
Affichage de
1 fichier modifié
Bash.md
+20
-18
20 ajouts, 18 suppressions
Bash.md
avec
20 ajouts
et
18 suppressions
Bash.md
+
20
−
18
Voir le fichier @
fdc3835a
...
...
@@ -34,7 +34,7 @@ It's called the **C**ommand **L**ine **U**ser **I**nterface
**CLUI** is one of the many strengths of Linux
:
-
allows to be independent of distros (or UNIX systems like OSX)
-
allows to easily work
at distance
(SSH)
-
allows to easily work
remotely
(SSH)
-
allows to join together simple (and less simple) commands to do complex things and automate **= scripting**
In Linux, process automation relies heavily on scripting. This involves creating a file containing a series of commands that can be executed together
...
...
@@ -43,7 +43,7 @@ In Linux, process automation relies heavily on scripting. This involves creating
# Linux Shell
A
**shell**
is a program that takes commands from the keyboard and
give
s them to the operating system to perform
A
**shell**
is a program that takes commands from the keyboard and
transmit
s them to the operating system to perform
The main function is to interpret your commands
**= language**
...
...
@@ -92,8 +92,6 @@ In a Bash shell many things constitute your environment
Environment includes many variables that may have been set
**by bash**
or
**by you**
**Access the value of a variable by prefixing its name with `$`**
---
# Environment variables
...
...
@@ -106,6 +104,8 @@ Environment includes many variables that may have been set **by bash** or **by y
|
`SHELL`
| the name of the shell |
|
`UID`
| the numeric user id of the logged-in user |
**Access the value of a variable by prefixing its name with `$`**
So to get the value of
`USER`
you would use
`$USER`
in bash code
> You can use special files to control bash variables : `$HOME/.bashrc`
...
...
@@ -142,7 +142,7 @@ echo "A comment will follow." # Comment here.
-
Use something your editor makes easy (
**Vim**
uses
`Tab`
)
---
<!-- JDF: je pnseque c'est trop tôt pour ceci
<!-- JDF: je p
e
nse
que c'est trop tôt pour ceci
### Command separators
Commands can be combined using
**meta-characters**
and
**control operators**
...
...
@@ -335,7 +335,7 @@ echo $b # 16
Use:
*
`if condition; then`
to start conditional block
*
`else`
to start alternative block
*
`elif`
to start alternative conition block
*
`elif`
to start alternative con
d
ition block
*
`fi`
to close conditional block
The following operaors can be used beween conditions:
...
...
@@ -442,7 +442,7 @@ esac
# Hands-on exercise
1.
In your
`bash_exercises`
folder create a new bash file called
`exercise_2.sh`
and make it executable
2.
Ask the user for two numbers smaller
or equal to 100,
put them in variables
`NUMBER1`
and
`NUMBER2`
2.
Ask the user for two numbers smaller
than 100 and
put them in variables
`NUMBER1`
and
`NUMBER2`
> ```bash
> #!/bin/bash
...
...
@@ -452,7 +452,7 @@ esac
3.
Check if the numbers are smaller than 100
-
If yes, check if both numbers are even and tell the user
-
If not, tell the user
with
`echo
'my message'
`
-
If not, tell the user
(use
`echo`
)
---
...
...
@@ -690,8 +690,6 @@ Shells use 3 standard I/O streams
Shell has several
**meta-characters**
and
**control operators**
> `|`, `&`, `>`, `;`, `<`, etc.
---
# Control operators
...
...
@@ -814,7 +812,7 @@ _files="$@"
*
"small script within a script" that you may call multiple times
*
great way to reuse code
*
a function is most reuseable when it performs a single task
*
good to put ancillary tasks within functions : logically separate from main
*
good to put ancillary tasks within functions : logically separate from main
code
```
bash
#!/bin/bash
...
...
@@ -883,6 +881,9 @@ my_function
echo
$func_result
```
<!-- Note JDF: est-ce que la partie ci-dessus est utile ? Autant montrer directement la bonne manière pour moi
-->
*
Better way is to send the value to
`stdout`
using
`echo`
```
bash
...
...
@@ -908,7 +909,7 @@ print_something () {
print_something Mars
```
>
Be careful in case of "overriding commands": plaese never use an
existing linux command
name
.
>
Athough it is possible, you should try to avoid having functions using the name of
existing linux command
s
.
---
...
...
@@ -929,8 +930,8 @@ Consider the script `test.sh` below :
```
bash
#!/bin/bash
echo
$
var1
echo
$
var2
echo
"var1 =
${
var1
}
"
echo
"var2 =
${
var2
}
"
```
Then run this script :
...
...
@@ -940,6 +941,7 @@ var1=23
export
var2
=
12
bash test.sh
```
> By default, variables from the main interpreter are not available in scripts, unless you `export` them.
---
...
...
@@ -956,7 +958,7 @@ bash test.sh
### Syntax
A command list embedded between parentheses runs as a subshell :
A command list embedded
**
between parentheses
**
runs as a subshell :
```
bash
#!/bin/bash
...
...
@@ -976,8 +978,8 @@ bash -c "command1; command2; command3"
## Differences between **Sourcing** and **Executing** a script
-
source a script = execution in the current shell
> variables and functions are valid in the current shell after sourcing
-
source a script = execution
**
in the current shell
**
> variables and functions are valid in the current shell after sourcing
even if not `export`ed
-
execute a script = execution in a new shell (in a subshell of the current shell)
> all new variables and functions created by the script will only live in the subshell
...
...
@@ -1041,7 +1043,7 @@ fi
### use `echo`
"c
lassical
"
but useful technique : insert
`echo`
throughout your code
C
lassical but useful technique : insert
`echo`
throughout your code
to check variable content
```
bash
#!/bin/bash
...
...
Ce diff est replié.
Cliquez pour l'agrandir.
Aperçu
0%
Chargement en cours
Veuillez réessayer
ou
joindre un nouveau fichier
.
Annuler
You are about to add
0
people
to the discussion. Proceed with caution.
Terminez d'abord l'édition de ce message.
Enregistrer le commentaire
Annuler
Veuillez vous
inscrire
ou vous
se connecter
pour commenter