1, test: the detection of the expression is established, the return value is 0, otherwise, not 0
Method:
1) test expression
2) "Expression"
2. File test:
-D: Whether it is a directory
-F: Whether it is a file
-E: whether there is
-r: Whether Read permission is available
-W: Write permission
-X: Whether there are execute permissions
3, Integer value comparison:
-eq: Equals
-ne: Not equal to
-GT: Greater Than
-LT: Less than
-ge: greater than or equal to
-le: Less than or equal to
4. String comparison:
=: string content is the same
! =: string content is not the same
-Z: string content is empty
5. Logic Test:
-A or &&: Also, two conditions are true at the same time, the preceding command executes successfully before executing the subsequent command
-O or | | : Or, a condition is true
! : No, true if condition is not satisfied
6, if statement: Conditional Judgment Statement, the establishment is executed, otherwise launched or the next judgment, up to execute a command
1) Single branch: Judge once, set up execution, do not set out
If "Conditional expression"
Then
Command
Fi
2) Double branch: Judge once, set up execute an order, do not set to execute another command
If "Conditional expression"
Then
Command 1
Else
Command 2
Fi
3) Multi-branch: Judge multiple times, execute sequentially
If "Conditional expression"
Then
Command 1
Elif "Conditional expression"
Then
Command 2
Else "conditional expression"
Then
Command 3
Fi
First, condition test operation
1 , File Testing
(1) Test directory/media is a directory, $? The return value is 0, indicating the existence of this directory.
If $? The return value is not 0, indicating that the directory does not exist
(2) Whether the test catalog exists, the output is more intuitive.
The output "Yes" indicates that the directory exists
No "yes" output indicates that the directory does not exist
2, Integer value comparison
(1) Determine the number of currently logged in users, when more than 5 output "too many".
No more than 5 "too many" instructions are present.
(2) Determine the currently available free memory (buffers/cache) size, when less than 2048MB output specific values.
3. String comparison
User enters "yes" or "no" to confirm a task
4. Logic Test
Determine if the kernel version of the current Linux system is greater than 2.4.
View Kernel
Judge
Second, if conditional Statements
1 , single Branch if Statement
(1) Determine the mount point directory and automatically create it if it does not exist
Step: First create the script vim chkmountdir.sh edit script, give x permission and execute (detailed script like)
Execute and view
(2) Determine whether the current user is root, if not the error and execute "Exit 1" Exit script, no longer execute other code.
Step: Edit a script vim/opt/chkifroot.sh and switch user authentication
Create and Switch user authentication
2 , dual-branch if Statement
(1) Determine if the target host is alive and show the test results
Step: Create a script vim pinghost.sh
Give x permission and verify, note: The native address is 192.168.1.10
(2) Check whether the VSFTPD service is running, if the operation is to list the listening address, PID number; otherwise output hint "warning, vsftpd Service Unavailable 1". (Insert the Linux system installation CD and install the VSFTPD package).
Start VSFTPD Services (Service vsftpd start), run the script and view the results.
Stop the VSFTPD service (service vsftpd stop), run the script, and view the results.
Step: Write the script vim chkftpd.sh and install the VSFTPD service
Give x permissions and verify
3 , multi-branch if Statement
(1) According to the input of different test scores to distinguish between excellent, qualified, unqualified three block.
Step: Write a script vim gradediv.sh
Give x permission and execute
Article reference public number: L Baby talk about it
Shell Script application (if statement's structure)