The Shell, like other programming languages, supports a variety of operators, including:
Arithmetic operators
Relational operators
Boolean operator
String operators
File Test 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.
For example, two numbers are added (note the use of theanti-quote 'Instead of single quotes '):
#!/bin/sh
Val=' Expr 2 + 2 ' #注意 2 + 2 There are spaces between
echo "Sum of two numbers: $val"
Execute the script with the output as follows:
The sum of the two numbers is: 4
Two points Note:
There is a space between an expression and an operator, such as the 2 + 2, which is not the same as most of the programming languages we are familiar with.
The complete expression is to be contained, note that this character is not a common single quote, below the ESC key.
Arithmetic Operators
The following table lists the commonly used arithmetic operators, assuming that variable A is 10 and variable B is 20:
+ addition ' expr $a + $b ' result is 30.
-Subtraction ' expr $a-$b ' result is-10.
* multiplication ' expr $a \*$b ' result is 200.#注意反斜杠 \
/Division ' expr $b/$a ' result is 2.
% "Expr $b% $a ' result is 0.
= Assignment A= $b assigns the value of variable B to a.
= = Equal. Used to compare two numbers, the same returns true. [$a = = $b] returns FALSE.
! = is 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].
An example of an arithmetic operator is as follows:
#!/bin/bash
# Author: Rookie Tutorial
# url:www.runoob.com
a=10
B=20
Val= ' expr $a + $b '
echo "A + B: $val"
Val= ' expr $a-$b '
echo "A-B: $val"
Val= ' expr $a \* $b '
echo "A * B: $val"
Val= ' expr $b/$a '
echo "b/a: $val"
val= ' expr $b% $a '
echo "B% A: $val"
if [$a = = $b]
Then
echo "a equals B"
Fi
if [$a! = $b]
Then
echo "A is not equal to B"
Fi
Execute the script with the output as follows:
A + b:30
A-B: 10
A * b:200
B/A: 2
B% a:0
A is not equal to B
Note:
Multiplication sign (*) must be added in front of the backslash (\) to achieve the multiplication operation;
If...then...fi is a conditional statement that will be explained later.
The expr syntax for the shell in MAC is: $ (expression), where "*" in the expression does not need to escape the symbol "\".
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:
-eqDetects if two numbers areEqual, equal returns True. [$a-eq $b] returns false.
-neDetects if two numbers areNot Equal, not equal returns TRUE. [$a-ne $b] returns TRUE.
-GTDetects if the number on the left isGreater thanTo the right, if it is, returns True. [$a-gt $b] returns false.
-ltDetects if the number on the left isless thanTo the right, if it is, returns True. [$a-lt $b] returns TRUE.
-geDetects if the number on the left isgreater than or equalTo the right, if it is, returns True. [$a-ge $b] returns false.
-leDetects if the number on the left isless than or equalTo the right, if it is, returns True. [$a-le $b] returns TRUE.
The relational operator instance is as follows:
#!/bin/bash
# Author: Rookie Tutorial
# url:www.runoob.com
a=10
B=20
If [$a-eq $b]
Then
echo "$a-eq $b: a equals B"
Else
echo "$a-eq $b: A is not equal to B"
Fi
If [$a-ne $b]
Then
echo "$a-ne $b: A is not equal to B"
Else
echo "$a-ne $b: a equals B"
Fi
If [$a-gt $b]
Then
echo "$a-gt $b: a greater than B"
Else
echo "$a-gt $b: A is not much more than B"
Fi
If [$a-lt $b]
Then
echo "$a-lt $b: a less than B"
Else
echo "$a-lt $b: A is not less than B"
Fi
If [$a-ge $b]
Then
echo "$a-ge $b: A is greater than or equal to B"
Else
echo "$a-ge $b: a less than B"
Fi
If [$a-le $b]
Then
echo "$a-le $b: A is less than or equal to B"
Else
echo "$a-le $b: a greater than B"
Fi
Execute the script with the output as follows:
10-eq 20:a Not equal to B
10-ne 20:a Not equal to B
10-GT 20:a is not much more than B.
10-lt 20:a less than B
10-ge 20:a less than B
10-le 20:a less than or equal to B
Boolean operator
The following table lists the commonly used Boolean operators, assuming that variable A is 10 and variable B is 20:
! Non-operation, the expression is true to return false, otherwise true. [! false] returns TRUE.
- O orOperation, which returns true if there is an expression of true. [$a-lt 20-o $b-GT 100] returns TRUE.
-A andoperation, two expressions are true to return true. [$a-lt 20-a $b-GT 100] returns FALSE.
Examples of Boolean operators are as follows:
#!/bin/bash
# Author: Rookie Tutorial
# url:www.runoob.com
a=10
B=20
if [$a! = $b]
Then
echo "$a! = $b: A is not equal to B"
Else
echo "$a! = $b: a equals B"
Fi
If [$a-lt 100-a $b-gt 15]
Then
echo "$a less than 100 and $b greater than 15: Returns True"
Else
echo "$a less than 100 and $b greater than 15: returns false"
Fi
If [$a-lt 100-o $b-GT 100]
Then
echo "$a less than 100 or $b greater than 100: Returns True"
Else
echo "$a less than 100 or $b greater than 100: returns false"
Fi
If [$a-lt 5-o $b-GT 100]
Then
echo "$a less than 5 or $b greater than 100: Returns True"
Else
echo "$a less than 5 or $b greater than 100: returns false"
Fi
Execute the script with the output as follows:
Ten! = 20:a Not equal to B
10 is less than 100 and 20 is greater than 15: Returns True
10 is less than 100 or 20 is greater than 100: Returns True
10 less than 5 or 20 greater than 100: returns false
logical Operators
The following is a description of the Shell's logical operators, assuming that variable A is 10 and variable B is 20:
&&of logic and[[$a-lt && $b-GT 100]] returns false
|| of logicOR[[$a-lt | | $b-GT 100]] returns True
Examples of logical operators are:
#!/bin/bash
# Author: Rookie Tutorial
# url:www.runoob.com
a=10
B=20
if [[$a-lt 100&&$b-GT 100]]
Then
echo "Return True"
Else
echo "return False"
Fi
if [[$a-lt 100||$b-GT 100]]
Then
echo "Return True"
Else
echo "return False"
Fi
Execute the script with the output as follows:
Returns false
Returns True
string Operators
The following table lists the commonly used string operators, assuming that variable a is "ABC" and Variable B is "EFG":
= 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 if 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 if the string is empty and does not return true for null. [$a] returns TRUE.
Examples of string operators are as follows:
#!/bin/bash
# Author: Rookie Tutorial
# url:www.runoob.com
A= "ABC"
b= "EFG"
if [$a = $b]
Then
echo "$a = $b: a equals B"
Else
echo "$a = $b: A is not equal to B"
Fi
if [$a! = $b]
Then
echo "$a! = $b: A is not equal to B"
Else
echo "$a! = $b: a equals B"
Fi
If [-Z $a]
Then
echo "-Z $a: string length 0"
Else
echo "-Z $a: string length is not 0"
Fi
If [-N $a]
Then
echo "-N $a: string length not 0"
Else
echo "-N $a: string length 0"
Fi
If [$a]
Then
echo "$a: String not empty"
Else
echo "$a: string is empty"
Fi
Execute the script with the output as follows:
ABC = Efg:a Not equal to B
ABC! = EFG:A Not equal to B
-Z ABC: string length is not 0
-N ABC: string length is not 0
ABC: string is not empty
File Test Operators
File test operators are used to detect various properties of Unix files.
Attribute detection is described as follows:
The-B file detects whether the files are block device files and, if so, returns True. [-B $file] returns FALSE.
The-C file detects whether the files are character device files and, if so, returns True. [-C $file] returns false.
- D fileDetects if the file isCatalogue, if it is, returns True. [-D $file] returns false.
- F fileDetects if the file isNormal file(Neither a directory nor a device file), and if so, returns True. [-F $file] returns TRUE.
The-G file detects if the SGID bit is set and returns True if it is. [-G $file] returns false.
The-K file detects whether the files have a sticky bit set (Sticky bit), and returns True if it is. [-K $file] returns false.
The-P file detects if the files are named pipes and, if so, returns True. [-P $file] returns false.
-U file detects whether the file has a SUID bit set and returns True if it is. [-U $file] returns false.
The-R file detects whether the files are readable and, if so, returns True. [-R $file] returns TRUE.
The-W file detects whether the files are writable and, if so, returns True. [-W $file] returns TRUE.
The-X file detects whether files can be executed and, if so, returns True. [-X $file] returns TRUE.
-S file to detect whether the files are empty (the file size is greater than 0) and not NULL to return TRUE. [-S $file] returns TRUE.
- E filedetection files (including directories)is present, if it is, returns True. [-e $file] returns TRUE.
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/bash
# Author: Rookie Tutorial
# url:www.runoob.com
File= "/var/www/runoob/test.sh"
If [-R $file]
Then
echo "File readable"
Else
echo "File not readable"
Fi
If [w $file]
Then
echo "File writable"
Else
echo "File not writable"
Fi
If [-X $file]
Then
echo "File Executable"
Else
echo "File not executable"
Fi
If [-f $file]
Then
echo "File as normal file"
Else
echo "File as special file"
Fi
If [-D $file]
Then
echo "File is a directory"
Else
echo "File is not a directory"
Fi
If [-s $file]
Then
echo "File is not empty"
Else
echo "File is empty"
Fi
If [-e $file]
Then
echo "File exists"
Else
echo "file does not exist"
Fi
Execute the script with the output as follows:
File readable
File can be written
File executable
File is a normal file
File is not a directory
The file is not empty
File exists
7-shell operator