In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-04-01 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Servers >
Share
Shulou(Shulou.com)06/03 Report--
2016.11.7
2 Scripting and the Shell
Good system administrators write scripts. Scripts standardize and automate the performance of administrative chores and free up admins' time for more important and more interesting tasks. In a sense, scripts are also a kind of low-rent documentation in that they act as an authoritative outline of the steps needed to complete a particular task.
All good system administrators write scripts. The script performs the complex tasks of the system administrator in a standard and automated manner, thereby saving the administrator's time to spend on more important and interesting tasks. In a sense, scripts are also low-quality documents because they serve as an authoritative outline of the steps needed to accomplish a particular task.
In terms of complexity, administrative scripts run the gamut from simple ones that encapsulate a few static commands to major software projects that manage host configurations and administrative data for an entire site. In this book we're primarily interested in the smaller, day-to-day scripting projects that sysadmins normally encounter, so we don't talk much about the support functions (e.g.bug tracking and design review) that are needed for larger projects.
From the complexity point of view, the scope of the system management script is very wide, as simple as encapsulating a few static commands to an important software project to manage host configuration and administrative data for the entire site. In this book, we are mainly interested in smaller day-to-day scripting projects that system administrators usually encounter. Therefore, we won't say much about the support features needed for large projects (for example, bug tracking and design review).
Administrative scripts should emphasize programmer efficiency and code clarity rather than computational efficiency. This is not an excuse to be sloppy, but simply a recognition that it rarely matters whether a script runs in half a second or two seconds. Optimization can have an amazingly low return on investment, even for scripts that run regularly out of cron.
System management scripts should focus on two points, namely, the development efficiency of programmers and the clear readability of the code. Computational efficiency should not be a focus, but it should not be an excuse for haste, but to realize that there is little need to care about whether a script runs in half a second or two seconds. The returns for tuning scripts are very low, even for scripts that run regularly through cron.
For a long time, the standard language for administrative scripts was the one defined by the shell. Most systems' default shell is bash (the "Bourne-again" shell), but sh (the original Bourne shell) and ksh (the Korn shell) are used on a few UNIX systems. Shell scripts are typically used for light tasks such as automating a sequence of commands or assembling several filters to process data.
For a long time, the standard language for writing system management scripts is the language defined by shell. On most systems, the default shell is by bash (that is, "Bourne agin" shell), but on a few UNIX systems, sh (the original Bourne shell) and ksh (Korn shell) are also used. Shell scripts are typically used for lightweight tasks, such as automating a series of commands or combining several filters to process data.
The shell is always available, so shell scripts are relatively portable and have few dependencies other than the commands they invoke. Whether or not you choose the shell, the shell may choose you: most environments include a hefty complement of existing sh scripts, and those scripts frequently need to be read, understood, and tweaked by administrators.
Shell is available on all operating systems, so shell scripts are quite portable and don't have much to rely on other than the commands they invoke. Whether or not you choose shell to write scripts, you will encounter a powerful complement to most shell; environments that include existing sh scripts, which system administrators read, understand, and adjust frequently.
For more sophisticated scripts, it's advisable to jump to a real programming language such as Perl or Python, both of which are well suited for administrative work. These languages incorporate a couple of decades' worth of language design advancements relative to the shell, and their text processing facilities (invaluable to administrators) are so powerful that sh can only weep and cower in shame.
For more complex and high-end scripts, it is recommended to switch to a real programming language, such as perl or python, both of which are well suited for system administration. These two languages incorporate design concepts 20 years ahead of shell, and their word processing capabilities (which are invaluable to system administrators) are so powerful that sh is overshadowed by them.
The main drawback to Perl and Python is that their environments can be a bit
Fussy to set up, especially when you start to use third-party libraries that have compiled components. The shell skirts this particular issue by having no module structure and no third-party libraries.
The main disadvantage of Perl and Python is that setting up their environment is a bit more cumbersome, especially when a third-party library is used and the library contains compiled parts. Shell has no modular structure and no third-party libraries, so it avoids this particular problem.
This chapter takes a quick look at bash, Perl, and Python as languages for scripting, along with regular expressions as a general technology.
This chapter briefly introduces the use of bash, Perl, and Python as scripting languages, as well as the general technique of regular expressions.
2.1 SHELL BASICS
Before we take up shell scripting, let's review some of the basic features and syntax of the shell. The material in this section applies to all major shells in the sh lineage (including bash and ksh, but not csh or tcsh), regardless of the exact platform you are using. Try out the forms you're not familiar with, and experiment!
2.1 basic knowledge of shell
Before we move on to script editing for Shell, let's take a look at one of the basic features and syntax of shell. No matter what platform the reader is using, this section applies to all major shell in the sh family (including bash and ksh, but not csh or tesh). Try the sh form that you are not familiar with and do the experiment!
2016.11.11 15:10
Command editing
We've watched way too many people edit command lines with the arrow keys. You
Wouldn't do that in your text editor, right?
If you like emacs, all the basic emacs commands are available to you when you're editing history. Goes to the end of the line and to the beginning. Steps backward through recently executed commands and recalls them for editing. Searches incrementally through your history to find old commands.
If you like vi, put your shell's command-line editing into vi mode like this:
$set-o vi
As in vi, editing is modal; however, you start in input mode. Type to leave input mode and "I" to reenter it. In edit mode, "w" takes you forward a word, "fX" finds the next X in the line, and so on. You can walk through past command history entries with k. Want emacs editing mode back again?
$set-o emacs
2.1.1 Editing commands
We have noticed that too many people use arrow keys to edit the command line. But readers don't do that in text editors, do they?
If you like emacs, you can use all the basic emacs commands when editing command history, use the end of the line, use the beginning of the command line, roll back to the most recently executed commands one by one, and call them up again for editing. Use the incremental search command history to find the old command.
If you like vi, you can put the command line editor of shell into vi mode with the following command:
$set-o vi
As in vi, editing has a pattern; however, it will enter input mode at first. Press the ESC key to start leaving input mode and press the "I" key to re-enter input mode. In edit mode, the "w" key advances a word, "FX" finds the next X in the line, and so on. You can use k to iterate over commands entered in the past. To go back to emacs editing code again, type:
$set-o emacs
Pipes and redirection
Every process has at least three communication channels available to it: "standard input" (STDIN), "standard output" (STDOUT), and "standard error" (STDERR). The kernel sets up these channels on the process's behalf, so the process itself doesn't necessarily know where they lead. They might connect to a terminal window, a file, a network connection, or a channel belonging to another process, to name a few possibilities.
2.1.2 Management and redirection
Each process has at least three pieces of information: standard input, standard output, and standard error. The kernel sets three channels for each process, so the process itself does not need to know where these three channels go. For example, they may be connected to a terminal window, a network connection, or a channel belonging to another process.
UNIX has a unified I/O model in which each channel is named with a small integer called a file descriptor. The exact number assigned to a channel is not usually significant, but STDIN, STDOUT, and STDERR are guaranteed to correspond to file descriptors 0, 1, and 2, so it's safe to refer to these channels by number. In the context of an interactive terminal window, STDIN normally reads from the keyboard and both STDOUT and STDERR write their output to the screen.
UNIX has a unified Ihand O model, in which each channel is named by an integer, which is called a file descriptor. Which integer value is assigned to a channel is usually meaningless, but make sure that STDIN, STDOUT, and STDERR correspond to file descriptors 0J1 and 2, so it is safe to refer to these three channels with numbers. In interactive terminal windows, STDIN generally reads keyboard input, while STDOUT and STDERR write their output to the screen.
Most commands accept their input from STDIN and write their output to STDOUT. They write error messages to STDERR. This convention lets you string commands together like building blocks to create composite pipelines.
Most commands accept input from STDIN and write their output to STDOUT and error information to STDERR. With such a convention, users can string commands together like building blocks to create hybrid pipes.
The shell interprets the symbols, and > > as instructions to reroute a command's input or output to or from a file. A
< symbol connects the command's STDIN to the contents of an existing file. The >And > symbols redirect STDOUT; > replaces the file's existing contents, and > > appends to them. For example, the command
$echo "This is a test message." > / tmp/mymessage
Shell interprets and "as instructions to redirect the input or output of a command to a file. And "then redirect STDOUT; > replaces the existing contents of the file, while" appends the contents to the file. For example, the following command
$echo "This is a test message." > / tmp/mymessage
Stores a single line in the file / tmp/mymessage, creating the file if necessary. The command below emails the contents of that file to user johndoe.
$mail-s "Mail test" johndoe
< /tmp/mymessage 在/tmp/mymessage这个文件里存入一行内容,如果必要,还会创建这个文件。下面的命令把该文件的内容用电子邮件发给用户johndoe。 $ mail -s "Mail test" johndoe < /tmp/mymessage To redirect both STDOUT and STDERR to the same place, use the >& symbol. To
Redirect STDERR only, use 2 >.
To redirect both STDOUT and STDERR to the same place, you can use the symbol > &, or 2 > if you just redirect STDERR.
The find command illustrates why you might want to handle STDOUT and
STDERR separately because it tends to produce output on both channels, especially when run as an unprivileged user. For example, a command such as
$find /-name core
The command find demonstrates why you want to handle STDOUT and STDERR separately because it provides output on both channels, especially when running as an unprivileged user. For example, like this command
$find /-name core
Usually results in so many "permission denied" error messages that genuine hits get lost in the clutter. To discard all the error messages, you can use
$find /-name core 2 > / dev/null
It usually leads to a lot of error messages such as "permission denied", drowning the real result in the chaotic output. To eliminate all error messages, you can use this command
$find /-name core 2 > / dev/null
In this version, only real matches (where the user has read permission on the parent directory) come to the terminal window. To save the list of matching paths to a file, try
$find /-name core > / tmp/corefiles 2 > / dev/null
In this version of the command, only the results that really match (where the user has access to the parent directory) will appear in the terminal window. To save the list of matching paths in a file, try the following command
$find /-name core > / tmp/corefiles 2 > / dev/null
This command line sends matching paths to / tmp/corefiles, discards errors, and sends nothing to the terminal window.
This command sends the matching path to the file / tmpcorefile, discards the error message, and sends nothing to the terminal window.
To connect the STDOUT of one command to the STDIN of another, use the | symbol, commonly known as a pipe. Some examples:
$ps-ef | grep httpd
$cut-d:-f7 < / etc/passwd | sort-u
To connect the STDOUT of one command to the STDIN of another command, you can use the | symbol, which is often called a pipe. Here are some examples
$ps-ef | grep httpd
$cut-d:-f7 < / etc/passwd | sort-u
The first example runs ps to generate a list of processes and pipes it through the grep command to select lines that contain the word httpd. The output of grep is not redirected, so the matching lines come to the terminal window.
The first example runs ps to produce a list of processes, which is piped to the grep command to select lines containing the word httpd. The output of the grep command is not redirected, so the matching results appear in the terminal window.
The cut command in the second example picks out the path to each user's shell
From / etc/passwd. The list of shells is then sent through sort-u to produce a
Sorted list of unique values.
The second example uses the cut command to select the path to each user's shell from the / etc/passwd file. Next, the paths of the listed shell are processed by sort-u, and in the resulting list, the path names are not only sorted in order, but also appear only once.
To execute a second command only if its precursor completes successfully, you
Can separate the commands with an & & symbol. For example
$lpr / tmp/t2 & & rm / tmp/t2
For the second command to be executed only after the first command is successfully completed, you can separate the two commands with a & & symbol. For example
$lpr / tmp/t2 & & rm / tmp/t2
Removes / tmp/t2 if and only if it is successfully queued for printing. Here, the success of the lpr command is defined as its yielding an exit code of zero, so the use of a symbol that suggests "logical AND" for this purpose may be confusing if you're used to short-circuit evaluation in other programming languages. Don't think about it too much; just accept it as a shell idiom.
This command deletes / tmp/t2 if and only if / tmp/t2 is successfully sent to the print queue. Here, the lpr command produces an exit code of 0, even if it executes successfully, so if the reader is used to "short-circuit" calculations in other programming languages, and here a symbol for "logical and" is used, it may cause confusion. Don't think too much about it; just think of it as an idiom for shell.
Conversely, the | | symbol executes the following command only if the preceding
Command fails (produces a nonzero exit status).
In a script, you can use a backslash to break a command onto multiple lines, helping to distinguish the error-handling code from the rest of the command pipeline:
Cp-preserve-recursive / etc/* / spare/backup\
| | echo "Did NOT make backup" |
For the converse effect-multiple commands combined onto one line-you can
Use a semicolon as a statement separator.
Instead, the symbol indicates that the following script is executed only if the previous command is not executed successfully (a non-zero exit code is generated).
In a script, you can use a backslash to divide a command into multiple lines to distinguish the error handling code from the rest of the command pipeline.
Cp-preserve-recursive / etc/* / spare/backup\
| | echo "Did NOT make backup" |
To achieve the opposite effect-integrating multiple commands on one line-use a semicolon as a statement delimiter.
Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.
Views: 0
*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.
Continue with the installation of the previous hadoop.First, install zookooper1. Decompress zookoope
"Every 5-10 years, there's a rare product, a really special, very unusual product that's the most un
© 2024 shulou.com SLNews company. All rights reserved.