Although some shell scripts written, but not profound for a lot of knowledge or memory. During the reading, just to see relevant knowledge in this record for later use.
Perform mathematical operations
1.expr command
➜ /etc expr 1 + 5
6
➜ /etc expr 1 * 5
expr: syntax error
➜ /etc expr 1 \* 5
5
Note: 1) have spaces between numbers and symbols
2) needs to be escaped individual symbols, as described above in Example asterisk (*)
2. Use square brackets
in bash, when a mathematical operation result is assigned to a variable, you can use dollar is in square brackets ($ [operation]) the mathematical expression fenced.
➜ /etc var1=$[1 + 5]
➜ /etc echo $var1
6
➜ /etc var2=$[$var1 * 2]
➜ /etc echo $var2
12
When calculated using the formula in square brackets, do not worry about shell misunderstand multiplication sign or other symbol, which is a big advantage.
Floating-point operations
Sometimes encounter floating-point arithmetic operations, the most common solution is to use the built-in calculator bash, called bc.
➜ /etc bc -q
3.44 / 5
0
scale=4
3.44 / 5
.6880
quit
The above example, -q is not display a welcome message bash calculator. to set the number of decimal places scale variable calculation reserved.
In the shell of the use of methods:
variable=$(echo "options; expression"|bc)
The first portion is provided to allow variable options, if more than one variable, can be separated with a semicolon.
expression parameter defines the mathematical expression by bc executed. Examples are as follows:
➜ /etc var1=$(echo "scale=4;3.44 / 5"|bc)
➜ /etc echo $var1
.6880
Numerical comparison
Numerical comparison can be said that the most common shell programming, and directly affects the direction of flow, so to grasp the value of comparative knowledge is also extremely important.
Compare | description |
---|---|
n1 -eq n2 | Check n1 and n2 are equal |
n1 -ge n2 | Check if n1 n2 is greater than or equal to |
n1 -gt n2 | Check if n1 is greater than n2 |
n1 n2 -The | Check n1 n2 is less than or equal to |
n1 -lt n2 | Check n1 is less than n2 |
n1 -ne n2 | Check n1 n2 not equal |
String comparison
Conditions also allows comparison string values.
Compare | description |
---|---|
str1 = str2 | Check that same str1 and str2 |
str1 != str2 | Check that str1 and str2 different |
str1 < str2 | Check if str1 str2 smaller than |
str1 > str2 | Check str1 is greater than str2 |
-n str1 | Check whether the length of the non-0 str1 |
-with str1 | Check whether the length of str1 0 |
File Compare
File comparison in shell programming is also the most used form of comparison. For state testing Linux file system files and directories.
Compare | description |
---|---|
-d file | Check the file exists and is a directory |
-e file | Check the file if there is |
-f file | Check the file exists and is a file |
-r file | Check whether the file exists and read |
-s file | Check the file if there is not empty |
-w file | Check whether the file exists and write |
-x file | Check whether the file exists and execute |
-O file | Check the file exists and the current user belongs to all |
-G file | Check the file exists and the current user default group same |
file1 file2 -nt | Check if file1 is newer than file2 |
file1 file2 -ot | Check if file1 is older than file2 |
Compound condition test
if-then statement allows the use of Boolean logic to test a combination of two Boolean operators are available:
- [ condition1 ] && [ condition2 ]
- [Condition1] || [condition2]
The first Boolean operation used to combine two Boolean operations AND conditions, then let the command execution portion, two conditions must be met.
A second Boolean OR Boolean operation used to combine the two conditions, will be executed if the condition is any command TRUE, then part.
Advanced features of the if-then
bash-shell provides advanced features that can be used in two if-then statement:
- The mathematical expression for the double parentheses
- For advanced string processing functions both brackets
1. double parentheses
double brackets command format is as follows:
((expression The))
expression The assignment may be any mathematical expressions or comparison.
2. Use double square brackets
double square brackets command format is as follows:
[[expression The]]
double square brackets provide a pattern-matching function.
Such as: [[$ USR == r * ]] can be used to determine whether the user begins with the letter r
CASE command
By case commands do not write lengthy if-then-else statements, and can check multiple values of variables through the list.
Format is as follows:
case variable in
pattern1 | pattern2) commands1;;
pattern3) commands2;;
*) commands;;
esac
Variable case specified command will be compared with different modes. If the variable and pattern matching, then SHELL is executed for the specified command mode. A plurality of modes can be separated by a vertical line in the operator. An asterisk (*) will capture all the value does not match the known patterns. Look closely, and JAVA of this case are very similar.