Arithmetic operations are undoubtedly very important in shell scripting programming;
The command to perform an integer arithmetic operation in bash is let, which has the syntax format:
Let Arg ...
Arg is a complete arithmetic expression consisting of a separate arithmetic expression, such as +,-,*,/,%,^;
where ^ is the square operation, such as 2^3 represents 2 of the three-time side;
The Let command can be overridden in the following ways:
((arithmetic expression))
For example
[[email protected] wjq]# let "a=2*3"
[Email protected] wjq]# echo $a
6
[[Email protected] wjq]# (("a=3*3"))
[Email protected] wjq]# echo $a
9
In addition to the simple subtraction operation, the arithmetic operation also includes the enhanced operation mode and the special enhanced operation mode.
Enhanced mode of operation:
+ =: such as A+=b-->a=a+b
-+: such as A-=b-->a=a-b
*=: such as A*=b-->a=a*b
%=: such as A%=b-->a=a%b
/=: such as a/=b-->a=a/b
The Convert to let command operation is:
[Email protected] wjq]# b=5
[[email protected] wjq]# let "a+=b"
[Email protected] wjq]# echo $a
14
[Email protected] wjq]#
Special-Enhanced operations:
A++:A first assigns the value to the variable, then +1;
++A:A first +1, then assigns the value to the variable;
A--:A assigns the value to the variable first, then-1;
--A:A first-1, then assigns the value to the variable;
The convert to let command is calculated as:
[Email protected] wjq]# a=4
[Email protected] wjq]# b=5
[[email protected] wjq]# let "a++"
[[email protected] wjq]# let "a+=b"
[Email protected] wjq]# echo $a
10
Arithmetic Operation Method:
①let var= Arithmetic expression
#let "B=5+4"
#echo $B
The arithmetic expression is first performed, and the result of the operation is saved to the variable var;
Variables: containers for storing data
Weakly variable: Weakly typed variable, default is character type variable, numeric type (not including float type)
②var=$[Arithmetic expression]
[[email protected] wjq]# A=$[a*b]
[Email protected] wjq]# echo $a
50
③var=$ ((arithmetic expression))
In an arithmetic expression, if there is a reference, you can omit the
[Email protected] wjq]# a=$ ((a*b))
[Email protected] wjq]# echo $a
250
④exper ARGU1 ARGU2 ARGU3
ARGU1 and ARGU3 must be numeric
ARGU2 is an operator
⑤echo "Arithmetic expression" | BC (BC Command equivalent Calculator)
[Email protected] wjq]# echo "3*4" | Bc
12
Conditional Test Statement:
There are three test statements for conditional testing
One is the test command;
The second is to enclose it in a pile of square brackets, which is completely equivalent;
Test-f "/etc/passwd" is exactly equivalent to [-F "/etc/passwd"]
Three is [[conditional expression]];
These three expressions can be used in conjunction with a variety of system operators, which can be divided into four classes: Numeric test operators, string test operators, file state test operators, and logical test operators;
The following four operators are represented here using the second expression;
Data test operator: operation with binocular operator;
[N1-eq n2]: Test if N1 and N2 are equal, equal to true, and not as false
[N1-ne n2]: Test N1 and N2 are not equal, unequal to true, equal to false;
[n1-lt n2]: Test N1 and N2, the left is less than the right, less than true, greater than false;
[N1-GT n2]: Test N1 and N2, the left is greater than the right, greater than true, less than false;
[N1-le n2]: Test N1 and N2, whether the left is less than or equal to the right;
Cases
[[Email protected] wjq]# ["$a"-eq 0] && echo "a equals 0" | | echo "A is not equal to 0"
A is not equal to 0
String test operators:
Operate with a single-mesh operator;
-Z ' N1 ': If the string length is 0, the test condition is true;
-N ' N1 ': If the string length is not 0, the non-null is true, and the null is false;
[[email protected] wjq]# a= ' string '
[[Email protected] wjq]# [-Z "$a"] && echo "a equals 0" | | echo "A is not equal to 0"
A is not equal to 0
To operate with the binocular operator:
==|=: The two strings tested are equal, the same is true, and the differences are false;
! =: The two characters tested are not the same, the difference is true, the same is false;
: The binary data of the two strings being tested is converted to ASCLL code; The left is greater than the right, greater than true, less than false;
: The two strings tested in the binary data after conversion to ASCLL code, the left is less than the right, less than true, greater than false;
Cases
①
[[email protected] wjq]# a= ' string '
[[email protected] wjq]# b= ' string '
[[Email protected] wjq]# ["$a" = = "$b"] && echo "a equals B" | | echo "A is not equal to B"
A equals b
Ii
[[email protected] wjq]# a= ' string '
[[email protected] wjq]# b= ' str '
[[Email protected] wjq]# ["$a"! = "$b"] && echo "A not equal to B" | | echo "a equals B"
A is not equal to B
③
[[email protected] wjq]# a= ' string '
[[email protected] wjq]# b= ' str '
[[Email protected] wjq]# ["$a" > "$b"] && echo "A greater than B" | | echo "A is less than B"
A greater than B
File status test operator: Monocular test operator;
File existence test, if the test file exists for true, does not exist for false;
-A|-E:
Cases
[[Email protected] wjq]# [-A./8yu] && echo "file exists" | | echo "file does not exist"
File exists
File type Test (test presence)
-B File: If the file exists and is a block device, the test is true;
-C File: If the file exists and is a character device, the test is true;
-D File: If the file exists and is a directory device, the test is true;
-F File: The test is true if it exists and is a text document;
-h|-l file: The test is true if it is present and the file is linked to a symbol;
-P File: If the file exists and is a socket file, the test is true;
Cases
[[Email protected] wjq]# [-F./8yu] && echo "File as text document" | | echo "File is not a text document"
File is a text document
Access permission settings for the file:
-R file: If the file exists and is user readable, the test is true;
-W File: If the file exists and is user writable, the test is true;
-X file: If the file exists and is user-executable, the test is true;
Cases
[[Email protected] wjq]# [-R./8yu] && echo "File readable" | | echo "File not readable"
File readable
[Email protected] wjq]# [-W./8yu] && echo "File writable" | | echo "File not writable"
File can be written
[[Email protected] wjq]# [-x/8yu] && echo "File executable" | | echo "File not executable"
File executable
File special permission Identification test:
-U file: If the file exists and the SUID permission is set, the test is true; otherwise false;
-G File: If the file exists and the Sgid permission is set, the test is true; otherwise false;
-K File: If the file exists and the sticky permission is set, the test is true; otherwise false;
Cases
[[Email protected] wjq]# [-U./8yu] && echo "file exists suid permissions" | | echo "file does not exist SUID permissions"
File does not exist SUID permissions
[[Email protected] wjq]# [-G./8yu] && echo "file exists Sgid permissions" | | echo "file does not exist Sgid permissions"
File does not exist Sgid permissions
[[Email protected] wjq]# [-K./8yu] && echo "file exists Sticky permissions" | | echo "file does not exist sticky permissions"
File does not exist SUID permissions
File ownership test:
-O File: If the file exists and its owner is the current active user, the test is true; otherwise false;
-G File: If the file exists and its genus is the current active user, the test is true; otherwise false;
Cases
[[Email protected] wjq]# [-O./8yu] && echo "file exists and its owner is the user" | | echo "file does not exist"
File does not exist
[[email protected] wjq]# ls./8yu
./8yu
[Email protected] wjq]# ls-l./8yu
-rwxrw-r--. 1 WJQ WJQ 71 March 20:39/8yu
Whether the file contents are empty:
-S file: True if the document exists and the content is not empty, and the content is not empty; otherwise false;
Time Stamp test:
-N File: The test file has been modified since the last time it was modified;
Logical Test operators:
①! : Logical non (not), he placed before any expression, is the original true expression becomes false, so that the original false expression becomes true;
②-a: Logic and (and), he placed between two logical expressions, only if two are true, the result is true;
Cases:
[-F./8yu-a-F/AA]
③-o: Logic or (or), he placed in the middle of two logical expressions, in which as long as one is true, the result is true;
Cases:
[-F./8yu-o-F/AA]
Two types of expressions are equivalent:
The first type of expression:
[-o/tmp/test] && [-s/tmp/test]
[-o/tmp/test] | | [-s/tmp/test]
The second type of expression:
[-o/tmp/test-a-s/tmp/test]
[-o/tmp/test-o-s/tmp/test]
[!-o/tmp/test]
Linux System Bash (Shell) Basics (3)