Linux Shell Programming (vi): operations

Source: Internet
Author: User
Tags arithmetic bitwise

Op-Expressions

The expression is contained in or (()) or [] within,

    • An arithmetic expression:(())

      • ((expression))
      • This expression is calculated according to the rules of arithmetic calculation. Equivalent to let expression .
    • Conditional Evaluation Expressions:[]

      • [expression]
      • This is a synonym for the built-in command test to [ begin with, ] ending with.

You can make a binary conversion in an expression: $ ((Number#scale)), $ ((10#8) =>8

To get the results of the calculation, you need to add the $ symbol in front of the expression, such as: $[expression] ,$((expression))

    • Difference
      • The difference between the two is that ((expr)) it can be executed in the shell, which can be assigned to a variable, without having to precede $号 it, and if the expression executes successfully the result is successful, otherwise it is unsuccessful, so it can be used as if a judgment expression.
Command Evaluation Expressions

In Linux, you can use commands to evaluate expressions, such as expr and let commands.

expr

The expr command evaluates an expression and prints its standard output.

 # usage:expr expression# expression# operator before and after a space ARG1 | ARG2 bits or Operations ARG1 & ARG2 bits and Operations ARG1 < ARG2 ARG1 less than ARG2 ARG1 <= ARG2 ARG1 less than equals ARG2 ARG1 = ARG2 ARG1 equals ARG2 ARG1! = ARG2 ARG1 Not equal to ARG2 ARG1 >= ARG2 ARG1 greater than equals ARG2 ARG1 > ARG2 ARG1 Greater than ARG2 ARG1 + ARG2 addition arg1-arg2 subtraction ARG1 * ARG2 multiplication using multiplication, the operator * should be escaped: \* arg1/arg2 Division ARG1 The% ARG2 takes the remainder string:regexp REGEXP the fixed match in string, the match string REGEXP is the same as string:regexp, and if the regular matches the string returns the matching Number of characters, otherwise return 0 substr string pos length string intercept, pos start position, POS minimum is 1,length is intercept length # such as: Expr substr Raojinlin 1 3 The above command outputs: Rao index STRING CHARS CHARS is a character , string is the string that the expression outputs the first occurrence of the character in the string, and if the character is not in the string, the length of the 0-length string string + token token is interpreted as a string , the value of expression to the ' match ' or '/' operator (expression) 
let

Let is used to calculate an arithmetic expression, which does not output the computed result standard, but instead assigns the result directly to the variable.

Grammar:let arg [arg]

Let computes each arg as an arithmetic expression. Evaluates a fixed-width integer without checking for overflow, but removes the 0 and marks it as an error. The following list of operators is grouped into levels of equal precedence operators. These levels are ranked in order of precedence.

    • Let parameter
          id++, id--  变量自增和自减,运算前先调用变量在运算,如果变量不存在则创建一个    ++id, --id  变量自增和自减,运算前先运算在调用,如果变量不存在则创建一个    -, +        整数和负数    !, ~        一个布尔操作符,用于将操作的值强制转换为布尔值并取反;位反运算    **          幂运算    *, /, %     乘法,除法,取余数    +, -        加法,减法    <<, >>      左移运算和右移运算,let执行结果为表达式的计算结果    <=, >=, <, >    比较操作符,let执行结果为表达式的计算结果    ==, !=      等于,不等于    &       位与 AND    ^       位异或 XOR    |       位或 OR    &&      逻辑与 AND    ||      逻辑或 OR    expr ? expr : expr            条件操作,三元运算    =, *=, /=, %=,    +=, -=, <<=, >>=,    &=, ^=, |=  赋值运算
## 运算符

' id++ id--'
Variable self-increment and self-subtraction
Calling a variable before the operation

' ++id--id '
Variable self-increment and self-subtraction
Operations are preceded by an operation in the calling variable

'-+ '
Positive negative number

`! ~
A Boolean operator that is used to cast the value of the operation to a Boolean value and inverse.
bitwise inverse Arithmetic

' * * '
exponentiation operation

' */% '
Multiply, divide, remainder

' +-'
arithmetic addition and subtraction

' << >> '
Left shift operation
Right Shift operation

' <= >= < > '
Compare operations

' = = = '
equal to and not equal to

' & '
Bits and arithmetic

^
Bitwise XOR OR operation

|
Bit or operation

' && '
Logic and

' | | '
Logical OR

' Expr? Expr:expr '
Three-dimensional expression
If the first expression is true, this executes the second expression, otherwise a third expression is executed

' = *=/=%= + = = <<= >>= &= ^= |= '
Assignment operations

' Expr1, Expr2 '
Comma

## 示例* ```expr```
[email protected]:~# expr 1 + 12[email protected]:~# expr 1 \* 11[email protected]:~# expr index raojinlin r1[email protected]:~# expr substr raojinlin 3 4ojin[email protected]:~# ```
    • let

    • (())
      # a 为上面let示例中的a变量,其值为2[email protected]:~# echo $((a+1)) # shell将会解释a变量5[email protected]:~# echo $((a+1 * 2))6[email protected]:~# echo $((a+1 * 2 / 2))5
    • []

      [email protected]:~# echo $[a+4]8[email protected]:~# echo $[23+4]27[email protected]:~# echo $[23 <=4]0

Linux Shell Programming (vi): operations

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.