# [Bash] Kit de base

## En-tête

```bash
#!/bin/bash
```

## Variables

Il existe 2 types de variables en bash :

- Les **variables traditionnelles** définies par l'utilisateur. Leur identifiant doit être en <span style="text-decoration: underline;">minuscule</span>.
- Les **variables d'environnements** qui sont définies par le système. Leur identifiant doit être en <span style="text-decoration: underline;">majuscule.</span>

#### Définition et affectation de variables

Valeur entière :

```bash
myVar=3
```

Chaîne de caractères :

```bash
myVar="Hello world !"
```

Liste :

```bash
myArray=("cat" "dog" "mouse" "frog")
```

Sortie d'une commande :

```bash
myVar=$( echo "test" )
```

#### Utilisation des variables

Pour accéder à la valeur stockée dans une variable on utilise le caractère **$** :

```bash
echo $myVar
```

## Conditions

#### If

```bash
if [ <some test> ]
then
    <commands>
fi
```

#### If / Else

```bash
if [ <some test> ]
then
    <commands>
else
    <other commands>
fi
```

####  If / Elif / Else  


```bash
if [ <some test> ]
then
    <commands>
elif [ <some test> ]
then
    <different commands>
else
    <other commands>
fi
```

#### Case

```bash
case <variable> in
<pattern 1>)
    <commands>
;;
<pattern 2>)
    <other commands>
;;
esac
```

#### Tests

<table border="1" class="fancy" id="bkmrk-operator-description" style="border-collapse: collapse; width: 69.8766%; border-style: solid;"><tbody><tr><th class="align-center" style="width: 29.7345%;">Operator</th><th class="align-center" style="width: 70.2655%;">Description</th></tr><tr><td class="align-center" style="width: 29.7345%;">! EXPRESSION</td><td class="align-center" style="width: 70.2655%;">The EXPRESSION is false.</td></tr><tr><td class="align-center" style="width: 29.7345%;">-n STRING</td><td class="align-center" style="width: 70.2655%;">The length of STRING is greater than zero.</td></tr><tr><td class="align-center" style="width: 29.7345%;">-z STRING</td><td class="align-center" style="width: 70.2655%;">The lengh of STRING is zero (ie it is empty).</td></tr><tr><td class="align-center" style="width: 29.7345%;">STRING1 = STRING2</td><td class="align-center" style="width: 70.2655%;">STRING1 is equal to STRING2</td></tr><tr><td class="align-center" style="width: 29.7345%;">STRING1 != STRING2</td><td class="align-center" style="width: 70.2655%;">STRING1 is not equal to STRING2</td></tr><tr><td class="align-center" style="width: 29.7345%;">INTEGER1 -eq INTEGER2</td><td class="align-center" style="width: 70.2655%;">INTEGER1 is numerically equal to INTEGER2</td></tr><tr><td class="align-center" style="width: 29.7345%;">INTEGER1 -gt INTEGER2</td><td class="align-center" style="width: 70.2655%;">INTEGER1 is numerically greater than INTEGER2</td></tr><tr><td class="align-center" style="width: 29.7345%;">INTEGER1 -lt INTEGER2</td><td class="align-center" style="width: 70.2655%;">INTEGER1 is numerically less than INTEGER2</td></tr><tr><td class="align-center" style="width: 29.7345%;">-d FILE</td><td class="align-center" style="width: 70.2655%;">FILE exists and is a directory.</td></tr><tr><td class="align-center" style="width: 29.7345%;">-e FILE</td><td class="align-center" style="width: 70.2655%;">FILE exists.</td></tr><tr><td class="align-center" style="width: 29.7345%;">-r FILE</td><td class="align-center" style="width: 70.2655%;">FILE exists and the read permission is granted.</td></tr><tr><td class="align-center" style="width: 29.7345%;">-s FILE</td><td class="align-center" style="width: 70.2655%;">FILE exists and it's size is greater than zero (ie. it is not empty).</td></tr><tr><td class="align-center" style="width: 29.7345%;">-w FILE</td><td class="align-center" style="width: 70.2655%;">FILE exists and the write permission is granted.</td></tr><tr><td class="align-center" style="width: 29.7345%;">-x FILE</td><td class="align-center" style="width: 70.2655%;">FILE exists and the execute permission is granted.</td></tr></tbody></table>

#### Opérations booléennes

On peut combiner les tests grâce aux opérateurs suivants :

<table border="1" id="bkmrk-et-ou-%26%26-%7C%7C" style="border-collapse: collapse; width: 100%; height: 59.6px;"><colgroup><col style="width: 50%;"></col><col style="width: 50%;"></col></colgroup><tbody><tr style="height: 29.8px;"><td class="align-center" style="height: 29.8px;">ET  
</td><td class="align-center" style="height: 29.8px;">OU  
</td></tr><tr style="height: 29.8px;"><td class="align-center" style="height: 29.8px;">&amp;&amp;  
</td><td class="align-center" style="height: 29.8px;">||</td></tr></tbody></table>

Exemple :

```bash
if [ -r $1 ] && [ -s $1 ]
then
    echo This file is useful.
fi
```

## Boucles

#### While

```bash
while [ <some test> ]
do
    <commands>
done
```

#### For

Pour parcourir une liste, on utilise une boucle for où la variable **var** sera l'élément sélectionné de la liste à traiter :

```bash
for var in <$list>
do
    <commands>
done
```

 Pour une "range" on fera le type de boucle for suivant :

```bash
for value in {1..5}
do
    echo $value
done
```

## Cacher la sortie d'une commande

```bash
<CMD> > /dev/null 2>&1
```

## Obtenir la date

Format **Heure-Minute-Jour-Mois-Année** :

```bash
currentDate=$(date +'%H:%M-%d-%m-%Y')
```

Format **Jour-Mois-Année-Heure-Minute** :

```bash
currentDate=$(date +'%d-%m-%Y-%H:%M')
```

## Afficher un espace sans faire d'espace

Cela peut s'avérer pratique notamment dans un contexte où les espaces ne sont pas autorisés mais que vous devez exécuter une commande avec des arguments (et donc des espaces).

Pour cela, on utilise la variable d'environnement **{IFS}** :

```bash
echo${IFS}
```