shell 07 (shell operator)

1. Shell operator

1.1 Arithmetic operators

expr command

expr is the abbreviation of evaluate expressions, translated as "evaluation expression". Shell expr is a powerful and complex command. In addition to integer calculations, it can also be combined with some options to process strings, such as calculating string length, string comparison, string matching, string extraction, etc. follow-up explanation.

  • Integer calculation
  • Calculate string length
  • string comparison
  • string matching
  • string extraction
expr 算术运算符表达式

Get the calculation result and assign it to a new variable syntax

result=`expr 算术运算符表达式`

Note: Here is the backtick ``

1.1.1 Evaluating expressions

expr 1 + 1  #2

The operation must have a space 

expr \( 10 + 10 \) \* 2 + 100

Case:

1.1.2 Calculate string length

 

 

 

1.2 Comparison Operators

1.2.1 Integer comparison operators

 Integer comparison operators only support integers, not decimals and strings (string comparisons will be explained later), unless the value of the string is an integer number .
Each command has a return value. We will explain the exit status later, and then explain it in detail. Returning 0 means success, and returning 1 means failure

1.2.2 String comparison operators

Two variables can be compared. The type of the variable can be numbers (integers, decimals) and strings. 

 recommend[[]]

  • The difference between [[]] and []

① The occurrence of word splitting 
[[]] There will be no word splitting
[] There will be word splitting. 

Word splitting will split and compare strings containing spaces

  • escape character

[[]] does not need to be escaped for <, the format is [[ string 1 < character 2 ]]

[] need to escape <,>, the format is [string1 \<string2] 

 1.3 Boolean operators

 1.4 Logical operators

1.5 File test operators

Ability to use common file test operators to check files,
for example: whether the file exists, whether it is readable, whether it is executable, whether it is empty, whether it is writable, whether it is a directory, whether it is an ordinary file

The file test operator is used to detect various attributes of the file.
The attribute detection is described as follows

Guess you like

Origin blog.csdn.net/peng_258/article/details/132451688