Common condition tests for shell

Common condition tests for shell

Here are some commonly used condition test tables in shells for your reference and reference

Different test types

There are three types of tests that can be done in Bash:

  • Test string
  • Test number
  • Test file

Test string

condition significance
$string1 = $string2 Whether the two strings are equal. Shell is case sensitive, so A and a are not the same.
$string1 != $string2 Whether the two strings are different.
-z $string Whether the string string is empty. z is the first letter of zero, which means "zero" in English.
-n $string Whether string is not empty. n is the first letter of English not, which means "not" in English.

example:

#!/bin/bash

if [ -z $1 ]
then
    echo "No parameter"
else
    echo "There is at least one parameter"
fi

Test string

condition significance
$num1 -eq $num2 Whether the two numbers are equal. It is not the same as the symbol (=) used to determine the character string. eq is the abbreviation of equal, which means "equal" in English.
$ num1 -ne $ num2 Whether the two numbers are different. ne is the abbreviation of not equal, which means "not equal" in English.
$num1 -lt $num2 Whether the number num1 is less than num2. lt is the abbreviation of lower than, which means "less than" in English.
$ num1 -le $ num2 Whether the number num1 is less than or equal to num2. le is the abbreviation of lower or equal, which means "less than or equal to" in English.
$num1 -gt $num2 Whether the number num1 is greater than num2. gt is the abbreviation of greater than, which means "greater than" in English.
$num1 -ge $num2 Whether the number num1 is greater than or equal to num2. Ge is the abbreviation of greater or equal, which means "greater than or equal" in English.

example:

#!/bin/bash

if [ $1 -ge 10 ]
then
    echo "You have entered a number greater than 10 or equal to 10"
else
    echo "You have entered a number lower than 10"
fi

Test file

condition significance
-e $ file Whether the file exists. e is the first letter of exist, which means "exist".
-d $file Whether the file is a directory. Because everything in Linux is a file, a directory is also a kind of file. d is the first letter of directory, which means "directory".
-f $file Whether the file is a file. f is the first letter of file, which means "file".
-L $file Whether the file is a symbolic link file. L is the first letter of link, which means "link".
-r $file Whether the file is readable. r is the first letter of readable, which means "readable".
-w $file Whether the file is writable. w is the first letter of writable, which means "writable".
-x $file Whether the file is executable. x is the first letter of executable, which means "executable".
$ file1 -nt $ file2 Whether file file1 is newer than file2. nt is the abbreviation of newer than, which means "newer".
$ file1 -from $ file2 Whether file file1 is older than file2. ot is the abbreviation of older than, which means "older".

example:

#!/bin/bash

read -p 'Enter a directory : ' file

if [ -d $file ]
then
    echo "$file is a directory"
else
    echo "$file is not a directory"
fi

————————————————————————————————————————————————
Reference: Linux Command Line and Shell Script Programming Encyclopedia/43 When the condition comes out, Shell is not satisfied

Guess you like

Origin blog.csdn.net/rjszz1314/article/details/104487269