I. Overview
The Shell, like other programming languages, supports a variety of operators, including:
- Arithmetic operators
- Relational operators
- Boolean operator
- String operators
- File Test Operators
Second, arithmetic operators
Native bash does not support simple math operations, but can be implemented with other commands, such as awk and expr,expr, which are most commonly used.
Expr is an expression evaluation tool that uses it to perform evaluation operations on expressions.
#!/bin/bashval= 'expr22'echo' two number of sum: $val "
Output
4
Two points Note:
- There is a space between the expression and the operator, for example, the 2 + 2 must be written.
- The complete expression is to be contained, note that this character is not a common single quote, below the ESC key.
The following table lists the commonly used arithmetic operators, assuming that variable A is 10 and variable B is 20:
operator |
Description |
Example |
+ |
Addition |
The ' expr $a + $b ' result is 30. |
- |
Subtraction |
' Expr $a-$b ' result is-10. |
* |
Multiplication |
The ' expr $a \* $b ' result is 200. |
/ |
Division |
The ' expr $b/$a ' result is 2. |
% |
Take surplus |
The ' expr $b% $a ' result is 0. |
= |
Assign value |
A= $b assigns the value of variable B to a. |
== |
Equal. Used to compare two numbers, the same returns true. |
[$a = = $b] returns FALSE. |
!= |
Not equal. Used to compare two numbers, and returns true if they are different. |
[$a! = $b] Returns TRUE. |
Note: conditional expressions are placed between square brackets and have spaces, for example: [$a = = $b] is wrong and must be written as [$a = = $b].
Attention:
- Multiplication sign (*) must be added in front of the backslash (\) to achieve the multiplication operation;
- The expr syntax for the shell in MAC is:$ (expression), where "*" in the expression does not need to escape the symbol "\".
Third, relational operators
Relational operators only support numbers, and strings are not supported unless the value of the string is a number.
The following table lists the commonly used relational operators, assuming that variable A is 10 and variable B is 20:
operator |
Description |
Example |
-eq |
Detects whether two numbers are equal and returns true for equality. |
[$a-eq $b] returns false. |
-ne |
Detects whether two numbers are not equal and returns true. |
[$a-ne $b] returns TRUE. |
-gt |
Detects if the number on the left is greater than the right and, if so, returns True. |
[$a-gt $b] returns false. |
-lt |
Detects if the number on the left is less than the right and, if so, returns True. |
[$a-lt $b] returns TRUE. |
-ge |
Detects if the number on the left is greater than or equal to the right, and returns true if it is. |
[$a-ge $b] returns false. |
-le |
Detects if the left number is less than or equal to the right, and returns true if it is. |
[$a-le $b] returns TRUE. |
Four, Boolean operators
The following table lists the commonly used Boolean operators, assuming that variable A is 10 and variable B is 20:
operator |
Description |
Example |
! |
Non-operation, the expression is true returns False, otherwise true. |
[! false] returns TRUE. |
-O |
Or operation, there is an expression of true to return true. |
[$a-lt 20-o $b-GT 100] returns TRUE. |
-A |
With an operation, two expressions are true to return true. |
[$a-lt 20-a $b-GT 100] returns FALSE. |
Five, logical operators
The following is a description of the Shell's logical operators, assuming that variable A is 10 and variable B is 20:
operator |
Description |
Example |
&& |
Logical AND |
[[$a-lt && $b-GT 100]] returns false |
|| |
Logical OR |
[[$a-lt | | $b-GT 100]] returns True |
Six, string operators
The following table lists the commonly used string operators, assuming that variable a is "ABC" and Variable B is "EFG":
operator |
Description |
Example |
= |
Detects whether two strings are equal and returns true for equality. |
[$a = $b] returns FALSE. |
!= |
Detects whether two strings are equal and returns true if they are not equal. |
[$a! = $b] Returns TRUE. |
-Z |
Detects whether the string length is 0 and returns true for 0. |
[-Z $a] returns false. |
-N |
Detects whether the string length is 0 and does not return true for 0. |
[-N $a] returns true. |
Str |
Detects whether the string is empty and does not return true for null. |
[$a] returns TRUE. |
SevenFile Test Operators
File test operators are used to detect various properties of Unix files.
Attribute detection is described as follows:
operator |
Description |
Example |
-B File |
Detects if the file is a block device file, and returns True if it is. |
[-B $file] returns FALSE. |
-C file |
Detects if the file is a character device file, and returns True if it is. |
[-C $file] returns false. |
-D File |
Detects if the file is a directory, and returns True if it is. |
[-D $file] returns false. |
-F File |
Detects if the file is a normal file (neither a directory nor a device file), and returns True if it is. |
[-F $file] returns TRUE. |
-G file |
Detects if the file has a SGID bit set, and returns True if it is. |
[-G $file] returns false. |
-K File |
Detects if the file has a sticky bit set (Sticky bit), and returns True if it is. |
[-K $file] returns false. |
-P File |
Detects if the file is a well-known pipe, and returns True if it is. |
[-P $file] returns false. |
-U file |
Detects if the file has a SUID bit set, and returns True if it is. |
[-U $file] returns false. |
-R File |
Detects if the file is readable and returns true if it is. |
[-R $file] returns TRUE. |
-W File |
Detects if the file is writable and returns true if it is. |
[-W $file] returns TRUE. |
-X File |
Detects if the file can be executed and, if so, returns True. |
[-X $file] returns TRUE. |
-S file |
Detects whether the file is empty (the file size is greater than 0) and does not return true for null. |
[-S $file] returns TRUE. |
-E File |
Detects whether the file (including the directory) exists and, if so, returns True. |
[-e $file] returns TRUE. |
Instance
The file "/var/www/runoob/test.sh", which has a size of 100 bytes, has rwx permissions. The following code will detect various properties of the file:
#!/bin/Bashfile="/var/www/runoob/test.sh"if[-R $file ] Then Echo "file readable"Else Echo "file is not readable"fiif[ -W$file ] Then Echo "file can be written"Else Echo "file is not writable"fiif[-X $file ] Then Echo "File Executable"Else Echo "file is not executable"fiif[-F $file ] Then Echo "file is a normal file"Else Echo "files are special files"fiif[-D $file ] Then Echo "file is a directory"Else Echo "file is not a directory"fiif[-S $file ] Then Echo "the file is not empty"Else Echo "file is empty"fiif[-E $file ] Then Echo "File exists"Else Echo "file does not exist"fi
Execute the script with the output as follows:
File readable file writable file executable file for normal file file not a directory file not empty file exists
004-shell basic operators, arithmetic operators, relational operators, Boolean operators, Series operators, string operators, file test operators