# Conditions
## IF
### if
```bash
if [ $foo -ge 3 ]; then
... (si $foo >= 3)
elif [ $foo -lt 3 ]; then
... (si $foo < 3)
else
...
fi
if [ $foo -ge 3 ]
then
... (si $foo >= 3)
elif [ $foo -lt 3 ]
then
... (si $foo < 3)
else
...
fi
```
### if (one-line)
```bash
if [ "$choice" == "72" ]; then echo "sphp 72"; elif [ "$choice" == "73" ]; then echo "sphp 73"; fi
```
### if imbriqué
```bash
if [ $value -ge 1 ]
then
if [ $foo -eq 1 ]
then
echo "One"
elif [ $foo -eq 2 ]
then
echo "Two"
elif [ $foo -eq 3 ]
then
echo "Three"
else
echo "Bigger than two"
fi
fi
```
### AND & OR
```bash
if [[ -n $1 ]] && [[ -r $1 ]]
then
echo "File exists and is readable"
fi
if [[ -z $1 ]] || [[ ! -r $1 ]]
then
echo "Either you didn't give me a value or file is unreadable"
exit 2
fi
if [ $num -gt 100 ] && [ $num -lt 200 ]
then
echo "The number lies between 100 and 200"
fi
```
### Conditions:
#### -fichier
Si le fichier *existingfile* existe
```bash
if [ -a existingfile ]; then
ou
if [ -e existingfile ]; then
if [ -a tmp.tmp ]; then
```
Si le fichier *blockspecialfile (/dev/fd0)* existe
```bash
if [ -b blockspecialfile ]; then
if [ -b /dev/fd0 ]; then
```
Si le fichier *characterspecialfile (/dev/null tty)* existe
```bash
if [ -c characterspecialfile ]; then
if [ -c /dev/dsp ]; then
```
Si le répertoire *directory* existe
```bash
if [ -d directory ]; then
if [ -d ~/.kde ]; then
```
Si le fichier *regularfile (ni un blockspecialfile, ni un characterspecialfile, ni un directory)* existe
```bash
if [ -f regularfile ]; then
if [ -f ~/.bashrc ]; then
```
Si le fichier *sgidfile (set-group-ID)* existe
```bash
if [ -g sgidfile ]; then
if [ -g . ]; then
```
Si le fichier *fileownedbyeffectivegroup* existe
```
if [ -G fileownedbyeffectivegroup ]; then
if [ ! -G file ]; then
```
Si le fichier *symboliclink (symbolic link)* existe
```bash
if [ -h symboliclink ]; then
ou
if [ -L symboliclink ]; then
if [ -h $pathtofile ]; then
```
Si le fichier *stickyfile (sticky bit set)* existe
```bash
if [ -k stickyfile ]; then
if [ ! -k /tmp ]; then
```
Si le fichier *modifiedsincelastread* *(a été modifié aprèès la dernière lecture)* existe
```bash
if [ -N modifiedsincelastread ]; then
if [ -N /etc/crontab ]; then
```
Si le fichier *fileownedbyeffectiveuser* *(si l'utilisateur exécutant le script le possède)* existe
```bash
if [ -O fileownedbyeffectiveuser ]; then
if [ -O file ]; then
```
Si le fichier *namedpipe* existe
```bash
if [ -p namedpipe ]; then
if [ -p $file ]; then
```
Si le fichier *readablefile (droit lecture)* existe
```bash
if [ -r readablefile]; then
if [-r file ]; then
```
Si le fichier *noemptyfile (taille > 0 octet)* existe
```bash
if [ -s nonemptyfile ]; then
if [ -s logfile ]; then
```
Si le fichier *socket* existe
```bash
if [ -S socket ]; then
if [ -S /var/lib/mysql/mysql.sock ]; then
```
Si le fichier *openterminal* existe
```bash
if [ -t openterminal ]; then
if [ -t /dev/pts/3 ]; then
```
Si le fichier *suidfile (set-user-ID)* existe
```bash
if [ -u suidfile ]; then
if [ -u executable ];
```
Si le fichier *writeablefile (droit écriture)* existe
```bash
if [ -w writeablefile ]; then
if [ -w /dev/hda ]; then
```
Si le fichier *executablefile (droit exécutable)* existe
```bash
if [ -x executablefile ]; then
if [ -x /root ];
```
Si le fichier *newerfile a été modifié après olderfile*, ou *si newerfile existe et pas olderfile.*
```bash
if [ newerfile -nt olderfile ]; then
if [ story.txt1 -nt story.txt ];
```
Si le fichier *olderfile a été modifié avant newerfile*, ou *si newerfile existe et pas olderfile.*
```bash
if [ olderfile -ot newerfile ]; then
if [ /mnt/remote/remotefile -ot localfile ]; then
```
Si les fichiers *same et file font référence au même device / inode*.
```bash
if [ same -ef file ]; then
if [ /dev/cdrom -ef /dev/dvd ]; then
```
Si un fichier a été modifié durant les 5 dernières minutes.
```bash
dir="/usr/local/etc/httpd"
name="httpd.conf"
test=$(find $dir -name "$name" -mmin -5 -maxdepth 1)
[ ! -z $test ] && echo -e "\033[1;31m❗️ ️$name was modified in the last 5 minutes\033[0m"
```
#### -chaine
Si les chaines sont *identiques* [ STRING1 == STRING2 ]
```bash
if [ “$1” = “moo” ]; then
```
```bash
if [[ “$1” == “moo” ]]; then
```
Si les chaines sont *différentes* [ STRING1 != STRING2 ]
```bash
if [ “$userinput” != “$password” ]; then
```
Si la chaine 1 *contient la sous-chaine* chaine 2 [ STRING1 != STRING2 ]
```bash
if [ “$userinput” == *“$password”* ]; then
if [ “$userinput” == “$password”* ]; then
if [ “$userinput” == *“$password” ]; then
```
```bash
if [ “$userinput” =~ .*$password.* ]; then
```
Si la chaine 1 *est triée après* la chaine 2 [ STRING1 > STRING2 ]
```bash
if [ “$userinput” > “$password” ]; then
```
Si la chaine 1 *est triée avant* la chaine 2 [ STRING1 < STRING2 ]
```bash
if [ “$userinput” < “$password” ]; then
```
Si la chaine *NONEMPTYSTRING a une longueur > 0* (contient 1 ou plusieurs caractères)
```bash
if [ -n NONEMPTYSTRING ]; then
if [ -n “$userinput” ]; then
```
Si la chaine *EMPTYSTRING est vide* (NULL)
```bash
if [ -z EMPTYSTRING ]; then
if [ -z $uninitializedvar ]; then
```
Si la chaine réussie la REGEX [[ STRING1 =~ REGEXPATTERN ]]
```bash
if [[ “$email” =~ “b[A-Za-z0-9._%+-]+@[A-Za-z0-9.-]+.[A-Za-z]{2,4}b” ]]; then
```
#### -nombre
Si deux nombres sont égaux (EQual) [ NUM1 -eq NUM2 ]
```bash
if [ $NUM1 -eq $NUM2 ]; then
```
Si deux nombres sont différents (Not Equal) [ NUM1 -ne NUM2 ]
```bash
if [ $NUM1 -ne $NUM2 ]; then
```
Si NUM 1 est supérieur à NUM 2 (Greater Than) [ NUM1 -gt NUM2 ]
```bash
if [ $NUM1 -gt $NUM2 ]; then
```
Si NUM 1 est supérieur ou égal à NUM 2 (Greater than or Equal) [ NUM1 -ge NUM2 ]
```bash
if [ $NUM1 -ge $NUM2 ]; then
```
Si NUM 1 est inférieur à NUM 2 (Less Than) [ NUM1 -lt NUM2 ]
```bash
if [ $NUM1 -lt $NUM2 ]; then
```
Si NUM 1 est inférieur ou égal à NUM 2 (Less than or Equal) [ NUM1 -le NUM2 ]
```bash
if [ $NUM1 -le $NUM2 ]; then
```
Exemples:
```bash
if [ $? -eq 0 ]; then # $? returns the exit status of the previous command
echo “Previous command ran succesfully.”
fi
if [ $(ps -p $pid -o ni=) -ne $(nice) ]; then
echo “Process $pid is running with a non-default nice value”
fi
if [ $num -lt 0 ]; then
echo “Negative numbers not allowed; exiting…”
exit 1
fi
```
#### -double parenthèses
Si deux nombres sont égaux (( NUM1 == NUM2 ))
```bash
if (( NUM1 == NUM2 )); then
```
Si deux nombres sont différents (( NUM1 != NUM2 ))
```bash
if (( NUM1 != NUM2 )); then
```
Si NUM 1 est supérieur à NUM 2 (( NUM1 > NUM2 ))
```bash
if (( NUM1 > NUM2 )); then
```
Si NUM 1 est supérieur ou égal à NUM 2 (( NUM1 >= NUM2 ))
```bash
if (( NUM1 >= NUM2 )); then
```
Si NUM 1 est inférieur à NUM 2 (( NUM1 < NUM2 ))
```bash
if (( NUM1 < NUM2 )); then
```
Si NUM 1 est inférieur ou égal à (( NUM1 <= NUM2 ))
```bash
if (( NUM1 <= NUM2 )); then
```