Instructions IF

Introduction à la programmation Bash

Alex Scriven

Data Scientist

IF de base

Une instruction IF de base en Bash suit cette structure :

if [ CONDITION ]; then

# SOME CODE
else # SOME OTHER CODE
fi

Deux conseils :

  • Espaces entre les crochets et les éléments de la condition (première ligne)
  • Point-virgule après le crochet fermant ];
Introduction à la programmation Bash

IF et chaînes de caractères

On peut comparer des chaînes dans une instruction IF :

x="Queen"

if [ $x == "King" ]; then
echo "$x is a King!"
else echo "$x is not a King!" fi
Queen is not a King!

Vous pouvez aussi utiliser != pour « différent de »

Introduction à la programmation Bash

IF arithmétiques (option 1)

Les IF arithmétiques peuvent utiliser la structure à doubles parenthèses :

x=10

if (($x > 5)); then echo "$x is more than 5!" fi
10 is more than 5!
Introduction à la programmation Bash

IF arithmétiques (option 2)

Les IF arithmétiques peuvent aussi utiliser des crochets et un indicateur arithmétique plutôt que (>, <, =, !=, etc.) :

  • -eq pour « égal à »
  • -ne pour « différent de »
  • -lt pour « plus petit que »
  • -le pour « plus petit ou égal à »
  • -gt pour « plus grand que »
  • -ge pour « plus grand ou égal à »
Introduction à la programmation Bash

Exemple d'IF arithmétique

Reprenons l'exemple précédent avec la notation à crochets :

x=10
if [ $x -gt 5 ]; then
    echo "$x is more than 5!"
fi
10 is more than 5!
Introduction à la programmation Bash

Autres indicateurs conditionnels Bash

Bash offre aussi divers indicateurs liés aux fichiers, par exemple :

  • -e si le fichier existe
  • -s si le fichier existe et a une taille supérieure à zéro
  • -r si le fichier existe et est lisible
  • -w si le fichier existe et est inscriptible

Et plusieurs autres :

Introduction à la programmation Bash

Utiliser AND et OR en Bash

 

Pour combiner des conditions (ET) ou utiliser OU en Bash, servez-vous des symboles suivants :

  • && pour AND
  • || pour OR
Introduction à la programmation Bash

Conditions multiples

En Bash, vous pouvez enchaîner les conditions ainsi :

x=10
if [ $x -gt 5 ] && [ $x -lt 11 ]; then
    echo "$x is more than 5 and less than 11!"
fi

Ou utiliser la notation à doubles crochets :

x=10
if [[ $x -gt 5 && $x -lt 11 ]]; then
    echo "$x is more than 5 and less than 11!"
fi
Introduction à la programmation Bash

IF et programmes en ligne de commande

Vous pouvez aussi utiliser plusieurs programmes en ligne de commande directement dans la condition, sans crochets.

Par exemple, si le fichier words.txt contient « Hello World! » :

if grep -q Hello words.txt; then
    echo "Hello is inside!"
fi
Hello is inside!
Introduction à la programmation Bash

IF avec sous-shell

Vous pouvez aussi appeler un sous-shell dans votre condition.

Réécrivons l'exemple précédent ; le résultat sera le même.

if $(grep -q Hello words.txt); then
    echo "Hello is inside!"
fi
Hello is inside!
Introduction à la programmation Bash

Passons à la pratique !

Introduction à la programmation Bash

Preparing Video For Download...