Fundamentals of shell programming (shell script learning notes)
Operators in Shell mainly include comparison operator (for integer comparison), string operator (for string test), file operation operator (for file test), logical operator, arithmetic operator, bit operator, self increasing and self decreasing operator, etc.
1. Arithmetic operator
Arithmetic operators refer to common arithmetic operations such as addition, subtraction, multiplication, division, remainder and idempotent, as well as compound arithmetic operations such as addition, subtraction, multiplication, division and remainder. It should be noted that Shell only supports integer calculation, that is, all operations that may produce decimals will round off the decimal part.
Most common arithmetic operations need to be used in combination with the built-in command let of Shell.
2. Bitwise operator
Bit operation is based on binary data in memory, that is, bit based operation. Common bit operations include left shift operation, right shift operation, bitwise and, bitwise OR, bitwise non, bitwise XOR and so on.
The left shift and right shift elements of bit operation are actually the "left and right shift" of integers in memory, in which the left shift operator is < < and the right shift operator is > >.
#Shift left shift right operation #4 shift left 2 [root@localhost ~]# let "value=4<<2" [root@localhost ~]# echo $value 16 #4 shift right 2 [root@localhost ~]# let "value=4>>2" [root@localhost ~]# echo $value 1
Bitwise and operation = = (&) = =, is to write two integers into binary form and compare them with the same bit. The result is 1 only when the corresponding binary value is 1.
#Bitwise sum operation [root@localhost ~]# let "value=8&4" [root@localhost ~]# echo $value 0
Bitwise OR operation = = (| = =) is to write two integers into binary form and compare them with the same bit. As long as the corresponding position has 1, the result is 1.
#Bitwise OR operation [root@localhost ~]# let "value=8|4" [root@localhost ~]# echo $value 12
Bitwise XOR operation = = (^) = =, is to write two integers into binary form, and then compare them with the same bit. As long as the corresponding positions are both 0 or 1, the result will be 0, otherwise it will be 1.
#Bitwise XOR operation [root@localhost ~]# let "value=10^3" [root@localhost ~]# echo $value 9
The calculation method of bitwise non = = (~) = = is troublesome. Here is a fast calculation formula: "~ a" has a value of "-" (a+1) ".
#Bitwise non operation [root@localhost ~]# let "value=~8" [root@localhost ~]# echo $value -9
3. Self increase and self decrease
* * Auto increase and auto decrease operations mainly include pre auto increase, pre auto decrease, post auto increase, post auto decrease, etc** The self incrementor is "+ +" and the self decrementor is "–". The operation object can only be a variable, not a constant or expression.
[root@localhost ~]# cat add_minus.sh #!/bin/bash Add_01=10 Add_02=10 #Add_01 front autoincrement #That is to Add_01 is incremented from 1 to 11, and then assigned to Add_03 is 11 let "Add_03=(++Add_01)" #Add_02 post auto increment #That is, assign the current value to add first_ 04, i.e. 10, and then Add_02 increases by 1, i.e. 11 let "Add_04=(Add_02++)" #Print the value of each variable #According to the above calculation method, Add_01,Add_02,Add_03 is 11, Add_04 is 10 echo Add_01 is:$Add_01 echo Add_02 is:$Add_02 echo Add_03 is:$Add_03 echo Add_04 is:$Add_04 [root@localhost ~]# bash add_minus.sh Add_01 is:11 Add_02 is:11 Add_03 is:11 Add_04 is:10
1. Use $ for calculation
$ is similar to $(()) and can be used for simple arithmetic operations. The usage is given below:
[root@localhost ~]# echo $[1+1] 2 [root@localhost ~]# echo $[2-1] 1 [root@localhost ~]# echo $[2*2] 4 #Division operation, because it is an integer operation, round off the decimal [root@localhost ~]# echo $[5/2] 2 #Remainder operation [root@localhost ~]# echo $[5%2] 1 #exponentiation [root@localhost ~]# echo $[5**2] 25
##2. Using expr to do operations
The expr command can also be used for integer operations. Unlike other arithmetic operations, expr requires spaces between operands and operators (otherwise only strings will be printed), so special operators should be escaped with escape characters (such as *).
#If there is no space between the operator and the operand, only the string will be printed [root@localhost ~]# expr 1+1 1+1 #It can be calculated normally after being separated by spaces [root@localhost ~]# expr 1 + 1 2 #Special symbols (metacharacters) need to be escaped with escape characters, otherwise an error will occur [root@localhost ~]# expr 2 * 2 expr: syntax error [root@localhost ~]# expr 2 \* 2 4
3. The built-in operation command declare
#Example 1: do not use declare to define variables [root@localhost ~]# I=1+1 [root@localhost ~]# echo $I 1+1 #Example 2: using declare to define variables [root@localhost ~]# declare-i J [root@localhost ~]# J=1+1 [root@localhost ~]# echo $J 2 #Note that the arithmetic operation in the Shell requires that there should be no spaces between operators and operands, but closely connected; Special symbols do not need to be escaped with escape characters (such as the + sign here); If the arithmetic expression contains other variables, it does not need to be referenced with $
The variable I in example 1 is assigned "1 + 1" without formal definition. For Shell, at this time, "1 + 1" is just a string, which is the same as "abc", so it is only a string printed out.
In example 2, the integer variable J = = (- i parameter specifies that the variable is "integer") = = is explicitly defined by declare. At this time, assign "1 + 1", and the Shell will parse the following string into arithmetic operation, so the printed value is the calculated value of arithmetic expression.
4. Arithmetic extension
Arithmetic extension is the operation mechanism of integer variables provided by Shell and one of the built-in commands of Shell. The basic syntax is as follows:
#Display output: echo $((Arithmetic expression)) #Example: calculate the value of 2*i+1 [root@localhost ~]# i=2 [root@localhost ~]# echo $((2*i+1)) #Note that there is no $sign before variable i 5 [root@localhost ~]# echo $((2*(i+1))) #Change operation priority with parentheses 6 #Variable assignment var=$((Arithmetic expression)) #Example: assign the value of 2*i+1 to the variable var [root@localhost ~]# var=$((2*i+1)) [root@localhost ~]# echo $var 5 #Undefined variables participate in arithmetic expression evaluation #If the variable in the expression is not defined, its value will be assumed to be 0 during calculation (but it will not really assign a value of 0 to the variable) [root@localhost ~]# echo $((2*(j+1))) 2
5. Use bc for calculation
bc under Linux is such a tool specially used for high-precision calculation. bc's man file describes it as "an arbitrary precision calculator language".
The simplest way to use bc under Linux is to directly enter the bc command. Enter and enter the interactive interface of bc. The flashing command prompt indicates that the expression can be entered now.
[root@localhost ~]# bc bc 1.06 Copyright 1991-1994,1997,1998,2000 Free Software Foundation,Inc. This is free software with ABSOLUTELY NO WARRANTY. For details type `warranty'. a=9 b=5 a+b 14 a-b 4 a*b 45 a/b 1 #In fact, by default, bc does not display the decimal part. You must set the number of decimal places to display. #Display of decimal places scale=3 a/b 1.800
In addition, bc also supports logical operation and comparison operation. However, in Shell programming, we often only need to call the processing results of bc without entering the interactive interface of bc. Fortunately, bc has considered this situation, so it supports batch processing and pipeline processing of expression calculation.
[root@localhost ~]# cat cal.bc 12*34 34/12 scale=3;34/12 a=1;b=2;a+b #Batch calculation [root@localhost ~]# cat cal.bc | bc 408 2 2.833 3
#Directly call the bc calculation expression and assign the calculation result to the variable to participate in the subsequent calculation or judgment. [root@localhost ~]# cat bc.sh #!/bin/bash NUM01=10 NUM02=15 TOTAL=$(echo "$NUM01+$NUM02" | bc) echo $TOTAL [root@localhost ~]# bash bc.sh 25
For more usage of bc, please refer to the man file. bc is also a language. It supports sequential execution, judgment, circulation and other operation mechanisms like conventional programming languages. It also supports user-defined functions. It has very powerful functions, which can be deeply understood by interested readers.