What I want to share with you today is the "shell script", one of the knowledge points I learned in the nineth day of operations, and I've seen the teacher write some simple scripts before class, when I felt that the script was a very tall thing, but when I really started learning, I found that writing a script was like a programming process. In this process he will apply to some of the command statements we have learned before, which requires that we have mastered these statements and skillfully applied to the actual going, but before the application we should still know how to build a script and edit and save the exit and other operations:
The first step: Before we edit, we need to know that the tool we use is a vim text editor, and then we select a path to store our script files. Generally we will cd/bin/app into the app Directory
Second step: We should create a compliant file in the app directory or our vim will open the file when the file Wen Yan, when it does not exist it will help us create a new file according to your personal requirements, and then will enter.
The third step: we enter after the default will enter the command mode, in the command mode we are unable to edit, but we can do some command operation:
Undo and Modify the command mode:
U Undo Recent Changes
#u many changes before undoing
U undo the modification of all this line after the cursor falls on this line
Ctrl-r Redo the final undo change
. Repeat previous action
N. Repeat the previous action n times
Multi-file Multi-file mode:
: Next Next
: Prev Before a
: First One
: Last One
: Wall Save All
: Qall Quit all
: Wqall Save and exit all
The fourth step: in the command mode we press I will enter the editing mode, which is what we call shell programming, and the program is divided into instructions and data there are two programming styles, one is the process is: the instruction to Center. Data Service to instructions. The other is object-oriented: data-centric directives serve data. The first line of the shell script has the following requirements:
First line Shabang mechanism
#! /bin/bash
#!/usr/bin/python
#!usr/bin/perl
The fifth step: When editing the program we can declare some variables, variables are divided into strong type and weak type, shell variables are: Local variables, environment variables, local variables, position variables, special variables and so on. We declare that the variable we have is available to us, and when we refer to a variable that is not declared, an error occurs.
On foot: When we finish writing the script we need to save the exits in the following ways:
First we press ESC to enter command mode and press: Enter exit Mode:
Q Only exits
W Save
Q! Force exit
Wq or x Save and exit
Finally, we need to execute the script with execute permission before we complete the script chmod +x file then also detects syntax errors in the script bash-n/etc/passwd/test.sh then debug execution bash-x/etc/passwd/te st.sh If there are no errors, we can only count the completion of a script.
A brief introduction to shell scripts