Shell is the basic operation of Linux, it must be learned. To learn a shell is to learn Linux commands.
Perl has a strong regular expression support, very tough for text processing, playing Linux has to learn.
python acts as an object-oriented, language that can be used as a Linux script to help you get your work done well.
Shell translated into shell means that it is wrapped in the outer layer of the Linux kernel, an operating system through a series of Linux commands related to the instructions of the Man-machine interface. The shell can combine a series of Linux commands with its conditional statements and loop statements to form a process-oriented program, Shell script, to achieve some of the more complex functions. All in all, the shell is a general concept of the
Linux command set, and is a human-machine interface that belongs to the command line
Shell scripts are very important in the operation of Linux system administrators. The following author will take you officially into the shell script World bar.
So far, you know what a shell script is. If you understand the best, do not understand it does not matter, I believe that with the depth of learning you will become more and more understand what is the shell script. First, it is a script and cannot be used as a formal programming language. Because it runs in the Linux shell, it's called a shell script. To be blunt, a shell script is a collection of commands. For example, I want to do this: 1 go to the/tmp/directory, 2 list all the file names in the current directory, 3, copy all the current files to the/root/directory, 4, and delete all the files in the current directory. The simple 4 step requires you to knock 4 times in the Shell window, pressing 4 times to enter. This is not very troublesome. Of course this 4-step operation is very simple, if it is more complex command settings need dozens of operations. In that case, it would be troublesome to knock on the keyboard one time. So you might want to record all of the actions in a document and then call the commands in the document so that you can do it in one step. In fact, this document is a shell script, but this shell script has its special format.
The shell script can help us to manage the server conveniently, because we can specify a task scheduled to execute a certain shell script implementation we want to need. This is a very proud thing for Linux system administrators. Now the 139 mailbox is very easy to use, send an email at the same time can also send a message to the user, using this, we can deploy on our Linux server monitoring shell script, such as network card traffic is abnormal or server Web server stopped can send an email to the administrator, At the same time sent to the administrator a message to the alert so that we can promptly know the server problem.
There is a problem that needs to be agreed, and any custom scripting suggestions are put into the/usr/local/sbin/directory, so that you can better manage the document, and then take over your admin to know where to put the custom scripts to facilitate maintenance.
" the basic structure of the shell script and how to execute "
Shell scripts are usually named after. SH, this is not to say without. sh This script can't execute, it's just a habit of everyone. So, later you find out. SH is a suffix file then it must be a shell script. The first line in test.sh must be "#! /bin/bash "It means that the file is using bash syntax. If you do not set the row, your shell script cannot be executed. ' # ' represents a comment, as described in the previous story. Followed by some comments about the script and the author and creation date or version, and so on. Of course these comments are not necessary, if you are lazy, you can omit, but the author does not recommend omitted. Because as you work more hours, you'll be writing more and more shell scripts, and if one day you look back at a script you wrote, you're likely to forget what the script is for and when it's written. So it is necessary to write a note. In addition, the system administrator is not your one, if the other administrator to see your script, he does not understand it is very depressing. The script is further down to the command you want to run.
The execution of the shell script is very simple, direct "sh filename" can, in addition you can also execute
By default, the document we edit with VIM does not have execute permissions, so we need to add an execution permission so that we can use './filename ' to execute the script. In addition, using the SH command to execute a shell script can be added with the-X option to see the script execution, which helps us debug this script where there is a problem.
The shell script uses the ' Date ' command, which is used to print the time of the current system. In fact, the date usage is very high in the shell script. There are several options that the writer often uses in a shell script:
%y represents the year,%m for the month,%d for the date,%h for hours,%m for minutes, and%s for seconds
Pay attention to the difference between%y and%y.
The-D option is also often used, it can print n days before or n days of the date, of course, can also print n months/years ago or after the date.
The other week is also used
"Shell variables in the script "
Using variables in a shell script it seems our script is more professional and more like a language, a joke, the role of variables is certainly not for professional. If you write a 1000-line shell script, and a command or path occurs hundreds of times in the script. Suddenly you think the path is not to change, it is not to change hundreds of times. You can use the batch substitution command, but it's also cumbersome, and the script looks bloated. The role of variables is to solve this problem.
If you use inverted quotes in test2.sh, do you remember how it works? The ' d ' and ' D1 ' appear as variables in the script, defining the variable as "variable name = value of variable". When referencing a variable in a script, you need to add the ' $ ' symbol, which is consistent with the previous custom variable in the shell. Let's take a look at the script execution results.
Here we use the shell to compute the two-digit and.
Mathematical calculations should be enclosed in ' [] ' and take a ' $ ' outside. The script results are:
The shell script can also interact with the user.
This uses the read command to get the value of the variable, followed by the variable name, from the standard input. "Read X" means that the value of the x variable needs to be entered by the user through the keyboard. The script execution process is as follows:
We might add the-X option and take a look at this execution:
There are more concise ways to test4.sh.
The READ-P option resembles the effect of ECHO. Implemented as follows:
Have you ever used a command like this? The/etc/init.d/iptables file in front of "/etc/init.d/iptables restart" is actually a shell script, why can I follow a "restart"? This involves the default variables of the shell script. In fact, shell scripts can be followed by variables and can be followed by multiple. I might as well write a script and you'll see.
The implementation process is as follows:
In the script, you wouldn't wonder where it came from $ and $, this is actually the default variable for the shell script, where the value is 1 of the input at the time of execution, and the $ $ value is the $ in the execution, of course, the default variable of a shell script is unlimited, this time you understand. There is another $ $, but it represents the name of the script itself. You might as well modify the script.
You must have guessed the results of the execution.
"Shell logical judgments in Scripts "
If you have studied C or other languages and believe you will not be unfamiliar with if, in the shell script we can also use if logic to judge. The basic syntax for if judgments in the shell is:
1 ) does not take Else
if Judgment statement; Then
Command
Fi
In the if1.sh ((a<60)) This form, which is a unique format in the shell script, with a parenthesis or not all the error, please remember this format. The results of the execution are:
2 ) with Else
if Judgment statement; Then
Command
Else
Command
Fi
The results of the execution are:
3 ) with elif
If&nbs