Tableaux en Bash

Introduction au scripting Bash

Alex Scriven

Data Scientist

Qu'est-ce qu'un tableau ?

Deux types de tableaux en Bash :

  • Un tableau
    • Structure « normale » indexée numériquement.
    • 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 au scripting Bash

Créer un tableau en Bash

La création d'un tableau indexé numériquement se fait de deux façons en Bash.

  1. Déclarer sans ajouter d'éléments
declare -a my_first_array
  1. Créer et ajouter des éléments en même temps
my_first_array=(1 2 3)

À retenir : pas d'espace autour du signe égal !

Introduction au scripting 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 au scripting Bash

Propriétés clés des tableaux

  • Tous les éléments d'un tableau peuvent être renvoyés 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 au scripting Bash

Manipuler les éléments d'un tableau

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

my_first_array=(15 20 300 42)
echo ${my_first_array[2]}
300
  • À retenir : Bash utilise un index de départ zéro comme Python (contrairement à R !)
Introduction au scripting Bash

Manipuler les éléments d'un 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
  • À retenir : n'utilisez pas le $ lors d'une réaffectation d'index, par ex. $my_first_array[0]=999, cela ne fonctionnera pas.
Introduction au scripting Bash

Manipuler les éléments d'un tableau

Utilisez la notation array[@]:N:M pour « découper » un sous-ensemble.

  • 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 au scripting Bash

Ajouter à un tableau

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 au scripting Bash

Ne pas ajouter (correctement) à un tableau

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 concaténée au premier élément. Ce n'est pas ce que l'on veut !

Introduction au scripting Bash

Tableaux associatifs

  • Un tableau associatif
    • Semblable à un tableau normal, mais avec des paires clé‑valeur, pas des index numériques
    • Semblable au dictionnaire de Python ou à la list de R
    • Remarque : disponible uniquement à partir de Bash 4. Certains Mac récents ont un Bash ancien ! 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 au scripting Bash

Créer un tableau associatif

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

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 au scripting 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 par clé pour obtenir la valeur
New York
Introduction au scripting Bash

Créer un tableau associatif

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

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

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

echo ${!city_details[@]} # Renvoyer toutes les clés
city_name population
Introduction au scripting Bash

Passons à la pratique !

Introduction au scripting Bash

Preparing Video For Download...