1er commit
De la docs au format Mkdocs
This commit is contained in:
402
docs/Linux/conditions.md
Normal file
402
docs/Linux/conditions.md
Normal file
@@ -0,0 +1,402 @@
|
||||
# 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 imbriqué
|
||||
|
||||
```bash
|
||||
if [ $value -ge 1 ]
|
||||
then
|
||||
|
||||
if [ $foo -eq 1 ]
|
||||
then
|
||||
echo "One"
|
||||
elif [ $foo -eq 2 ]
|
||||
then
|
||||
echo "Two"
|
||||
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
|
||||
```
|
||||
|
||||
|
||||
|
||||
### Conditions:
|
||||
|
||||
#### -fichier
|
||||
|
||||
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 répertoire *<u>directory</u>* existe
|
||||
|
||||
```bash
|
||||
if [ -d directory ]; then
|
||||
|
||||
if [ -d ~/.kde ]; then
|
||||
```
|
||||
|
||||
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>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
|
||||
```
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
#### -chaine
|
||||
|
||||
Si les chaines sont <u>*identiques*</u> [ STRING1 == STRING2 ]
|
||||
|
||||
```bash
|
||||
if [ “$1” == “moo” ]; then
|
||||
```
|
||||
|
||||
Si les chaines sont <u>*différentes*</u> [ STRING1 != STRING2 ]
|
||||
|
||||
```bash
|
||||
if [ “$userinput” != “$password” ];
|
||||
```
|
||||
|
||||
Si la chaine 1 <u>*est triée après*</u> la chaine 2 [ STRING1 > STRING2 ]
|
||||
|
||||
```bash
|
||||
if [ “$userinput” > “$password” ];
|
||||
```
|
||||
|
||||
Si la chaine 1 <u>*est triée avant*</u> la chaine 2 [ STRING1 < STRING2 ]
|
||||
|
||||
```bash
|
||||
if [ “$userinput” < “$password” ];
|
||||
```
|
||||
|
||||
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
|
||||
```
|
||||
|
||||
|
||||
|
||||
#### -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
|
||||
```
|
||||
|
||||
Reference in New Issue
Block a user