In daily linux system management, shell scripts are indispensable. If you do not write shell scripts, you are not a qualified administrator. At present, many organizations are required to write shell scripts when recruiting linux system administrators. Some organizations even use the ability to write shell scripts to measure the experience of the linux system administrator. There is only one purpose for this article, that is, to let you take shell scripts seriously. From the very beginning, you must master the basic knowledge and continue to practice. As long as you write shell scripts well, I believe that your linux job will be much easier. In this chapter, I will not introduce shell scripts in detail, but just bring you into the world of shell scripts, if you are very interested, please download relevant materials online or purchase related books from the bookstore.
Before learning shell scripts, you need to know a lot about shell, which is the basis for writing shell scripts, so I hope you can master it well.
【What isShell]
Simply put, it is the intermediate medium used by the system to interact with computer hardware. It is just a tool of the system. In fact, there is another layer between shell and computer hardware, that is, the system kernel. For example, if we compare computer hardware to a human body, while the system kernel is a human brain, shell seems more relevant to human facial features. Back to the computer, the user is not directly facing the computer hardware, but the shell. The user tells the shell command, and then the shell is transmitted to the system kernel, and then the kernel then controls the computer hardware to perform various operations.
The shell installed by default in the Redhat/CentOS system is bash, which is the enhanced version of sh (Bourne Shell. The Bourn Shell is the first shell to work, and the Founder is Steven Bourne. to commemorate it, it is called the Bourn Shell and the Shell is called. So what are the characteristics of bash?
1) record command history
The commands we have typed will be recorded in linux. By default, 1000 historical commands can be recorded. These commands are stored in the. bash_history file in the user's home directory. One thing you need to know is that the command running in the current shell will be saved to the. bash_history file only when the user exits the current shell normally.
An interesting character related to the command history is "!" . There are several common applications: (1 )!! (Two consecutive "!"), Indicates executing the previous command; (2 )! N (here n is a number) indicates the nth command in the execution command history, for example "! "100" indicates 100th commands in the execution history. (3 )! String (string greater than or equal to 1), for example! Ta, indicating the most recent command in the execution history starting with ta.
2) command and file name completion
I introduced this feature at the beginning of this tutorial. Remember? By the way, press the tab key. It can help you complete a command, or complete a path or a file name. Press the tab key twice in a row to list all commands or file names.
3) alias
As described earlier, alias is one of the unique functions of bash. We can use alias to alias a commonly used and long command to a simple and easy-to-remember command. If you do not want to use it, you can also use unalias to remove the alias function. If you press alias directly, you will see the current preset alias:
As you can see, the alias commands preset by the system are just a few. You can also customize the alias of the commands you want. The alias syntax is very simple. alias [command alias] = ['specific command '].
4) wildcard
In bash, you can use * to match zero or multiple characters? Match a character.
5) Input/Output
Input redirection is used to change the input of a command, and output redirection is used to change the output of a command. Output redirection is more commonly used. It is often used to input command results into files rather than on the screen. The command for entering redirection is <, the command for outputting redirection is>, and the error redirection 2> and append redirection>, which will be detailed later.
6) pipeline operator
The pipeline character "|" has been mentioned earlier, that is, the result of running the preceding command is thrown to the subsequent command.
7) Job control.
When running a process, you can pause it (press Ctrl + z), then use the fg command to restore it, and use the bg command to run it in the background, you can also terminate it (press Ctrl + c ).
【Variable]
In the previous sections, I have introduced the environment variable PATH. This environment variable is a variable preset by shell. Generally, the variables preset by shell are in upper case. A variable. The simple point is to use a simple string to replace some special settings and data. Taking PATH as an example, this PATH replaces the absolute PATH setting of all common commands. Because the PATH variable is available, we do not enter the global PATH when running a command. Just press the command name. You can use the echo command to display the variable value.
Besides PATH, HOME, and LOGNAME, what are the preset environment variables?
Use the env command to list all system variables preset by the system. However, different login users have different values for these environment variables. Currently, the environment variable of the root account is displayed. The following describes common environment variables:
PATH determines the directories to which shell will look for commands or programs.
HOME main directory of the current user
HISTSIZE
LOGNAME: The Login Name of the current user.
HOSTNAME indicates the host name.
SHELL Type before Shell
Environment variables related to the LANG language, which can be modified in multiple languages
MAIL current user's MAIL storage directory
PWD Current Directory
The variables displayed by the env command are only environment variables. There are many preset variables in the system. You can use the set command to display all the preset variables in the system.
Due to space limitations, I did not show all the results in the above example. Set not only displays the system preset variables, but also displays them together with User-Defined variables. User-Defined variables? Yes. You can also define variables.
Although you can customize a variable, it can only take effect in the current shell. Do you believe you can try another shell?
Run the bash command to open another shell. The previously set myname variable does not exist. Exit the current shell and return to the original shell. The myname variable is still in. What should I do if I want to keep setting variables effective? There are two scenarios:
1) if you want to use this variable after logging on to all users in the system
Add "export myname = Aming" to the last line of the/etc/profile file and run "source/etc/profile. Run the bash command or run the su-test account.
2) you only want the current user to use this variable.
Add "export myname = Aming" to the last line of the. bashrc file in the user's main directory and run "source. bashrc. Log on to the test account again, and the myname variable will not take effect. The purpose of the source command above is to refresh the current configuration, that is, it can take effect without logging out and then logging on.
In the above example, I used "myname = Aming" to set the variable myname. What are the rules for setting custom variables in linux?
A. Set the variable format to "a = B", where a is the variable name, B is the content of the variable, and there cannot be spaces on both sides of the equal sign;
B. The variable name can only consist of English letters, numbers, and underscores, and cannot start with a number;
C. When the content of a variable contains special characters (such as spaces), single quotation marks are required;
In one case, you must note that the variable content contains single quotation marks, which requires double quotation marks.
D. If other commands are required to run the result in the variable content, you can use reverse quotation marks;
E. You can add the content of other variables in double quotation marks;
If you accidentally add double quotation marks into single quotation marks, you will not get the expected result.
Through the above examples, you may see the difference between single quotes and double quotes: when using double quotes, the special characters in the double quotes will not be removed ($ here ), when single quotes are used, all the special characters in the quotation marks lose their role.
In the previous example, the author used the bash command multiple times. If the bash command is run in the current shell, a new shell will be entered. This shell is the sub-shell of the original shell, you may want to use the pstree command to check it.
The pstree command prints all processes in the linux system in a tree structure. I have not listed them all. You can simply enter pstree to view them. After a variable is set in the parent shell, the variable does not take effect after entering the sub-shell. To make the variable take effect in the sub-shell, you need to use the export command, I have used it before.
Export is actually the meaning of this variable, so that the sub-shell of this shell also knows that the value of the variable abc is 123. if no variable name is added after the export, it declares all variables.
At the end of the statement together with our custom variables.
Previously, I explained how to set variables. What if I want to cancel a variable? Just enter "unset variable name.
After unset abc is used, echo $ abc is used to output NO content.
【Configuration files for system environment variables and personal environment variables]
I have talked about many system variables. in linux, where are these variables stored? Why do users automatically have these variables when they log on to shell?
/Etc/profile: This file presets several important variables, such as PATH, USER, LOGNAME, MAIL, INPUTRC, HOSTNAME, HISTSIZE, umas, and so on.
/Etc/bashrc: This file mainly defaults umask and ps1. This PS1 is the character in front of the command. For example, in my linux system, PS1 is [root @ localhost ~]. #. Check the PS1 value.
\ U is the user, \ h host name, \ W is the current directory, \ $ is the '#', and if it is a common user, it is displayed as '$'
In addition to two system-level configuration files, each user's home directory has several such hidden files:
. Bash_profile: Defines the user's personal path and the name of the Environment Variable file. Each user can use this file to input the shell information dedicated to his own use. When a user logs on, this file is executed only once.
. Bashrc: This file contains bash information dedicated to your shell. This file is read when you log on and each time you open a new shell. For example, you can write user-defined alias or custom variables to this file.
. Bash_history: Used to record command history.
. Bash_logout: When you exit shell, the file is executed. You can put some cleanup work in this file.
【Linux shellSpecial symbols in]
When you are learning linux, you may have been touched by a special symbol, such as "*". It is a wildcard number that represents zero or multiple characters or numbers. Next I will talk about common special characters.
1 .*: Represents zero or multiple characters or numbers.
There can be no or multiple characters after test. Either or none of them can be matched.
2 .?: Represents only one arbitrary character
Both numbers and letters can be matched.
3 .#: This symbol indicates the meaning of the description in linux, that is, the content after "#" is ignored in linux.
Insert "#" at the beginning or in the middle of the command, which will be ignored in linux. This symbol is used in many shell scripts.
4 .\: Remove the special characters (for example.
5. |: Pipeline operator, which has been said many times before, is used to throw the result of the command before the symbol to the command after the symbol. The subsequent commands mentioned here are not applicable to all commands. Generally, commands for document operations are commonly used, such as cat, less, head, tail, grep, cut, sort, wc, uniq, tee, tr, split, sed, awk and so on. grep, sed, and awk are the tools required for regular expressions.
6. $: in addition to the identifier used before the variable, there is also a wonderful use, that is, and '! .
'! Then, enter it in the current command! Then, test.txt is used.
1)Grep: Filter one or more characters, which will be detailed in subsequent content.
2) cut: Intercept a field
Syntax: cut-d "delimiter" [-cf] n here n is a number
-D: followed by double quotation marks.
-C: followed by the nth character
-F: The blocks to be followed
-D is followed by a separator. Here, a colon is used as the delimiter.-f 1 is the first segment. spaces between-f and 1 are optional.
-C can be followed by a number n, an interval n1-n2, or multiple numbers n1, n2, n3
3) sort: Used for sorting
Syntax: sort [-t separator] [-kn1, n2] [-nru] n1 <n2
-T separator: indicates the effect of-d in cut.
-N: Sorting by numbers only
-R: reverse sorting
-U: deduplication
-Kn1 and n2: Sort data from the n1 range to the n2 range. You can write only-kn1, that is, sort the n1 field.
4) wc: Count the number of lines, characters, and words of a document. Common options include:
-L: Number of Statistics rows
-M: Number of statistical characters
-W: count words
5)Uniq: Repeated rows. I only use one of the following options:
-C: count the number of repeated rows and write the number of rows in front
Note that before uniq, you need to sort it by sort before uniq. Otherwise, you will not get what you want, I have already sorted the above test, so I omitted that step.
6)Tee: Followed by the file name, similar to redirection ">", but the specific gravity orientation has an additional function. It is also displayed on the screen when the file is written to the following file.
7)Tr: Replacement character. It is often used to process special symbols in a document, such as the ^ M symbol in a DOS document. There are two common options:
-D: delete a character.-d is followed by the character to be deleted.
-S: Remove repeated characters
The most common is to change the lower case to uppercase: tr' [a-z] ''[A-Z]''
Of course, you can replace one character.
However, replacement, deletion, and deduplication are all for a single character and have certain limitations. If it is for a string, it will no longer work, so I suggest you simply understand this tr. In the future, you will learn more tools to implement string operations.
8)Split: Cut document, common options:
-B: Split the document by size, in bytes.
Format: for example, passwd is the prefix of the split file name, and the split file name is passwdaa, passwdab, passwdac...
-L: Split the document based on the number of lines
6.;: Semicolon. We usually press a command in one line and press enter to run it. What if we want to run two or more commands in one line? You need to add ";" between commands.
7 .~: User's home directory. If it is root, it is/root. If it is a common user, it is/home/username.
8 .&: If you want to put a command in the background for execution, you need to add this symbol. It is usually used when the command runs for a long time.
Jobs can be used to view the tasks executed in the background of the current shell. You can use fg to go to the front-end for execution. Here the sleep command is sleep, followed by a number, in seconds, in a shell script that is commonly used in loop.
Press CTRL + z to pause the task, and then enter bg To Go To The background again.
If you want to transfer a task to the foreground for execution under a multi-task scenario, the fg is followed by the task number. You can use the jobs command to obtain the task number.
9.>,>>, 2>, 2>: The redirection symbols> and> indicate the meaning of substitution and append respectively, and there are two symbols here: 2> and 2> indicate error redirection and error append redirection respectively, when we run a command to report an error, the error message is output to the current screen. to redirect to a text, use 2> or 2>.
10. []: Brackets. The Center is a combination of characters, representing any one of the intermediate characters.
11 .&&And|
As mentioned above, the semicolon is used as the delimiter between multiple commands. There are also two special symbols that can be used in the middle of multiple commands, that is, "&" and "| ". The following lists all the situations:
1) command1; command2
2) command1 & command2
3) command1 | command2
When ";" is used, command2 will be executed no matter whether command1 is successfully executed or not; when "&" is used, command2 will be executed only after command1 is successfully executed; otherwise, command2 will not be executed; when "|" is used, command2 is not executed after command1 is successfully executed; otherwise, command2 is executed. In short, there is always one command for command1 and command2 to be executed.