Tableaux en Bash

Introduction à la programmation Bash

Alex Scriven

Data Scientist

Qu'est-ce qu'un tableau ?

Deux types de tableaux en Bash :

  • Un tableau
    • Structure « normale » à index numériques.
    • Appelé « list » en Python ou « vector » en R.

En Python : my_list = [1,3,2,4]

En R : my_vector <- c(1,3,2,4)

Introduction à la programmation Bash

Créer un tableau en Bash

On peut créer un tableau à index numériques de deux façons en Bash.

1. Déclarer sans ajouter d'éléments

declare -a my_first_array

2. Créer et ajouter des éléments en même temps

my_first_array=(1 2 3)

À retenir : aucun espace autour du signe égal !

Introduction à la programmation Bash

Attention aux virgules !

Les virgules ne séparent pas les éléments d'un tableau en Bash :

Ceci n'est pas correct :

my_first_array=(1, 2, 3)

Ceci est correct :

my_first_array=(1 2 3)
Introduction à la programmation Bash

Propriétés importantes des tableaux

  • Tous les éléments d'un tableau se récupèrent avec array[@]. Notez que Bash exige des accolades autour du nom du tableau pour accéder à ces propriétés.
my_array=(1 3 5 2)
echo ${my_array[@]}
1 3 5 2
  • La longueur d'un tableau s'obtient avec #array[@]
echo ${#my_array[@]}
4
Introduction à la programmation Bash

Manipuler des éléments de tableau

Accéder aux éléments avec des crochets.

my_first_array=(15 20 300 42)
echo ${my_first_array[2]}
300
  • À retenir : Bash indexe à partir de zéro, comme Python (mais pas R !)
Introduction à la programmation Bash

Manipuler des éléments de tableau

Définir des éléments avec la notation par index.

my_first_array=(15 20 300 42 23 2 4 33 54 67 66)

my_first_array[0]=999
echo ${my_first_array[0]}
999
  • Rappel : n'utilisez pas $ lors d'une réaffectation d'index, p. ex. $my_first_array[0]=999, car cela ne fonctionne pas.
Introduction à la programmation Bash

Manipuler des éléments de tableau

Utilisez array[@]:N:M pour « tronquer » une partie du tableau.

  • Ici, N est l'index de départ et M le nombre d'éléments à retourner.
my_first_array=(15 20 300 42 23 2 4 33 54 67 66)
echo ${my_first_array[@]:3:2}
42 23
Introduction à la programmation Bash

Ajouter à des tableaux

Ajouter à un tableau avec array+=(elements).

Par exemple :

my_array=(300 42 23 2 4 33 54 67 66)

my_array+=(10) echo ${my_array[@]}
300 42 23 2 4 33 54 67 66 10
Introduction à la programmation Bash

(Ne pas) ajouter à des tableaux

Que se passe-t-il si vous n'ajoutez pas de parenthèses autour de ce que vous voulez ajouter ? Voyons.

Par exemple :

my_array=(300 42 23 2 4 33 54 67 66)
my_array+=10
echo ${my_array[@]}
30010 42 23 2 4 33 54 67 66

La chaîne 10 est simplement ajoutée au premier élément. Pas ce qu'on veut !

Introduction à la programmation Bash

Tableaux associatifs

  • Un tableau associatif
    • Comme un tableau normal, mais avec des paires clé‑valeur, pas des index numériques
    • Semblable au dictionnaire de Python ou à la liste de R
    • Note : offert seulement à partir de Bash 4. Certains Mac récents ont un vieux Bash ! Vérifiez avec bash --version dans le terminal

En Python :

my_dict = {'city_name': "New York", 'population': 14000000}

En R :

my_list = list(city_name = c('New York'), population = c(14000000))
Introduction à la programmation Bash

Créer un tableau associatif

On ne peut créer un tableau associatif qu'avec declare (et l'option -A en majuscules).

Vous pouvez déclarer d'abord, puis ajouter des éléments, ou tout faire sur une seule ligne.

  • Entourez les « clés » de crochets, puis associez une valeur après le signe égal.
    • Vous pouvez ajouter plusieurs éléments à la fois.
Introduction à la programmation Bash

Exemple de tableau associatif

Créons un tableau associatif :

declare -A city_details # Déclarer d'abord

city_details=([city_name]="New York" [population]=14000000) # Ajouter des éléments
echo ${city_details[city_name]} # Indexer avec la clé pour obtenir la valeur
New York
Introduction à la programmation Bash

Créer un tableau associatif

Autre option : créer et affecter un tableau associatif sur une seule ligne

  • Le reste est identique
declare -A city_details=([city_name]="New York" [population]=14000000)

Accéder aux « clés » d'un tableau associatif avec !

echo ${!city_details[@]} # Retourner toutes les clés
city_name population
Introduction à la programmation Bash

Passons à la pratique !

Introduction à la programmation Bash

Preparing Video For Download...