您好,登錄后才能下訂單哦!
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 變量和引用
變量名在賦值的時候沒有標永,但在訪問它們的值的時候要在變量名之前加一個$符。例如:
$ 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。正常情況下不要求有花括號,但是如果想要在雙引號起來的字符串里擴展變量,它們就會派上用場了。因為人們經常想要在一個變量的內容之后跟著字母或者標點符號。例如:
$ 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變量起名字沒有標準的命令規范,但如果變量名的所有字母都大寫,一般表明該變量是環境變量,或者是從全局配置文件里讀取的變量。本地變量則多半是所有字母都小寫,而且在變量名的各個部分之間用下劃線隔開。變量名區分大小寫。
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.
環境變量會被自動導入bash的變量名空間,所以它們可以用標識的語法來設置和讀取。命令exportvarname將一個shell變量提升為一個環境變量。用來在用戶登錄時設置環境變量的那些命令,都應該放在該用戶的~/.profile 或 ~/.bash_profile這兩個文件里。而其他像PWD(代表當前工作目錄)這樣的環境變量都由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.
左引號也叫做撇號,對它的處理和引號類似,但是它們還有其他作用,即能夠把串的內容按一條shell命令來執行,并且用該命令的輸出來替換這個字符串。例如:
$ 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讀入數據,向STDOUT輸出結果”這樣循規蹈距的命令,都可以當作一個過濾器(也就是說,管理的一個環節)來處理數據,在這一小節,我們簡要回顧一些使用較為廣泛的過濾器命令(包括上面已經用到過的一些命令)但是這樣的過濾器命令實際上是無窮無盡的。過濾器命令多面向“集團作戰”,所以有時候它們各自的用處很難單獨體現出來。
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.
大多數過濾命令都接受在命令行提供的一個或多個文件名作為輸入。只有在一個文件都未指定的時候,它們才從自己的標準輸入讀取數據。
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頁的例子所示,但是它也能返回由列邊界所限定的若干區段。默認的限定符是<tab>,但是可以用-d選項改變這個限定符。-f選項指定輸出里包括哪些域。
參考下面介紹unip命令工節的內容,了解cut用法的例子,如下
sort:將行排序
sort命令對輸入行進行排序。簡單吧,不是嗎?或許并不簡單--到底按每行哪些部分(即“關鍵字”)進行排序,以及進行排序的順序,都可以做精細的調整。表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:
…
下面的命令展示出了數值排序和字典排序的不同之處,默認按字典排序。這兩條命令都用了-t:和-k3,3兩個選項,對/etc/group文件的內容按照由冒號分隔的第三個域(即組ID)進行排序。第一條命令按照數值排序,而第二條命令則按照字母排序。
$ 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累計每行出現的次數,-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:統計行數、字數和字符數
統計一個文件里的行數、字數和字符數是另一項常用的操作,wc(表示word count,即字數統計)命令是完成這項操作的一條方便途徑。如果不帶任何參數運行wc,它會顯示全部3種統計結果:
$ 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選項,讓它只輸出一個數。在撇號里最常出現這種形式的命令,這樣一來,命令的執行結果就可以被保存起來,或者根據執行結果確定下一步的操作。
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:把輸入復制到兩個地方
命令的管理一般都是線性的,但是從中單插入管理里的數據流,然后把一份副本發送到一個文件里,或者送到終端窗口上,也往往會很有幫助。用tee命令就能做到這一點,該命令把自己的標準輸入即發送到標識輸出,又發送到在命令行上指定的一個文件里。可以把它想成是水管上的一個三通。
設備/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文件的數量都打印出來了。
把tee命令作為一條執行時間很長的命令管理的最后一條命令,這是一種常見的習慣用法,這樣一來,管理的輸出既可以送到一個文件,又可以送到終端窗口供用戶查看。用戶可以預先看到一開始的輸出結果,從而確保一切按預期執行,然后用戶就可以在命令運行的同時不去管它,因為他們知道結果會被保存下來。
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:讀取文件的開頭或結尾
管理員會經常碰到一項操作,即查看一個文件開頭或者結尾的幾行內容。這兩條命令默認顯示前10行內容,但用戶可以帶一個命令行參數,指定到底要看多少行內容。
對于交互式的應用場合,head命令已經或多或少被less命令所取代,后者能夠給被顯示的文件標出頁數,但是head命令仍然在腳本里大量使用。
tail也有一個不錯的-f選項,對于系統來之不易員來說,這個選項特別有用。tail -f命令在按要求的行數打印完之后,不是立即退出,而是等著有新行被追加到文件末尾,再隨著新行的出現打印新行----對于監視日志文件來說很有用。不過要注意,寫文件的那個程序可能會緩沖它的輸出。即使從邏輯上講,新行是按有規律的時間間隔追加的,但它們可能只按1kib或者4kib的塊來顯示。
鍵入<control-C>即可停止監視。
免責聲明:本站發布的內容(圖片、視頻和文字)以原創、轉載和分享為主,文章觀點不代表本網站立場,如果涉及侵權請聯系站長郵箱:is@yisu.com進行舉報,并提供相關證據,一經查實,將立刻刪除涉嫌侵權內容。