UNIX/Linux 系統(tǒng)管理技術(shù)手冊(cè)閱讀(十二)

2016.11.17?
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?變量和引用?
?變量名在賦值的時(shí)候沒有標(biāo)永,但在訪問它們的值的時(shí)候要在變量名之前加一個(gè)$符。例如:?
?$ etcdir='/etc'?
?$ echo $etcdir?
?/etc?
?不要在等號(hào)兩邊留空白,否則shll會(huì)把變量名誤以為是命令名。?
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.?
?引用一個(gè)變量,可用花括號(hào)把這個(gè)變量的名字括起來,讓分析程序和閱讀代碼的人能清楚地知道變量名的起止位置;例如,用${etcdir}代替$etcdir。正常情況下不要求有花括號(hào),但是如果想要在雙引號(hào)起來的字符串里擴(kuò)展變量,它們就會(huì)派上用場(chǎng)了。因?yàn)槿藗兘?jīng)常想要在一個(gè)變量的內(nèi)容之后跟著字母或者標(biāo)點(diǎn)符號(hào)。例如:?
?$ 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變量起名字沒有標(biāo)準(zhǔn)的命令規(guī)范,但如果變量名的所有字母都大寫,一般表明該變量是環(huán)境變量,或者是從全局配置文件里讀取的變量。本地變量則多半是所有字母都小寫,而且在變量名的各個(gè)部分之間用下劃線隔開。變量名區(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)境變量會(huì)被自動(dòng)導(dǎo)入bash的變量名空間,所以它們可以用標(biāo)識(shí)的語法來設(shè)置和讀取。命令exportvarname將一個(gè)shell變量提升為一個(gè)環(huán)境變量。用來在用戶登錄時(shí)設(shè)置環(huán)境變量的那些命令,都應(yīng)該放在該用戶的~/.profile?或?~/.bash_profile這兩個(gè)文件里。而其他像PWD(代表當(dāng)前工作目錄)這樣的環(huán)境變量都由shell自動(dòng)維護(hù)。?
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}.?
?對(duì)于單引號(hào)和雙引號(hào)括起來的字符串而言,shell以相似的方式處理它們,例外之處在于雙引號(hào)括起來的變量可以進(jìn)行替換(*和?這樣的文件名匹配元字符做擴(kuò)展)和變量擴(kuò)展。例如:?
?$ 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.?
?左引號(hào)也叫做撇號(hào),對(duì)它的處理和引號(hào)類似,但是它們還有其他作用,即能夠把串的內(nèi)容按一條shell命令來執(zhí)行,并且用該命令的輸出來替換這個(gè)字符串。例如:?
?$ 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ī)蹈距的命令,都可以當(dāng)作一個(gè)過濾器(也就是說,管理的一個(gè)環(huán)節(jié))來處理數(shù)據(jù),在這一小節(jié),我們簡(jiǎn)要回顧一些使用較為廣泛的過濾器命令(包括上面已經(jīng)用到過的一些命令)但是這樣的過濾器命令實(shí)際上是無窮無盡的。過濾器命令多面向“集團(tuán)作戰(zhàn)”,所以有時(shí)候它們各自的用處很難單獨(dú)體現(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ù)過濾命令都接受在命令行提供的一個(gè)或多個(gè)文件名作為輸入。只有在一個(gè)文件都未指定的時(shí)候,它們才從自己的標(biāo)準(zhǔn)輸入讀取數(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ū)段。默認(rèn)的限定符是<tab>,但是可以用-d選項(xiàng)改變這個(gè)限定符。-f選項(xiàng)指定輸出里包括哪些域。?
?參考下面介紹unip命令工節(jié)的內(nèi)容,了解cut用法的例子,如下?
?sort:將行排序?
?sort命令對(duì)輸入行進(jìn)行排序。簡(jiǎn)單吧,不是嗎?或許并不簡(jiǎn)單--到底按每行哪些部分(即“關(guān)鍵字”)進(jìn)行排序,以及進(jìn)行排序的順序,都可以做精細(xì)的調(diào)整。表2.1給出了一些比較常見的選項(xiàng),但要查看手冊(cè)頁才能了解到其他選項(xiàng)。