Files
mkdocs/docs/Linux/conditions.md
2021-08-06 13:32:29 +02:00

485 lines
8.1 KiB
Markdown
Raw Blame History

This file contains invisible Unicode characters
This file contains invisible Unicode characters that are indistinguishable to humans but may be processed differently by a computer. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
# 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
```
### Ternaire
```bash
[ "$nb" -gt 1 ] && echo -e "condition ok" || echo -e "condition nok"
```
### 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 répertoire *<u>directory</u>* existe
```bash
if [ -d directory ]; then
if [ -d ~/.kde ]; then
```
Si le répertoire *<u>directory</u>* existe ET n'est <u>pas vide</u>
```bash
local_path=$HOME/Sites/
if find "$local_path/node_modules" -mindepth 1 -maxdepth 1 | read; then echo "dir not empty"; else echo "dir empty"; fi
if [ -d "$local_path/node_modules" ] && [ -n "$(ls -A "$local_path/node_modules")" ]; then echo "dir not empty"; else echo "dir empty"; fi
```
Si le fichier *<u>regularfile</u> (ni un blockspecialfile, ni un characterspecialfile, ni un directory)* existe
```bash
if [ -f regularfile ]; then
if [ -f ~/.bashrc ]; then
```
Si le fichier *<u>existingfile</u>* existe
```bash
if [ -a existingfile ]; then
ou
if [ -e existingfile ]; then
if [ -a tmp.tmp ]; then
```
Si le fichier *<u>blockspecialfile</u> (/dev/fd0)* existe
```bash
if [ -b blockspecialfile ]; then
if [ -b /dev/fd0 ]; then
```
Si le fichier *<u>characterspecialfile</u> (/dev/null tty)* existe
```bash
if [ -c characterspecialfile ]; then
if [ -c /dev/dsp ]; then
```
Si le fichier *<u>sgidfile</u> (set-group-ID)* existe
```bash
if [ -g sgidfile ]; then
if [ -g . ]; then
```
Si le fichier *<u>fileownedbyeffectivegroup</u>* existe
```
if [ -G fileownedbyeffectivegroup ]; then
if [ ! -G file ]; then
```
Si le fichier *<u>symboliclink</u> (symbolic link)* existe
```bash
if [ -h symboliclink ]; then
ou
if [ -L symboliclink ]; then
if [ -h $pathtofile ]; then
```
Si le fichier *<u>stickyfile</u> (sticky bit set)* existe
```bash
if [ -k stickyfile ]; then
if [ ! -k /tmp ]; then
```
Si le fichier *<u>modifiedsincelastread</u>* *(a été modifié aprèès la dernière lecture)* existe
```bash
if [ -N modifiedsincelastread ]; then
if [ -N /etc/crontab ]; then
```
Si le fichier *<u>fileownedbyeffectiveuser</u>* *(si l'utilisateur exécutant le script le possède)* existe
```bash
if [ -O fileownedbyeffectiveuser ]; then
if [ -O file ]; then
```
Si le fichier *<u>namedpipe</u>* existe
```bash
if [ -p namedpipe ]; then
if [ -p $file ]; then
```
Si le fichier *<u>readablefile</u> (droit lecture)* existe
```bash
if [ -r readablefile]; then
if [-r file ]; then
```
Si le fichier *<u>noemptyfile</u> (taille > 0 octet)* existe
```bash
if [ -s nonemptyfile ]; then
if [ -s logfile ]; then
```
Si le fichier *<u>socket</u>* existe
```bash
if [ -S socket ]; then
if [ -S /var/lib/mysql/mysql.sock ]; then
```
Si le fichier *<u>openterminal</u>* existe
```bash
if [ -t openterminal ]; then
if [ -t /dev/pts/3 ]; then
```
Si le fichier *<u>suidfile</u> (set-user-ID)* existe
```bash
if [ -u suidfile ]; then
if [ -u executable ];
```
Si le fichier *<u>writeablefile</u> (droit écriture)* existe
```bash
if [ -w writeablefile ]; then
if [ -w /dev/hda ]; then
```
Si le fichier *<u>executablefile</u> (droit exécutable)* existe
```bash
if [ -x executablefile ]; then
if [ -x /root ];
```
Si le fichier *<u>newerfile a été modifié après olderfile</u>*, ou *<u>si newerfile existe et pas olderfile</u>.*
```bash
if [ newerfile -nt olderfile ]; then
if [ story.txt1 -nt story.txt ];
```
Si le fichier *<u>olderfile a été modifié avant newerfile</u>*, ou <u>*si newerfile existe et pas olderfile.*</u>
```bash
if [ olderfile -ot newerfile ]; then
if [ /mnt/remote/remotefile -ot localfile ]; then
```
Si les fichiers <u>*same et file font référence au même device / inode*</u>.
```bash
if [ same -ef file ]; then
if [ /dev/cdrom -ef /dev/dvd ]; then
```
Si un fichier a été <u>modifié durant les 5 dernières minutes</u>.
```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 <u>*identiques*</u> [ STRING1 == STRING2 ]
```bash
if [ "$1" = "moo" ]; then
```
```bash
if [[ "$1" == "moo" ]]; then
```
Si les chaines sont <u>*différentes*</u> [ STRING1 != STRING2 ]
```bash
if [ "$userinput" != "$password" ]; then
```
Si la chaine 1 *<u>contient la sous-chaine</u>* chaine 2
```bash
if [ "$userinput" == *"$password"* ]; then
if [ "$userinput" == "$password"* ]; then
if [ "$userinput" == *"$password" ]; then
```
```bash
if [ "$userinput" =~ .*$password.* ]; then
```
Si la chaine 1 <u>*est triée après*</u> la chaine 2 [ STRING1 > STRING2 ]
```bash
if [ "$userinput" > "$password" ]; then
```
Si la chaine 1 <u>*est triée avant*</u> la chaine 2 [ STRING1 < STRING2 ]
```bash
if [ "$userinput" < "$password" ]; then
```
Si la chaine <u>*NONEMPTYSTRING a une longueur > 0*</u> (contient 1 ou plusieurs caractères)
```bash
if [ -n NONEMPTYSTRING ]; then
if [ -n "$userinput" ]; then
```
Si la chaine <u>*EMPTYSTRING est vide*</u> (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
```
Si la chaine commence par '#'
```bash
if [[ ${string:0:1} == '#' ]]; then
```
#### -nombre
Si deux nombres sont <u>égaux</u> (EQual) [ NUM1 -eq NUM2 ]
```bash
if [ $NUM1 -eq $NUM2 ]; then
```
Si deux nombres sont <u>différents</u> (Not Equal) [ NUM1 -ne NUM2 ]
```bash
if [ $NUM1 -ne $NUM2 ]; then
```
Si NUM 1 est <u>supérieur à</u> NUM 2 (Greater Than) [ NUM1 -gt NUM2 ]
```bash
if [ $NUM1 -gt $NUM2 ]; then
```
Si NUM 1 est <u>supérieur ou égal à</u> NUM 2 (Greater than or Equal) [ NUM1 -ge NUM2 ]
```bash
if [ $NUM1 -ge $NUM2 ]; then
```
Si NUM 1 est <u>inférieur à</u> NUM 2 (Less Than) [ NUM1 -lt NUM2 ]
```bash
if [ $NUM1 -lt $NUM2 ]; then
```
Si NUM 1 est <u>inférieur ou égal à</u> 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 <u>égaux</u> (( NUM1 == NUM2 ))
```bash
if (( NUM1 == NUM2 )); then
```
Si deux nombres sont <u>différents</u> (( NUM1 != NUM2 ))
```bash
if (( NUM1 != NUM2 )); then
```
Si NUM 1 est <u>supérieur à</u> NUM 2 (( NUM1 > NUM2 ))
```bash
if (( NUM1 > NUM2 )); then
```
Si NUM 1 est <u>supérieur ou égal à</u> NUM 2 (( NUM1 >= NUM2 ))
```bash
if (( NUM1 >= NUM2 )); then
```
Si NUM 1 est <u>inférieur à</u> NUM 2 (( NUM1 < NUM2 ))
```bash
if (( NUM1 < NUM2 )); then
```
Si NUM 1 est <u>inférieur ou égal à</u> (( NUM1 <= NUM2 ))
```bash
if (( NUM1 <= NUM2 )); then
```
#### -test si la variable est un entier/decimal/chaine:
```bash
#!/bin/bash
read -p "Type a number or a string: " input
if [[ $input =~ ^[+-]?[0-9]+$ ]]; then
echo "Input is an integer."
elif [[ $input =~ ^[+-]?[0-9]+\.$ ]]; then
echo "Input is a string."
elif [[ $input =~ ^[+-]?[0-9]+\.?[0-9]*$ ]]; then
echo "Input is a float."
else
echo "Input is a string."
fi
```