1, the syntax of the script structure:
Shell script is a "program" written using the function of the shell, which uses a plain text file (the file suffix is preferably sh file, which is convenient for us to manage), and writes some shell syntax and instructions (including external instructions) to the inside, with wildcards, With formal notation, pipeline command and data flow redirection, conditional judgment, loop logic and other functions to achieve our desired processing purposes.
Composition (common): Shell syntax and external directives
wildcard [A-z] [a-z]? *
Regular expressions
Pipeline Command |
Data redirection >> << > <
Conditional Judgment and circulation
2, why write scripts, the pros and cons of writing scripts?
Advantages:
1, the main basis of automation management
Query the login file, track traffic, monitor the user's use of the host State, host various hard device status, host software update query, not to mention the sudden request of other users
2, tracking and management system of important work
3. Simple Intrusion detection function
4. Continuous instruction Single
5. Easy Data Processing
6, cross-platform support and learning process is relatively short
Disadvantages:
Although Shell script claims to be a program, the shell script does not have enough speed to process the data. Because shell script uses external instructions and some of the default tools of the bash shell, he often calls the external library. Therefore, the instruction cycle is certainly less than the traditional programming language. So, shell script is a good tool for system management, but it is not good enough to handle a lot of numerical operations, because the shell scripts is slower and uses more CPU resources, resulting in poor allocation of host resources . Fortunately, we usually use shell script to handle server detection, but there is no need to do a lot of computing! So don't worry about it.
3. Scripting Considerations :
①, command execution is performed from top to bottom, left to right
Multiple spaces between ②, commands, options, and arguments will be ignored
③, blank lines will also be ignored, and the open [tab] key blank will also be ignored
④, if you read to a enter sign (CR), try to start executing the line (the string) command
⑤, if a command is too long, you can use ' \[enter ' to write commands skip
⑥, #为注解, annotations do not execute when the command is executed
4, the script file naming rules and execution methods and the implementation of scripting differences
1, naming rules: Generally named after the file '. Sh ' (e.g. xxx.sh)
2, the method of implementation:
You need to have Execute permission first: chmod u+x xxx.sh
Execution method:
①, with path and modify variable (path) to execute
Path includes relative path and absolute road strength
Execute under current directory:./xxx.sh
Do not execute in current directory:./opt/xxx.sh
To modify the PATH variable:
Method One: Place the script file under the folder of the path variable to execute
Method Two: Modify the path of the path variable to add the folder as the default path path
②, using Source to execute
Source script file name (source xxx.sh)
The difference between the above two methods of execution ① and ②:
When using the Execute method inside the ①: The script uses a new bash environment to execute instructions within the scripts! In other words, when the user executes the method, the script file is actually
Subroutines executed within the bash. So your variables are actually executed within the subroutine bash. All data within the subroutine bash is removed when the subroutine finishes executing, so the parent program
At the bottom of the "echo $ variable name", you don't see anything.
When using source in ② to execute a script file: The script is executed in the parent program shell, so the value of the variable is seen when executing "echo $ variable name"
For specific analysis of script execution, please refer to: http://www.jb51.net/article/53924.htm
5. The steps to write the script (what comments are needed in the script):
· function of script
· Version information for script
· Author of script and contact information
· Script's Copyright Declaration method
· History of script (historical record)
· A more specific instruction within the script, using the "absolute path" method to release
· Pre-declaration and setting of environment variables required for script operation
· Good commenting habits and proper [tab] BACKSPACE functionality
6. Condition test and Control flow:
Reference website:http://blog.csdn.net/wklken/article/details/6566945; (basic knowledge and examples can refer to this blog)
http://a3147972.blog.51cto.com/2366547/1284633;
http://www.jb51.net/article/54488.htm;
Write your own several basic code:
Test instruction testing and usage (as well as the difference between "test" and "[]"):
Conditional Judgment Type:
Syntax for if......elif......else......fi and case:http://www.cnblogs.com/l75790/p/7059296.html
Syntax for function functions:http://www.cnblogs.com/l75790/p/7060483.html
Three--shell script scripts for Shell programming (not yet done)