Theoretical part:
1, in a simple script, each of the statements executed sequentially, so as to achieve automated management,
Sequential single execution is too mechanized, not "smart" enough to achieve more flexible system tasks.
Example: Bulk Install package
100 Machines 50 units installed 50 not installed
2, the shell environment according to the $? To determine whether the execution succeeds, when the return value of 0 o'clock represents a successful execution. If the non-0 represents an integer that performs an exception that is typically 1 or 1 to 127.
3. Common Logic Test
&& : The meaning of logic with, "and also" satisfies two conditions
|| : Logical OR, "or" means satisfying one of the two conditions
! : Logical No
Logic and && refers to and also means that only the first and the two conditions are set to return a value of 0 (established) otherwise it is 0 (not established)
Logic and-Examples
Enter the/USR directory to display the current directory, if the CD does not enter the directory after the command will not be executed
Logical OR-instance
Combat-1
We judge the values of the two variables by means of a logical representation.
2 , the comparison of numerical values
-eq : Equals (Equal)
-ne : Not equal to (not Equal)
-GT : Greater Than (Greater Than)
-lt : Less than (Lesser Than)
-le : Less than or equal to (Lesser or Equal)
-ge : greater than or equal to (Greater or Equal)
format [numeric 1 operator value 2]
3. String Comparisons
= string content is the same
!= string content is different
- Z string content is empty
! Take the opposite meaning.
Actual combat
To determine the locale of the current system, when found not en. US output cue message "$LANG"
$LANG variable is the language that defines the current system as belonging to the environment variable
To take the opposite is not equal to the meaning
Combat 2-
-Z for tests on variables that are assigned
2-1- the format of a single branch if statement
If condition test action
Then
Command sequence
Fi
The structure of a single branch is very simple, the condition is set to execute then command sequence, the condition is not true fi end
Combat-1
2-2- Dual-Branch if statement
Two-branch selection results, the requirements for the condition is set up, the conditions are not set up to perform different operations, respectively
Grammatical structure
If condition test action
Then (conditional execution)
Command sequence 1
else (otherwise, if the condition is not set, then execute)
Command Sequence 2
Fi
Combat 1-
We write a connectivity test script that provides the destination host address through the location parameter, and then gives the appropriate hint based on the ping test result
- C Ping Number of packages
- I. Ping the interval of the package
- W time-out interval
Script Explanation: If the if is judged to be 0 then execute then output target host up
If no 0 is executed, the else output target host is down
2-3- Multi-Branch if statement
Since the IF statement can be based on the test results,
Do not set up separate execution, so can be nested use, make multiple judgments.
If condition test action 1
Then
Command sequence 1
Elif Condition Test 2
Then
Command Sequence 2
Else
Command Sequence 3
Fi
Multi-Branch If statement application
1. Write a score sub-document script gradediv.sh according to the input of different scores to distinguish between excellent, qualified, unqualified three block
Vim gradediv.sh
First judge whether the greater than 85 is less than 100 if the output is good and end judgment
If less than 85 and greater than 60 for excellent execution then output qualified
Otherwise all of the others are not eligible to continue
Results of execution
Actual combat part
1, the script program is used to monitor the operating state of the system service httpd, the requirements are as follows
Log information is logged in the "/var/log/httpderr.log" file when the service state fails.
Automatically restarts the HTTPD service for which the state failed. If restarting the HTTPD service fails, test attempts to restart the server host
Vim httpd.sh
View/var/log/httpderr.log
2, write uname.sh script to determine the current system kernel and primary version if more than 2.4 output the current version otherwise the output indicates that the current version is too low
Filter the major version number
Filter out the minor version number
Vim kernel.sh
Perform:
3-1-for structure of a looping statement
Use for when looping statements, you need to specify a variable and a list of possible values. Follow the different values, execute the same command sequence until the value of the variable is taken out of the loop. The list of values is called The execution condition of the For statement
Syntax structure for the FOR statement execution
For variable name in value list
Do
Command sequence
Done
its species variable name can be customized, do inside can also nest multiple if statement done end Loop
inch is a keyword, where values are taken, and multiple values are separated with spaces
Experimental Environment : There are times when our system needs multiple users, and the user name uses the employee's pinyin as the user name which can also be used for loop to create and set the initial password
We'll get the employee Name list from the Personnel department. Define this column as a list of values
Here we simulate creating a
these users as a value, are the names of some manufacturers .
Vim Users.txt
View the user who created the success
Edit a script Add user vim adduser.sh
First define a variable as a list of values
Assigning a list of values to a variable User
nesting a if statement to determine whether the user exists, or if it exists, the output user already exists or executes Else command sequence to add a user and set a password to implement a value loop
if Judging using grep and the awk Filter Out /etc/passwd The first field
Use user_a compare to the list of values if it is equal to the output user already exists, if not equal to the execution Else the command sequence
Execute script: sh–x adduser.sh
Execute again
Output the user already exists,
To delete a user you just created, you can write a script
Vim deluser.sh
Execution :
All right, that's it. Batch create users and bulk delete users
Case based IP Address List detection host status
Set up a list of IP address values
Vim Ip.txt
Vim ping.sh
determine if the current host is alive, if output host is up otherwise output host is down
this is simple. for application of cyclic statements
Shell Script (ii) Use of--IF flow control statements and for loop statements