Common learning linux command summary 2- operators

1, arithmetic operators

Operators Explanation For example
+ addition `Expr $ a + $ b` 30 results.
- Subtraction `Expr $ a - $ b` result is 10.
* multiplication `Expr $ a \ * $ b` result is 200.
/ division `Expr $ b / $ a` 2 results.
% Remainder `Expr $ b% $ a` result is 0.
= Assignment $ a = b value will be assigned to the variable b a.
== equal. For comparing the two figures, the same returns true. [$ A == $ b] returns false.
!= not equal. Used to compare two numbers, the same is not true returns. [$ A! = $ B] returns true.

 

 

 

 

 

 

 

 

 

 

 

 

Precautions:

  • expr in the multiplication sign (*) must be added in front of a backslash (\) in order to achieve multiplication;
  • The conditional expression to be placed between square brackets, and include a space, for example, [$ a == $ b] is wrong and must be written [$ a == $ b].

2, relational operators

Operators Explanation For example
-eq Detecting whether the two numbers are equal, it returns true equal. [$ A -eq $ b] returns true.
-born Detecting whether the two numbers are equal, not equal Returns true. [$ A -ne $ b] returns true.
-gt Whether the number of detections is greater than the right to the left, and if so, it returns true. greater than [$ A -gt $ b] returns false.
-lt The number of detections is less than the right to the left, and if so, it returns true. less than [$ A -lt $ b] returns true.
-give Whether the number of detection equal to the right of the left side of the large, and if so, it returns true. [$ A -ge $ b] returns false.
-the Detecting whether the number is less than or equal to the right of the left, and if so, it returns true. [$ A -le $ b] returns true.

 

 

 

 

 

 

 

 

 

Note: The relational operators only support digital does not support string, unless the value is a string of numbers. 

3, Boolean operators

Operators Explanation For example
! Non-operational, the expression is true returns false, true otherwise. [! False] returns true.
-O Or operations, there is an expression that returns true then is true. or [$ A -lt 20 -o $ b -gt 100] return true.
-a And operations, the two expressions return true is true only. and [$ A -lt 20 -a $ b -gt 100] return false.

 

 

 

 

 

4, string operators

Operators Explanation For example
= Detecting whether the two strings are equal, it returns true equal. [$ A = $ b] returns false.
!= Detecting whether the two strings are equal, not equal Returns true. [$ A! = $ B] returns true.
-with Detecting whether the string length of 0, 0 returns true. [-Z $ a] returns false.
-n Detecting whether the string length is 0, 0 is not returns true. [-Z $ a] returns true.
str Detecting whether the string is empty, it does not return true empty. [$ A] returns true.



 

 

 

 

 

5, file operator

Operators Explanation For example
-b file Detecting whether the file is a device file block, if so, it returns true. [-B $ file] returns false.
-c file Detecting whether the file is a character file, and if so, it returns true. [-B $ file] returns false.
-d file Detecting whether the file is a directory, and if so, it returns true.  dictionary [-D $ file] returns false.
-f file Detecting whether the file is a regular file (neither catalog nor the device file), and if so, it returns true. file [-F $ file] returns true.
-g file Detecting whether the file has the SGID bit, and if so, it returns true. [-G $ file] returns false.
-k file Detecting whether a file has the sticky bit (Sticky Bit), and if so, it returns true. [-K $ file] returns false.
-p file Detecting whether the file is a named pipe, and if so, it returns true. [-P $ file] returns false.
-u file Detecting whether the file has the SUID bit, and if so, it returns true. [-U $ file] returns false.
-r file Detecting whether the file is readable, and if so, it returns true. [-R $ file] returns true.
-w file Detecting whether the file can be written, and if so, it returns true. [-W $ file] returns true.
-x file Detecting whether the file is executable, and if so, it returns true. [-X $ file] returns true.
-s file Detecting whether the file is empty (the file size is greater than 0), is not empty return true. size [-S $ file] returns true.
-e file Detect file (including directory) exists, and if so, it returns true. exist [-E $ file] returns true.
 
 
 Test shell are as follows:
# / bin /! the bash
 echo  " Arithmetic operators - * /% = = ==! " 
# arithmetic operator -! * /% == = = 
# arithmetic take an equal assignment is not equal to I 

# conditional expression to put between square brackets, and include a space, for example, [$ a == $ B] is wrong and must be written [== $ a $ B] 
a = 20 is 
B = 30 
C = 20 is 

echo  " a: $ a "  
echo  " B: $ B "  
echo  " C: $ C " 
Val = ` expr $ A + $ B`
 echo  " : A + B $ Val " front to # plus spaces, or the result B + A: 20 is + 30 
 
val`= Expr $ A -   $ b`
 echo  " ab: $ Val " 

Val =` expr $ A \ # * $ b` multiplication sign (* ) must be added in front of a backslash (\) escape in order to achieve multiplication;
 echo  " A * B: $ Val " 
 
Val = ` expr $ B / $ a`
 echo  " B / A: $ Val " 

Val =` expr $ B% $ a`
 echo  " B% A: Val $ " 

C = $ B
 echo  " C = B: $ C " 

IF [== $ A $ B]
 the then  echo  " A equal B IS" 
The else  echo  " A IS B Not equal " 
Fi 

IF [A = $! $ B]
 the then  echo  " A IS B Not equal " 
the else  echo  " A equal B IS " 
Fi 

echo  " relational operators -eq -ne -gt - -le -ge lt " 
# relational operators -eq -gt -LT--ge -ne - Le 
# support only numeric relational operators, do not support the string until the string is a numeric value arithmetic operator. 
IF [$ a - EQ $ b]
 the then  echo  " $ A -eq $ b: A b IS equal is " 
the else  echo  " $ A -eq $ b: A b IS equal is not "
be

if [ $a -ne $b ]
then echo "$a -ne $b : a is not equal b"
else echo "$a -ne $b : a is  equal b"
fi

if [ $a -gt $b ]
then echo "$a -gt $b : a is greater than  b"
else echo "$a -gt $b : a is not greater than b"
fi

if [ $a -lt $b ]
then echo "$a -lt $b : a is less than  b"
else echo "$a -lt $b : a is not less than b"
iffi

 [ $a -ge $b ]
then echo "$a -ge $b : a is greater or equal  b"
else echo "$a -ge $b : a is not greater or  equal b"
fi

if [ $a -le $b ]
then echo "$a -le $b : a is less or equal  b"
else echo "$a -le $b : a is not less  or equal b"
fi


echo "布尔运算符"
#布尔运算符 ! 非 -o 或 -a 与
if [ $a != $b ]
then echo "a is not equal b"
else echo "a is  equal b"
fi

if [ $a -gt 10 -a $b -gt 20 ]
then echo "$a -gt 10 -a $b -gt 20 : ture"
else echo "$a -gt 10 -a $b -gt 20 : false"
fi

if [ $a -gt 100 -a $b -gt 20 ]
then echo "$a -gt 100 -a $b -gt 20 : ture"
else echo "$a -gt 100 -a $b -gt 20 : false"
fi

if [ $a -gt 100 -o $b -gt 20]
 The then  echo  " $ A $ -o -gt 100 B 20 is -gt: ture " 
the else  echo  " $ A $ B -gt 100 -o -gt 20 is: to false " 
Fi 

echo  " String operators " 
# ! = = The -Z - n-STR 
# 0 is equal to a length not equal to zero does not empty to true 

D = " ABCD " 
E = " ABC " 

IF [= $ D $ E]
 the then  echo  " $ E $ = D: E D iS equal " 
the else  echo  " $ E $ = D: E D IS Not equal " 
Fi 

IF [ $d != $e ]
then echo "$d != $e : d is not equal e"
else echo "$d != $e : d is  equal e"
fi

if [ -z $d ]
then echo "-z $d : d string length is zero"
else echo "-z $d : d string length is not zero"
fi

if [ -n $d ]
then echo "-n $d : d string length is not zero"
else echo "-n $d : d string length is  zero"
[$ d]
iffi

then echo "$d : d string  is not empty"
else echo "$d : d string  is  empty"
fi

echo "文件测试运算符"
# -d -f -r -e -x -s -e 
filename="/data/cfltest/operator.sh"

if [ -d $filename ]
then echo "-d $filename : it is a directory"
else echo "-d $filename : it is not a directory"
fi

if [ -f $filename ]
then echo "-f $filename : it is a file"
else echo "-f $filename : it is not a file"
fi

 

Guess you like

Origin www.cnblogs.com/shishibuwan/p/11240120.html