This was learned more than a year ago. I did not expect to learn it again today. By the way, I will share it with you.
1. shell conditional test command
Purpose: test whether the expression is true. If yes, the returned value is 0. If no, the returned value is another value.
Format: test condition expression [condition expression]
Compare the status character strings of common test files to an integer compare the status of the logical test files
Format: [operator files or directories] common test operators
-D: test whether the directory is "directory]
[Root @ localhost/] # [-d/var/spool/]
[Root @ localhost/] # echo $?
0
-E: test whether the test directory or file exist exists]
[Root @ localhost/] # [-e/etc/passwd]
[Root @ localhost/] # echo $?
0
-F: test whether the file is used. [file]
[Root @ localhost Packages] # [-f/etc/passwd]
[Root @ localhost Packages] # echo $?
0
[Root @ localhost Packages] #
-R: test whether the current user has the read permission. [read]
[Root @ localhost/] # [-r/etc/shadow]
[Root @ localhost/] # echo $?
0
[Root @ localhost/] #
-W: test whether the current user has the write permission]
[Root @ localhost/] # [-w/etc/shadow]
[Root @ localhost/] # echo $?
0
[Root @ localhost/] #
-X: test whether the current user can execute and the file [excute]
[Root @ localhost/] # [-x/etc/shadow]
[Root @ localhost/] # echo $?
1
[Root @ localhost/] # ll/etc/shadow ----------. 1 root 1086 February 26 22:38/etc/shadow
-L: test whether the file is a symbolic link [link]
[Root @ localhost/] # ll/etc/shadow ----------. 1 root 1086 February 26 22:38/etc/shadow
[Root @ localhost/] # [-L/etc/shadow]
[Root @ localhost/] # echo $?
1
Another test
[-E/media/cdrom] & echo "YES" is followed by the correct execution
[-E/media/cdrom/Server] & echo "YES"
Integer comparison format: [integer 1 operator integer 2]
Common Operation tester-eq: Equal to (Equal)-ne: Not Equal to (Not Equal)-gt: Greater Than (Greater Than)-lt: less Than (Lesser Than)-le: less than or Equal to (Lessser or Equal)-ge: Greater than or Equal to (Greater or Equal)
If the number of logon users is less than or equal to 10, YES is output.
[Root @ localhost desktop] # who | wc-l 2
[Root @ localhost desktop] # ['who | wc-l'-le 10] & echo "YES" YES
String comparison format [String 1 = string 2]
[String 1! = String 2]
[-Z string]
Common Test Operators
=: The string content is the same! =: The string content is different ,! -Z: The character content is blank.
# Read-p "input your name:" name root
# Echo $ name
# [$ Name! = "Root"] & echo "name is erro"
# Read-p "input your name :"
Name zhangsan
# [$ Name! = "Root"] & echo "name is erro"
# Echo $ name
Logic Test
Format: [expression 1] OPERATOR [expression 2]
Common test operators-a or &: logical and, "and" meaning
# Both the first and second expressions are true only when the test result is true. Otherwise, the expression is false (if the first expression is true, the expression is executed later)
-O or |: logical or, or meaning
# If at least one operator is true, the result is true. Otherwise, the result is false.
(Run the following command for the previous false )! : Logical no # If the specified condition is invalid, the returned result is true.
If Condition Statement -- if condition test command is executed for a single branch when the condition is true.
If the disk space used> 80% then command sequence = then alarm fi
Application case:
If the space used by the/boot partition exceeds 80%, the alarm information is output.
#! /Bin/bash RATE = df | awk 'nr = 4 {print int ($5 )}'
If [$ RATE-gt 80] then echo "warning, DISK is full! "
Fi
If Condition Statement -- different operations are performed on the dual branch when the condition is true or the condition is not true.
If condition test command if 3306 port is in listening status then command sequence 1 =, then mysqld service has run else command sequence 2 else start mysqld service fi
Example: Determine whether vsftp is running. If it is already running, a prompt is displayed; otherwise, the vsftp service is restarted #! /Bin/bash service vsftp status &>/dev/null if [$? -Eq 0] then echo "vsftp service is running" else/etc/init. d/vsftp restart fi
If the space of the/boot partition exceeds 80%, the alarm is output. Let's write a script.
# Vi a. sh #! /Bin/bash #. sh NUM = 'df | awk' NR = 5 {print int ($5)} ''if [$ NUM-gt 80] then echo" Warning, DISK is full! "If
Additional extensions of awk
[Root @ localhost ~] # Cat/etc/passwd | grep bash | awk-F: '{print ($1)} 'root lenovo [root @ localhost ~] # ^ C
For Loop statement
For recipient in mail address list, repeat a set of command operations based on the variable value, such as "do for variable name in Value List", and mail the do done command sequence "done"
#! /Bin/bash for TM in a B c d e do echo $ TM sleep 1 done ~ ~ ~ Repeat the specified test conditions. If the conditions are true, the corresponding command operation is executed repeatedly.
While command or expression while available memory <100 mb do = "do command list to get the number of available memory done
Multiple branch statements
Root @ localhost ~] # Cat a. sh
#! /Bin/bash # a. sh case $1 in start)
Echo "start..."; stop) echo "stop ......";;
Restart) echo "restart ......"; laod) echo "laod ......"; status)
Echo "start..." echo "stop..."; esac
[Root @ localhost ~] #./A. sh start ....
[Root @ localhost ~] #./A. sh stop... [root @ localhost ~] #
[Root @ localhost ~] # Cat a. sh #! /Bin/bash
# A. sh read-p "input:" NUM case "$ NUM" in [a-z] | [A-Z])
Echo "it is english"; [0-9])
Echo "it is shuzi"; *) echo ".........";;
Esac
[Root @ localhost ~] #
[Root @ localhost ~] #./A. sh input: 2 it is shuzi
[Root @ localhost ~] #./A. sh input: a it is english [root @ localhost ~] #./A. sh @ input: @ ...... [root @ localhost ~] #./A. sh input: # ...... [root @ localhost ~] #
The shift migration statement is used to migrate the location variable ~ $9 is passed to the left in sequence
[Root @ localhost ~] #./A. sh 10 20 The sum is: 30
[Root @ localhost ~] # Cat a. sh #! /Bin/bash
# A. sh Result = 0 while [$ #-gt 0] do Result = 'expr $ Result + $ 1' shift done echo "The sum is: $ Result"
[Root @ localhost ~] #
Break statement
It is used to jump out of the current loop body and execute the statement after the loop body.
Function Application
[Root @ localhost ~] #./C. sh 46 55
[Root @ localhost ~] # Cat c. sh
#! /Bin/bash # c. sh adder () {echo 'expr $1 + $2 '}
Adder 12 34 adder 22 33
[Root @ localhost ~] #./C. sh 46 55
[Root @ localhost ~] #