91超碰碰碰碰久久久久久综合_超碰av人澡人澡人澡人澡人掠_国产黄大片在线观看画质优化_txt小说免费全本

溫馨提示×

溫馨提示×

您好,登錄后才能下訂單哦!

密碼登錄×
登錄注冊×
其他方式登錄
點擊 登錄注冊 即表示同意《億速云用戶服務條款》

UNIX/Linux 系統管理技術手冊閱讀(十一)

發布時間:2020-03-19 19:35:46 來源:網絡 閱讀:376 作者:cix123 欄目:系統運維

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.

  好的系統管理員都要寫腳本。腳本以標準和自動的方式履行系統管理員的繁雜事務,藉此把管理員的時間節省出來,以花在更重要和更有意思的任務上。從某種意思上講,腳本也是一種低質量的文檔,因為它們充當了一種權威提綱,提綱里列出完成特殊任務所需的步驟。

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.

  從復雜性來看,系統管理腳本的范圍很廣,簡單得只封裝幾條靜態命令,大到一處重要的軟件項目,為整個站點管理主機配置和管理性數據。在本書里,我們所感興趣的主要是系統管理員通常會碰到的較小的日常腳本項目。因此,對于大的項目才需要的支持功能(例如,bug追蹤和設計評審),我們不會講得太多。

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.

  系統管理腳本應該注重兩點,即編程人員的開發效率和代碼的清晰可讀性。計算效率不應該成為關注重點,但這不應該成為草率行事的借口,而是要認識到,很少需要在意一個腳本是在半秒還是兩秒內運行完。優化腳本獲得的回報都非常低,甚至對通過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.

  長期以來,編寫系統管理腳本的標準語言是shell所定義的語言。在大多數系統上,默認的shell都是由bash(即“Bourne agin”shell),但是在幾種不多的UNIX系統上,也用sh(最初的Bourne shell)和ksh(Korn shell)。shell腳本一般用于輕量級的任務,如自動執行一系列命令,或者把幾個過濾器組合起來處理數據。

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,所以shell腳本可移值性相當好,除了它們調用的命令之外,要依賴的東西也不多。無論是否選擇shell來編寫腳本,都會碰到shell;大多數環境都包括已有sh腳本的強大補充,系統管理員會頻頻閱讀、理解和調整這些腳本。

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.

  對于更為復雜高端的腳本來說,建議轉而采用一種真正的編程語言來寫,像perl或者python這樣的語言,它們兩者都很適合于系統管理工作。這兩種語言融入的設計理念比shell領先20年,它們的字處理功能(對于系統管理員來說,價值難以估量)如此強大,sh在它們面前黯然失色。

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.

  Perl和Python的主要缺點在于,建立它們的環境要麻煩一點,尤其是要用到的第三方庫,而庫里又包含已經編譯好的部件的時候。shell沒有模塊結構,也沒有第三方的庫,因此避開了這個特殊的問題。

This chapter takes a quick look at bash, Perl, and Python as languages for scripting, along with regular expressions as a general technology.

  本章簡要介紹了bash、Perl和Python作為腳本編程語言的用法,以及正則表達式這種通用的技術。


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 shell的基礎知識

  在我們開始介紹Shell的腳本編輯之前,讓我們先看看shell的一此基本特性和語法。不管讀者正在使用的是何種平臺,本節都適用于sh大家庭里的所有主流shell(包括bash和ksh,但不包括csh或者tesh)。嘗試一下自己不熟悉的sh形式,做做實驗吧!


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. <Control-E> goes to the end of the line and <Control-A> to the beginning. <Control-P> steps backward through recently executed commands and recalls them for editing. <Control-R> 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 <Esc> 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 <Esc> k. Want emacs editing mode back again?

  $ set -o emacs

2.1.1 編輯命令

  我們已經注意到一點,太多人都用方向鍵來編輯命令行。但讀者朋友不會在文字編輯器里這么做,對嗎?

  如果喜歡emacs,那么在編輯命令歷史的時候,所有emacs基本命令都用得上,用<Control-E>到行尾,用<Control-A>到命令行的開頭,用<Control-P>一條一條回退到最近執行過的命令,重新把它們調出來進行編輯。用<Control-R>增量搜索命令歷史找出老命令。

  如果喜歡vi,那么用下面的命令就能讓shell的命令行編輯進入vi模式:

  $ set -o vi

  和在vi里一樣,編輯操作也是有模式的;不過,一開始會進入輸入模式。按ESC鍵開始離開輸入模式,按“i”鍵重新進入輸入模式。在編輯模式下“w”鍵向前進一個單詞,“FX”在本行里找到下一個X,等等。用<ESC>k可以遍歷過去輸入的命令。想要再次回到emacs編輯代碼,輸入:

  $ 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 管理和重定向

  每個進程都至少有3個信息:“標準輸入”、標準輸出、和“標準出錯”。內核給每個進程都設置了3個信道,所以進程本身不必知道這三個信道通到哪里。舉例來說,它們可能連接到一個終端窗口、一個網絡連接,或者屬于另一個進程的信道。

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有一個統一的I/O模型,在這個模型中,每個信道都以一個整數來命名,它叫做文件描述符。分配給一個信道整數值到底是哪個,通常而主沒有意義,但要保證STDIN、STDOUT和STDERR對應文件描述符0,1和2,所以保險的做法是,用數字來引用這三個信道。在交互式的終端窗口里,STDIN一般讀取鍵盤的輸入,而STDOUT和STDERR把它們的輸出寫到屏幕上。

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.

  大多數命令都接受從STDIN來輸入,并且把自己的輸出寫到STDOUT,而把錯信息寫到STDERR。有了這樣的約定,用戶就能把命令像積木一樣串起來,創建出混合管道。

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將<、>和》解釋成指令,用來把一條命令的輸入或者輸出重新定向到一個文件。<這個符號把這條命令的STDIN和已有的某個文件的內容聯系起來。符號>和》則重定向STDOUT;>會替換文件的現有內容,而》則給文件追加內容。例如,下面的命令

  $ 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>.

  為了把STDOUT和STDERR都重定向到同一個地方,可以用>&這個符號,僅僅重定向STDERR的話,則用2>。

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

  命令find演示了想要分開處理STDOUT和STDERR的原因,因為它會在兩個信道提供輸出,特別是以非特權用戶身份運行的時候。例如,像下面這條命令

  $ 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

  通常會導致很多“permission denied”這樣的出錯信息,從而把真正的結果淹沒在混亂的輸出里了。要消除所有出錯消息,可以用這條命令

  $ 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

  在這個版本的命令里,只有真正匹配的結果(該用戶對父目錄有讀權的地方)才會出現在終端窗口中。要把匹配路徑的清單保存在一個文件里,可以試試下面的命令

  $ 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.

  這一行命令把匹配的路徑發到/tmpcorefile這個文件,丟棄出錯消息,向終端窗口什么都不發。

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

  要把一條命令的STDOUT連接到另一條命令的STDIN上,可以用|這個符號,它常叫做管道。下面是一些例子

  $ 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.

  第一個例子運行ps產生一份進程清單,由管道送給grep命令選出包含httpd這個詞的若干行。grep命令的輸出沒有重定向,所以匹配的結果都出現在終端窗口里。

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.

  第二個例子用cut命令從/etc/passwd文件里把每個用戶的shell的路徑選出來。接著,列出的shell的路徑都通過sort -u進行處理,產生的清單中,路徑名不但依次排序,且路徑名只出現一次。

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

  要讓第二條命令只有在第一條命令成功完成之后才執行,可以用一個&&符號把兩條命令隔開。例如

  $ 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.

  這條命令當且僅當/tmp/t2成功送入打印隊列之后,才會刪除/tmp/t2。在這里,lpr命令產生的退出碼為0的話,就算它執行成功,所以,如果讀者已經習慣了其他編程語言中的“短路”計算,而這里用一個表示“邏輯與”的符號,那么就可能造成混亂。不要想得太多;僅僅把它當做一個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.

  相反,||這個符號表明,只有前一條命令執行不成功(產生了一個非零的退出碼)時,才執行后面的腳本。

  在一個腳本里,可以用反斜線把一條命令分成多行來寫,從而把出錯處理代碼和命令管道的其他部分區分開來。

  cp --preserve --recursive /etc/* /spare/backup \

       || echo "Did NOT make backup"

  要實現相反的效果--將多條命令整合在一行里--可以用分號作為語句分隔符。


向AI問一下細節

免責聲明:本站發布的內容(圖片、視頻和文字)以原創、轉載和分享為主,文章觀點不代表本網站立場,如果涉及侵權請聯系站長郵箱:is@yisu.com進行舉報,并提供相關證據,一經查實,將立刻刪除涉嫌侵權內容。

AI

浠水县| 吕梁市| 达孜县| 乌什县| 正安县| 奉化市| 恩施市| 鹤岗市| 鞍山市| 信阳市| 治多县| 庐江县| 永德县| 昌黎县| 五台县| 进贤县| 普宁市| 衡阳市| 嵩明县| 盈江县| 措勤县| 广汉市| 叙永县| 巴南区| 德钦县| 西华县| 磴口县| 英吉沙县| 清苑县| 读书| 洪湖市| 宜兰市| 汝城县| 阿图什市| 麦盖提县| 邓州市| 布尔津县| 屏东县| 建瓯市| 西安市| 和顺县|