Files
mkdocs/docs/Linux/conditions.md
Bruno 21 e82296ba06 1er commit
De la docs au format Mkdocs
2018-09-16 14:48:15 +02:00

6.4 KiB

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 imbriqué

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

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 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 répertoire directory existe

if [ -d directory ]; then

if [ -d ~/.kde ]; then

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

-chaine

Si les chaines sont identiques [ STRING1 == STRING2 ]

if [$1== “moo” ]; then

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

if [$userinput” !=$password]; 

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

if [$userinput” > “$password]; 

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

if [$userinput” < “$password]; 

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

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 ]

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