UNIX/Linux系統(tǒng)管理技術(shù)手冊閱讀(十二)-創(chuàng)新互聯(lián)

2016.11.17

創(chuàng)新互聯(lián)公司堅持“要么做到,要么別承諾”的工作理念,服務(wù)領(lǐng)域包括:成都網(wǎng)站建設(shè)、網(wǎng)站設(shè)計、企業(yè)官網(wǎng)、英文網(wǎng)站、手機端網(wǎng)站、網(wǎng)站推廣等服務(wù),滿足客戶于互聯(lián)網(wǎng)時代的沈陽網(wǎng)站設(shè)計、移動媒體設(shè)計的需求,幫助企業(yè)找到有效的互聯(lián)網(wǎng)解決方案。努力成為您成熟可靠的網(wǎng)絡(luò)建設(shè)合作伙伴!

Variables and quoting

Variable names are unmarked in assignments but prefixed with a dollar sign when their values are referenced. For example:

 $ etcdir='/etc'

 $ echo $etcdir

 /etc

Do not put spaces around the = symbol or the shell will mistake your variable name for a command name.

2.1.3 變量和引用

 變量名在賦值的時候沒有標永,但在訪問它們的值的時候要在變量名之前加一個$符。例如:

 $ etcdir='/etc'

 $ echo $etcdir

 /etc

 不要在等號兩邊留空白,否則shll會把變量名誤以為是命令名。

When referencing a variable, you can surround its name with curly braces to clarify to the parser and to human readers where the variable name stops and other text begins; for example, ${etcdir} instead of just $etcdir. The braces are not normally required, but they can be useful when you want to expand variables inside double-quoted strings. Often, you’ll want the contents of a variable to be followed by literal letters or punctuation. For example,

 $ echo "Saved ${rev}th version of mdadm.conf."

 Saved 8th version of mdadm.conf.

 引用一個變量,可用花括號把這個變量的名字括起來,讓分析程序和閱讀代碼的人能清楚地知道變量名的起止位置;例如,用${etcdir}代替$etcdir。正常情況下不要求有花括號,但是如果想要在雙引號起來的字符串里擴展變量,它們就會派上用場了。因為人們經(jīng)常想要在一個變量的內(nèi)容之后跟著字母或者標點符號。例如:

 $ echo "Saved ${rev}th version of mdadm.conf."

 Saved 8th version of mdadm.conf.

There’s no standard convention for the naming of shell variables, but all-caps names typically suggest environment variables or variables read from global configuration files. More often than not, local variables are all-lowercase with components separated by underscores. Variable names are case sensitive.

 給shell變量起名字沒有標準的命令規(guī)范,但如果變量名的所有字母都大寫,一般表明該變量是環(huán)境變量,或者是從全局配置文件里讀取的變量。本地變量則多半是所有字母都小寫,而且在變量名的各個部分之間用下劃線隔開。變量名區(qū)分大小寫。

Environment variables are automatically imported into bash’s variable namespace, so they can be set and read with the standard syntax. Use export varname to promote a shell variable to an environment variable. Commands for environment variables that you want to set up at login time should be included in your ~/.profile or ~/.bash_profile file. Other environment variables, such as PWD for the current working directory, are maintained automatically by the shell.

 環(huán)境變量會被自動導入bash的變量名空間,所以它們可以用標識的語法來設(shè)置和讀取。命令exportvarname將一個shell變量提升為一個環(huán)境變量。用來在用戶登錄時設(shè)置環(huán)境變量的那些命令,都應該放在該用戶的~/.profile 或 ~/.bash_profile這兩個文件里。而其他像PWD(代表當前工作目錄)這樣的環(huán)境變量都由shell自動維護。

The shell treats strings enclosed in single and double quotes similarly, except that double-quoted strings are subject to globbing (the expansion of filename-matching metacharacters such as * and ?) and variable expansion. For example:

 $ mylang="Pennsylvania Dutch"

 $ echo "I speak ${mylang}."

 I speak Pennsylvania Dutch.

 $ echo 'I speak ${mylang}.'

 I speak ${mylang}.

 對于單引號和雙引號括起來的字符串而言,shell以相似的方式處理它們,例外之處在于雙引號括起來的變量可以進行替換(*和?這樣的文件名匹配元字符做擴展)和變量擴展。例如:

 $ mylang="Pennsylvania Dutch"

 $ echo "I speak ${mylang}."

 I speak Pennsylvania Dutch.

 $ echo 'I speak ${mylang}.'

 I speak ${mylang}.

Back quotes, also known as back-ticks, are treated similarly to double quotes, but they have the additional effect of executing the contents of the string as a shell command and replacing the string with the command’s output. For example,

 $ echo "There are `wc -l /etc/passwd` lines in the passwd file."

 There are 28 lines in the passwd file.

 左引號也叫做撇號,對它的處理和引號類似,但是它們還有其他作用,即能夠把串的內(nèi)容按一條shell命令來執(zhí)行,并且用該命令的輸出來替換這個字符串。例如:

 $ echo "There are `wc -l /etc/passwd` lines in the passwd file."

 There are 28 lines in the passwd file.

Common filter commands

Any well-behaved command that reads STDIN and writes STDOUT can be used

as a filter (that is, a component of a pipeline) to process data. In this section we briefly review some of the more widely used filter commands (including some used in passing above), but the list is practically endless. Filter commands are so team oriented that it’s sometimes hard to show their use in isolation.

2.1.4 常見的過濾命令

 任何“從STDIN讀入數(shù)據(jù),向STDOUT輸出結(jié)果”這樣循規(guī)蹈距的命令,都可以當作一個過濾器(也就是說,管理的一個環(huán)節(jié))來處理數(shù)據(jù),在這一小節(jié),我們簡要回顧一些使用較為廣泛的過濾器命令(包括上面已經(jīng)用到過的一些命令)但是這樣的過濾器命令實際上是無窮無盡的。過濾器命令多面向“集團作戰(zhàn)”,所以有時候它們各自的用處很難單獨體現(xiàn)出來。

Most filter commands accept one or more filenames on the command line. Only

if you fail to specify a file do they read their standard input.

 大多數(shù)過濾命令都接受在命令行提供的一個或多個文件名作為輸入。只有在一個文件都未指定的時候,它們才從自己的標準輸入讀取數(shù)據(jù)。

cut: separate lines into fields

The cut command prints selected portions of its input lines. It’s most commonly used to extract delimited fields, as in the example on page 32, but it can return segments defined by column boundaries as well. The default delimiter is <Tab>, but you can change delimiters with the -d option. The -f options specifies which fields to include in the output.

For an example of the use of cut, see the section on uniq, below.

sort: sort lines

sort sorts its input lines. Simple, right? Well, maybe not—there are a few potential subtleties regarding the exact parts of each line that are sorted (the “keys”) and the collation order to be imposed. Table 2.1 shows a few of the more common options, but check the man page for others.

 cut:把行分成域

 cut命令從它的輸入行中選出若干部分,再打印出來。該命令常見的用法是提取被限定的若干域,如32頁的例子所示,但是它也能返回由列邊界所限定的若干區(qū)段。默認的限定符是<tab>,但是可以用-d選項改變這個限定符。-f選項指定輸出里包括哪些域。

 參考下面介紹unip命令工節(jié)的內(nèi)容,了解cut用法的例子,如下

 sort:將行排序

 sort命令對輸入行進行排序。簡單吧,不是嗎?或許并不簡單--到底按每行哪些部分(即“關(guān)鍵字”)進行排序,以及進行排序的順序,都可以做精細的調(diào)整。表2.1給出了一些比較常見的選項,但要查看手冊頁才能了解到其他選項。

2017.11.18

P83  P71

sort options

Opt Meaning

-b Ignore leading whitespace

-f Case insensitive sorting

-k Specify the columns that form the sort key

-n Compare fields as integer numbers

-r Reverse sort order

-t Set field separator (the default is whitespace)

-u Output unique records only

The commands below illustrate the difference between numeric and dictionary

sorting, which is the default. Both commands use the -t: and -k3,3 options to sort the /etc/group file by its third colon-separated field, the group ID. The first sorts numerically and the second alphabetically.

 $ sort -t: -k3,3 -n /etc/group1

 root:x:0:

 bin:x:1:daemon

 daemon:x:2:

 …

 $ sort -t: -k3,3 /etc/group

 root:x:0:

 bin:x:1:daemon

 users:x:100:

 …

 下面的命令展示出了數(shù)值排序和字典排序的不同之處,默認按字典排序。這兩條命令都用了-t:和-k3,3兩個選項,對/etc/group文件的內(nèi)容按照由冒號分隔的第三個域(即組ID)進行排序。第一條命令按照數(shù)值排序,而第二條命令則按照字母排序。

 $ sort -t: -k3,3 -n /etc/group1

 root:x:0:

 bin:x:1:daemon

 daemon:x:2:

 …

 $ sort -t: -k3,3 /etc/group

 root:x:0:

 bin:x:1:daemon

 users:x:100:

 …

uniq: print unique lines

uniq is similar in spirit to sort -u, but it has some useful options that sort does not emulate: -c to count the number of instances of each line, -d to show only duplicated lines, and -u to show only nonduplicated lines. The input must be presorted, usually by being run through sort.

 uniq:重復行只打印一次

 uniq命令在思想上和sort -u類似,但它有一些sor不能模擬的選項:-c累計每行出現(xiàn)的次數(shù),-d只顯示重復行,而-u只顯示不重復的行。uniq命令的輸入必須先排好序,因此通常把它放在sort命令之后運行。

For example, the command below shows that 20 users have /bin/bash as their

login shell and that 12 have /bin/false. (The latter are either pseudo-users or users whose accounts have been disabled.)

 $ cut -d: -f7 /etc/passwd | sort | uniq -c

 20 /bin/bash

 12 /bin/false

 例如,下面的命令顯示出:有20個用戶把/bin/bash作為自己的登錄shell,12個用戶把/bin/false作為登錄的shell(后者要么是偽用戶,要么就是賬號被禁用的用戶)

 $ cut -d: -f7 /etc/passwd | sort | uniq -c

 20 /bin/bash

 12 /bin/false

wc: count lines, words, and characters

Counting the number of lines, words, and characters in a file is another common operation, and the wc (word count) command is a convenient way of doing this. Run without options, it displays all three counts:

 $ wc /etc/passwd

 32 77 2003 /etc/passwd

 wc:統(tǒng)計行數(shù)、字數(shù)和字符數(shù)

 統(tǒng)計一個文件里的行數(shù)、字數(shù)和字符數(shù)是另一項常用的操作,wc(表示word count,即字數(shù)統(tǒng)計)命令是完成這項操作的一條方便途徑。如果不帶任何參數(shù)運行wc,它會顯示全部3種統(tǒng)計結(jié)果:

 $ wc /etc/passwd

 32 77 2003 /etc/passwd

In the context of scripting, it is more common to supply a -l, -w, or -c option to make wc’s output consist of a single number. This form is most commonly seen inside backquotes so that the result can be saved or acted upon.

 在腳本編程的應用場合里,常給wc命令加上-l、-w、或者-c選項,讓它只輸出一個數(shù)。在撇號里最常出現(xiàn)這種形式的命令,這樣一來,命令的執(zhí)行結(jié)果就可以被保存起來,或者根據(jù)執(zhí)行結(jié)果確定下一步的操作。

tee: copy input to two places

A command pipeline is typically linear, but it’s often helpful to tap into the data stream and send a copy to a file or to the terminal window. You can do this with the tee command, which sends its standard input both to standard out and to a file that you specify on the command line. Think of it as a tee fixture in plumbing.

The device /dev/tty is a synonym for the current terminal. For example,

 $ find / -name core | tee /dev/tty | wc -l

 tee:把輸入復制到兩個地方

 命令的管理一般都是線性的,但是從中單插入管理里的數(shù)據(jù)流,然后把一份副本發(fā)送到一個文件里,或者送到終端窗口上,也往往會很有幫助。用tee命令就能做到這一點,該命令把自己的標準輸入即發(fā)送到標識輸出,又發(fā)送到在命令行上指定的一個文件里??梢园阉氤墒撬苌系囊粋€三通。

 設(shè)備/dev/tty是當前終端的同義語。例如:

 $ find / -name core | tee /dev/tty | wc -l

prints both the pathnames of files named core and a count of the number of core files that were found.

A common idiom is to terminate a pipeline that will take a long time to run with a tee command so that output goes both to a file and to the terminal window for inspection. You can preview the initial results to make sure everything is working as you expected, then leave while the command runs, knowing that the results will be saved.

 該命令把名叫core的文件的路徑名,以及找到的core文件的數(shù)量都打印出來了。

 把tee命令作為一條執(zhí)行時間很長的命令管理的最后一條命令,這是一種常見的習慣用法,這樣一來,管理的輸出既可以送到一個文件,又可以送到終端窗口供用戶查看。用戶可以預先看到一開始的輸出結(jié)果,從而確保一切按預期執(zhí)行,然后用戶就可以在命令運行的同時不去管它,因為他們知道結(jié)果會被保存下來。

head and tail: read the beginning or end of a file

Reviewing lines from the beginning or end of a file is a common administrative operation. These commands display ten lines by default, but you can include a command-line option to specify how many lines you want to see.

For interactive use, head is more or less obsoleted by the less command, which paginates files for display. But head still finds plenty of use within scripts.

tail also has a nifty -f option that’s particularly useful for sysadmins. Instead of exiting immediately after printing the requested number of lines, tail -f waits for new lines to be added to the end of the file and prints them as they appear— great for monitoring log files. Be aware, however, that the program writing the file may be buffering its output. Even if lines are being added at regular intervals from a logical perspective, they may only become visible in chunks of 1KiB or 4KiB.2

Type <Control-C> to stop monitoring.

 head和tail:讀取文件的開頭或結(jié)尾

 管理員會經(jīng)常碰到一項操作,即查看一個文件開頭或者結(jié)尾的幾行內(nèi)容。這兩條命令默認顯示前10行內(nèi)容,但用戶可以帶一個命令行參數(shù),指定到底要看多少行內(nèi)容。

 對于交互式的應用場合,head命令已經(jīng)或多或少被less命令所取代,后者能夠給被顯示的文件標出頁數(shù),但是head命令仍然在腳本里大量使用。

 tail也有一個不錯的-f選項,對于系統(tǒng)來之不易員來說,這個選項特別有用。tail -f命令在按要求的行數(shù)打印完之后,不是立即退出,而是等著有新行被追加到文件末尾,再隨著新行的出現(xiàn)打印新行----對于監(jiān)視日志文件來說很有用。不過要注意,寫文件的那個程序可能會緩沖它的輸出。即使從邏輯上講,新行是按有規(guī)律的時間間隔追加的,但它們可能只按1kib或者4kib的塊來顯示。

 鍵入<control-C>即可停止監(jiān)視。

另外有需要云服務(wù)器可以了解下創(chuàng)新互聯(lián)scvps.cn,海內(nèi)外云服務(wù)器15元起步,三天無理由+7*72小時售后在線,公司持有idc許可證,提供“云服務(wù)器、裸金屬服務(wù)器、高防服務(wù)器、香港服務(wù)器、美國服務(wù)器、虛擬主機、免備案服務(wù)器”等云主機租用服務(wù)以及企業(yè)上云的綜合解決方案,具有“安全穩(wěn)定、簡單易用、服務(wù)可用性高、性價比高”等特點與優(yōu)勢,專為企業(yè)上云打造定制,能夠滿足用戶豐富、多元化的應用場景需求。

本文標題:UNIX/Linux系統(tǒng)管理技術(shù)手冊閱讀(十二)-創(chuàng)新互聯(lián)
本文URL:http://muchs.cn/article0/cshpoo.html

成都網(wǎng)站建設(shè)公司_創(chuàng)新互聯(lián),為您提供服務(wù)器托管、建站公司、微信小程序網(wǎng)站制作、網(wǎng)站維護、企業(yè)網(wǎng)站制作

廣告

聲明:本網(wǎng)站發(fā)布的內(nèi)容(圖片、視頻和文字)以用戶投稿、用戶轉(zhuǎn)載內(nèi)容為主,如果涉及侵權(quán)請盡快告知,我們將會在第一時間刪除。文章觀點不代表本網(wǎng)站立場,如需處理請聯(lián)系客服。電話:028-86922220;郵箱:631063699@qq.com。內(nèi)容未經(jīng)允許不得轉(zhuǎn)載,或轉(zhuǎn)載時需注明來源: 創(chuàng)新互聯(lián)

網(wǎng)站優(yōu)化排名