stay Bash We usually use test Command to check conditions .test The return value of the command is 0 or 1.0 express true, 1 express
false. For simplicity , We can directly think that test The result is true or false.

You can also use brackets instead test, Note that spaces must be left between parentheses and expressions , So the following two ways of writing are the same :
test "$1" = "$name" or [ "$1" = "$name" ]
test The main purpose of the command is to check the string , Compare integers and check file properties . This article will introduce you with a simple example test Common usage of commands .

Check string

Judge whether the string is empty

test When checking strings , Non empty string return true, Null string or no parameter returned false.
if test "$abc"; then echo "It's not an empty string." else echo "It's an empty
string." fi
The output of running the above code is :
It's an empty string.
That's because we don't define variables abc , therefore "$abc" Interpreted as an empty string .

In fact, it uses the operator to check the empty string -n It makes the code clearer :
if test -n "$abc"; then echo "It's not an empty string." else echo "It's an
empty string." fi
-n Means : The length of the string is not 0 Back on true. So the output of this code is the same as above .

If we need to judge an empty string, we can use the operator directly -z:
if test -z "$abc"; then echo "It's an empty string." fi
The output of this code is still :
It's an empty string.
Determine string equality

test Two operators are supported to judge the equality of strings := and ==.
abc="hello" if test "hello" == "$abc"; then echo "You got same string." fi
The output of running the above code is :
You got same string.
because = It looks more like an assignment , So I prefer to use ==. But when your scripts need to cross platform , It's better to use =(test In = yes POSIX Standard defined !).

If you want to judge that two strings are not equal , have access to != operator :
abc="helloo" if test "hello" != "$abc"; then echo "Your word is not 'hello'."
fi
The output of running the above code is :
Your word is not 'hello'.
Why wrap variables in double quotes ?

If you pay attention to the previous code, we will find that we use double quotes when referring to variables , This is about shell A trick in scripting . Try running the following code :
abc="hello xx" if test "hello" != $abc; then echo "Your word is not 'hello'."
fi
Syntax error will be prompted :



Because of variables abc The value of is "hello xx", There is a space in the middle of the string . So in Bash When interpreting an order , above test The command became :
test "hello" != hello xx
It's not legal test command , So when the script is executed, an error is reported !

It's not just spaces , Included in $IFS Other characters in , And when the variable is empty , Can cause syntax errors . So wrapping variables in double quotes is a protection mechanism , Can improve script robustness .

Compare numbers ( integer )

test Supports comparison between integers , The available operators are '-eq','-ne','-lt','-le','-gt','-ge'.
For example, it can be used to judge that two integers are equal -eq operator :
num=3 if test "$num" -eq 3; then echo "num is 3." fi
The output of running the above code is :
num is 3.
The meanings of common number operators are :
n1 -eq n2 : integer n1 be equal to n2 n1 -ne n2 : integer n1 Not equal to n2 n1 -lt n2 : n1 less than n2 n1 -gt
n2 : n1 greater than n2 n1-le n2 : n1 Less than or equal to n2 n1 -ge n2 : n1 Greater than or equal to n2
Check file properties

Checking the properties of a file is also test Common usage . For example, to check whether the type of a file is a normal file , have access to -f option :
touch test.sh filename="test.sh" if test -f "$filename"; then echo "It's a
regular file." fi
The output of running the above code is :
It's a regular file.
Another important option is -d, It is used to check whether a path is a directory :
dirname="/bin" if test -d "$dirname"; then echo "It's a directory." fi
The output of running the above code is :
It's a directory.
If it is only to judge whether a path exists , have access to -e operator . It only checks if the file exists and doesn't care about the specific type of file .

Here are some other common file checking operators :
-b file : File exists and is a block device file . -c file : File exists and is a character device file . -d file : File exists and is a directory . -e file
: File exists .-f file : File exists and is a general file . -g file : File exists and is set setgid position . -h file :
File exists and is a linked file .-p file : File exists and is a named pipe (FIFO). -r file : The file exists and is readable . -s file :
File exists and has content .-u file : File exists and is set setuid. -w file : The file exists and is writable . -x file :
File exists and is executable .-S file : The file exists and is a socket.
summary

Personal feelings test yes Bash Important command in . After you have mastered the basic scripting skills , adopt test Command and combine if
You can control the execution process of the program , To open the door of script programming .