Learning Shell The script experience
Shell script Run Environment:Shell script, is to put multiple commands, organized into a file, to implement a series of actions, convenient execution of a file.
Shell scripts are primarily used in shell environments where we use rhel6, the main use of the Bash Shell interpreter tool to accomplish human-machine interaction. In the daily operating system there are other shell interpretation tools, such as csh,tcsh,dash,zsh and so on. Shell Scripts enable the automated operation of a series of actions. It is a powerful tool for automation operation and management, and is also a strong tools for Linux system administration. Learning good shell scripts, for future work, has a lot of significance.
basic structure and basic knowledge of shell scripts
a Shell script, consisting mainly of two parts, the first part is to declare the Shell's interpretation environment
"#! /Bin/bash"as above, the declaration system uses /bin/bash to interpret and run the script, and then your command section. The command part is executed in the order of the program, or through various branches to determine the loop structure, to control the operation. Finally achieve the purpose of the completion of work tasks.
in the Shell , the first thing you need to know is the basic command, which is our daily management of the operating system instructions, such as the Common CD, Pwd,useradd,passwd,touch,sed,awk, etc., these are composed Shell scripts that implement the basic elements of functionality. Through the different combinations of multiple commands, and process control, to achieve the deployment environment, management system tasks.
in our system, there are many variables, including system variables ($PWD, $USER, $UID ... ), is cured in the system, we can directly call. There are a number of predefined variables ($?,$*,$#), custom variables, and so on. Is the basic idea that the flexible script is indispensable. Learning to use variables can effectively reduce the script's writing effort.
How to make the program run better, sometimes need to use the branch to judge the loop structure control, here we learned,if,for,while,case and so on a series of process control structure. Use them to flexibly control the operation of the script.
use expect to implement the auto-answer part of human-computer interaction, which solves the interaction problem we need to input at the terminal.
through a week of learning, basically get a shell script to write ideas and methods. Of course, the deeper the understanding of system commands and systems , the easier it is for us to write Shell scripts, which requires my constant learning, thinking and summing-up.
the idea of writing shell scripts
Why write a shell script? Basically you need to implement an action, or let the shell script help us complete a series of tedious, or tedious operations. Reduce the intensity of your work. It is also the basic skill to realize automation operation and maintenance.
write a script, to start from the actual, do not write the function is very comprehensive, powerful. My principle is to be as good as possible. And then in the gradual improvement.
first of all to make your task clear, and then use the test machine to do the test, success, or encounter problems, in the thinking of solutions. Collect these commands, compose a shell script, solve the problem one after the other, or bypass it, the function of completing the script is the most important, do not do destructive action.
Sometimes it's a good way to get around, and it's more effective than solving.
implementation of shell scripts
the implementation of the script requires a painful process, and sometimes a seemingly simple shell function script may cost you a lot of thought. The best way is to do sections, sections, sections, tests, and a whole combination. Thin code. Adjust your ideas and improve your scripts.
don't try to write a complex script at once. Unless you're green with shell and system services like fire.
Limitations of Shell scripts and their practical application in the work
You can try to use shell scripting in your daily life to help you do a bit of simple work. such as the daily backup of your boot directory,etc directory. Work with scheduled tasks. Because sometimes, a lot of work is done by the shell script itself. You can't rely too much on the shell. Let it play its own merits. For example, some risky operations, such as disk format,LVM Management, the need for careful operation, or remote landing, such as more reliable points. In case you go down a script, the whole data is finished. Even though the script was written and tested by us. No one can guarantee that it will not be such a problem.
Finally, through a week of study, I initially understand the Shell script writing methods, their own operating system, and various functions of the implementation methods and shell script implementation methods, the method must be carefully studied and summarized. These are my little experience and experience. There are many shortcomings and shortcomings, hope that teachers and students to help.
ren hao . Beijing
This article from "11452553" blog, declined reprint!
Shell a week of learning experience