Conditional judgment, control structure:
Single Branch if statement
if judgment condition; Then
Statement1
Statement2
...
Fi
Two-branch if statement:
if judgment condition; Then
Statement1
Statement2
...
Else
Statement3
Statement4
...
Fi
Multi-Branched if statement:
if judgment condition 1; Then
Statement1
...
Elif judgment Condition 2; Then
Statement2
...
Elif judgment Condition 3; Then
Statement3
...
Else
Statement4
...
Fi
Test method:
- [Expression]
- [[Expression]]
- Test expression
There are three common conditions tested in bash:
Integer test:
- -EQ: Tests whether two integers are equal, such as $A-eq $B
- -ne: Test whether the two integers are unequal, unequal, true, equal, false;
- -GT: Tests whether one number is greater than the other, greater than, true, or false;
- -LT: Tests whether one number is less than the other, less than, true; otherwise, false;
- -ge: greater than or equal to
- -le: Less than or equal to
Example:
int1=63
int2=77
[$INT 1-eq $INI 2]
[[$INT 1-eq $INT 2]]
Test $INT 1-eq $INT 2
File test:
- -e file: Test files exist
- -F File: Test files are normal files
- -D FILE: Tests whether the specified path is a directory
- -R File: Tests whether the current user has read access to the specified file;
- -W
- -X
Example: [-e/etc/inittab] [-x/etc/rc.d/rc.sysinit]
How to perform arithmetic operations in the shell:
A=3
B=6
1. Let arithmetic operation expression
Let c= $A + $B
2. $[arithmetic operation expression]
c=$[$A + $B]
3, $ (arithmetic operation expression)
c=$ (($A + $B))
4, expr arithmetic expression, the expression in the operands and operators to have a space between, and to use the command reference
c= ' expr $A + $B '
Defining script Exit Status codes
Exit: Exit Script Exit # If the script does not explicitly define exit status code, then the exit code of the last command executed is the exit status code of the script;
Test script for syntax error: bash-n script
Bash-x Script: Stepping
Types of Bash variables:
Local variables (local variables)
Environment variables
Positional variables: $, $, ... shift
Special variables:
- $?
- $#: Number of parameters
- $*: Parameter list
- [email protected]: parameter list
Shell programming in Linux (i)