In the previous article, we mainly talked about the most basic things in shell, such as history, File/number testing, and how to execute a script. This article mainly introduces some in-depth content. For example, test, if condition statement, for loop, while loop, case statement, custom function, etc...
In the previous article, we mainly talked about the most basic things in shell, such as history, File/number testing, and how to execute a script. This article mainly introduces some in-depth content.
For example, test, if condition statement, for loop, while loop, case statement, custom function, etc. I hope to help you. I would like to give more comments .....
I. test conditions
1. test Command
Purpose: test whether a specific expression is true. if the condition is true, the return value after command execution is 0. Otherwise, the return value is another value.
Format: test condition expression
[Conditional expressions]
Common test types: test file status string comparison integer comparison logical test
1) Test file status
Format: [operator file or directory]
C. common test operators
-D: test whether the Directory is used)
-E: test whether a directory or file Exist exists)
-F: test whether the File is used)
-R: test whether the current user has the permission to Read (Read)
-W: test whether the current user has the Write permission)
-X: test whether the current user can execute the file (Excute ).
-L: test whether the file is a symbolic Link.
[-E/etc/sysconfig/] & echo "yes"
2) integer comparison
Format: [integer 1 operator integer 2]
Common test operators
-Eq: Equal to (Equal)
-Ne: Not Equal to (Not Equal)
-Gt: Greater Than (Greater)
-Lt: less Than (Lesser)
-Le: less than or Equal to (Lesser or Equal)
-Ge: Greater than or Equal to (Greater or Equal)
3) string comparison
Format: [String 1 = String 2]
[String 1! = String 2]
[-Z string]
Common test operators
=: Same string content
! =: The String content is different ,! Number indicates the opposite meaning
-Z: The String content is empty.
4) logic test
A) format: [expression 1] operator [expression 2]...
Common test operators
-A or &: logical and, "and" meaning
The test result is true only when both expressions are true. Otherwise, the test result is false.
-O or |: logical or, "or"
II. if condition statement
1. single branch:
If the space used by the/boot partition exceeds 80%, the alarm information is output.
#! /Bin/bash
RATE = 'df-hT | grep "/boot" | awk '{print $6}' | cut-d "%"-F1'
If [$ RATE-gt 80]
Then
Echo "Warning, DISK is full! "
Fi
2. dual branch
Different operations are performed when "condition is true" or "condition is not true"
Example: judge whether sshd is running. if it is running, a prompt is displayed. otherwise, restart the sshd service.
#! /Bin/bash
Service sshd status &>/dev/null
If [$? -Eq 0]
Then
Echo "mysqld service is running ."
Else
/Etc/init. d/sshd restart
Fi
3. multiple branches
III. for loop statements
Execute a group of commands repeatedly based on different values of the variable.
Example 1: output three lines of text in sequence, including the "Morning", "Noon", and "Evening" strings in a day.
#! /Bin/bash
For TM in "Morning" "Noon" "Evening"
Do
Echo "The $ TM of the day ."
Done
Example 2:
#! /Bin/bash
DIR = "/opt"
LMT = 1, 100
ValidUsers = 'grep "/bin/bash"/etc/passwd | cut-d ":"-f 1'
For UserName in $ ValidUsers
Do
Num = 'find $ DIR-user $ UserName | wc-L'
If [$ Num-gt $ LMT]; then
Echo "$ UserName have $ Num files ."
Fi
Done
4. while loop statement
Application Example 1:
Add 20 system user accounts in batches. the user names are "stu1", "stu2 ",...... , "Stu20"
#! /Bin/bash
I = 1
While [$ I-le 20]
Do
Useradd stu $ I
Echo "123456" | passwd -- stdin stu $ I &>/dev/null
I = 'expr $ I + 1'
Done
Application Example 2: delete the 20 accounts you just created
#! /Bin/bash
I = 1
While [$ I-le 20]
Do
Userdel-r stu $ I
I = 'expr $ I + 1'
Done
V. case multi-branch statement
Execute different command operations based on different values of variables
#! /Bin/bash
Echo-n "please inset some keys :"
Read AA
Case $ AA in
[A-z] | [A-Z])
Echo "they are letters"
;;
[0-9])
Echo "they are number"
;;
*)
Echo "they are other words"
;;
Esac
6. until statement
The until statement executes repeated operations based on the condition, which is the opposite of the while loop.
VII. shift migration statement
For migration location variables, set $1 ~ $9 is passed to the left in sequence
For example, if the location variable obtained by the current script program is as follows:
P $1 = file1, $2 = file2, $3 = file3, $4 = file4
After the shift command is executed, the variables at different locations are:
P $1 = file2, $2 = file3, $3 = file4
After the shift command is executed again, the variables at different locations are:
P $1 = file3, $2 = file4
#! /Bin/bash
Result = 0
While [$ #-gt 0]
Do
Result = 'expr $ Result + $1'
Shift
Done
Echo "The sum is: $ Result"
8. loop control statements
1. break statement: In loop statements such as for, while, and until, it is used to jump out of the current loop body and execute the statement after the loop body
2. continue: In loop statements such as for, while, and until, it is used to skip the remaining statements in the loop body and re-determine the condition to execute the next loop.
9. Shell function application
Shell functions
When writing a Shell script program, you can define some command operations that need to be reused as commonly used statement blocks, which are called functions.
Reasonable use of Shell functions can make the script content more concise, enhance the ease of coding of the program, and improve the execution efficiency
1. define a new function in two ways:
Method 1:
Function name {
Command sequence
}
Method 2:
Function name (){
Command sequence
}
2. example
Define an addition function in the script to calculate the sum of two integers
Calls the sum of the values of this function compute (12 + 34), (56 + 78 ),
#! /Bin/bash
Adder (){
Echo 'expr $1 + $2'
}
Adder 12 34
Adder 56 789