Aller au contenu

Conditions

IF

if

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)

if [ "$choice" == "72" ]; then echo "sphp 72"; elif [ "$choice" == "73" ]; then echo "sphp 73"; fi

Ternaire

[ "$nb" -gt 1 ] && echo -e "condition ok" || echo -e "condition nok"

if imbriqué

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

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 directory existe

if [ -d directory ]; then

if [ -d ~/.kde ]; then

Si le répertoire directory existe ET n'est pas vide

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 regularfile (ni un blockspecialfile, ni un characterspecialfile, ni un directory) existe

if [ -f regularfile ]; then

if [ -f ~/.bashrc ]; then

Si le fichier existingfile existe

if [ -a existingfile ]; then
ou
if [ -e existingfile ]; then

if [ -a tmp.tmp ]; then

Si le fichier blockspecialfile (/dev/fd0) existe

if [ -b blockspecialfile ]; then

if [ -b /dev/fd0 ]; then

Si le fichier characterspecialfile (/dev/null tty) existe

if [ -c characterspecialfile ]; then

if [ -c /dev/dsp ]; then

Si le fichier sgidfile (set-group-ID) existe

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

if [ -h symboliclink ]; then
ou
if [ -L symboliclink ]; then

if [ -h $pathtofile ]; then

Si le fichier stickyfile (sticky bit set) existe

if [ -k stickyfile ]; then

if [ ! -k /tmp ]; then

Si le fichier modifiedsincelastread (a été modifié aprèès la dernière lecture) existe

if [ -N modifiedsincelastread ]; then

if [ -N /etc/crontab ]; then

Si le fichier fileownedbyeffectiveuser (si l'utilisateur exécutant le script le possède) existe

if [ -O fileownedbyeffectiveuser ]; then

if [ -O file ]; then

Si le fichier namedpipe existe

if [ -p namedpipe ]; then

if [ -p $file ]; then

Si le fichier readablefile (droit lecture) existe

if [ -r readablefile]; then

if [-r file ]; then

Si le fichier noemptyfile (taille > 0 octet) existe

if [ -s nonemptyfile ]; then

if [ -s logfile ]; then

Si le fichier socket existe

if [ -S socket ]; then

if [ -S /var/lib/mysql/mysql.sock ]; then

Si le fichier openterminal existe

if [ -t openterminal ]; then

if [ -t /dev/pts/3 ]; then

Si le fichier suidfile (set-user-ID) existe

if [ -u suidfile ]; then

if [ -u executable ];

Si le fichier writeablefile (droit écriture) existe

if [ -w writeablefile ]; then

if [ -w /dev/hda ]; then

Si le fichier executablefile (droit exécutable) existe

if [ -x executablefile ]; then

if [ -x /root ];

Si le fichier newerfile a été modifié après olderfile, ou si newerfile existe et pas olderfile.

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.

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.

if [ same -ef file ]; then

if [ /dev/cdrom -ef /dev/dvd ]; then

Si un fichier a été modifié durant les 5 dernières minutes.

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 ]

if [ "$1" = "moo" ]; then
if [[ "$1" == "moo" ]]; then

Si les chaines sont différentes [ STRING1 != STRING2 ]

if [ "$userinput" != "$password" ]; then

Si la chaine 1 contient la sous-chaine chaine 2

if [ "$userinput" == *"$password"* ]; then
if [ "$userinput" == "$password"* ]; then
if [ "$userinput" == *"$password" ]; then
if [ "$userinput" =~ .*$password.* ]; then

Si la chaine 1 est triée après la chaine 2 [ STRING1 > STRING2 ]

if [ "$userinput" > "$password" ]; then

Si la chaine 1 est triée avant la chaine 2 [ STRING1 < STRING2 ]

if [ "$userinput" < "$password" ]; then

Si la chaine NONEMPTYSTRING a une longueur > 0 (contient 1 ou plusieurs caractères)

if [ -n NONEMPTYSTRING ]; then

if [ -n "$userinput" ]; then

Si la chaine EMPTYSTRING est vide (NULL)

if [ -z EMPTYSTRING ]; then

if [ -z $uninitializedvar ]; then

Si la chaine réussie la REGEX [[ STRING1 =~ REGEXPATTERN ]]

if [[ "$email" =~ "b[A-Za-z0-9._%+-]+@[A-Za-z0-9.-]+.[A-Za-z]{2,4}b" ]]; then

Si la chaine commence par '#'

if [[ ${string:0:1} == '#' ]]; then

-nombre

Si deux nombres sont égaux (EQual) [ NUM1 -eq NUM2 ]

if [ $NUM1 -eq $NUM2 ]; then

Si deux nombres sont différents (Not Equal) [ NUM1 -ne NUM2 ]

if [ $NUM1 -ne $NUM2 ]; then

Si NUM 1 est supérieur à NUM 2 (Greater Than) [ NUM1 -gt NUM2 ]

if [ $NUM1 -gt $NUM2 ]; then

Si NUM 1 est supérieur ou égal à NUM 2 (Greater than or Equal) [ NUM1 -ge NUM2 ]

if [ $NUM1 -ge $NUM2 ]; then

Si NUM 1 est inférieur à NUM 2 (Less Than) [ NUM1 -lt NUM2 ]

if [ $NUM1 -lt $NUM2 ]; then

Si NUM 1 est inférieur ou égal à NUM 2 (Less than or Equal) [ NUM1 -le NUM2 ]

if [ $NUM1 -le $NUM2 ]; then

Exemples:

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 ))

if (( NUM1 == NUM2 )); then

Si deux nombres sont différents (( NUM1 != NUM2 ))

if (( NUM1 != NUM2 )); then

Si NUM 1 est supérieur à NUM 2 (( NUM1 > NUM2 ))

if (( NUM1 > NUM2 )); then

Si NUM 1 est supérieur ou égal à NUM 2 (( NUM1 >= NUM2 ))

if (( NUM1 >= NUM2 )); then

Si NUM 1 est inférieur à NUM 2 (( NUM1 < NUM2 ))

if (( NUM1 < NUM2 )); then

Si NUM 1 est inférieur ou égal à (( NUM1 <= NUM2 ))

if (( NUM1 <= NUM2 )); then

-test si la variable est un entier/decimal/chaine:

#!/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
# entier
if [ -z "${i//[0-9]}" ]; then

Dernière mise à jour: August 6, 2021