网站建设资讯

NEWS

网站建设资讯

UNIX/Linux系统管理技术手册阅读(十二)

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 , 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页的例子所示,但是它也能返回由列边界所限定的若干区段。默认的限定符是,但是可以用-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 to stop monitoring.

  head和tail:读取文件的开头或结尾

  管理员会经常碰到一项操作,即查看一个文件开头或者结尾的几行内容。这两条命令默认显示前10行内容,但用户可以带一个命令行参数,指定到底要看多少行内容。

  对于交互式的应用场合,head命令已经或多或少被less命令所取代,后者能够给被显示的文件标出页数,但是head命令仍然在脚本里大量使用。

  tail也有一个不错的-f选项,对于系统来之不易员来说,这个选项特别有用。tail -f命令在按要求的行数打印完之后,不是立即退出,而是等着有新行被追加到文件末尾,再随着新行的出现打印新行----对于监视日志文件来说很有用。不过要注意,写文件的那个程序可能会缓冲它的输出。即使从逻辑上讲,新行是按有规律的时间间隔追加的,但它们可能只按1kib或者4kib的块来显示。

  键入即可停止监视。


本文标题:UNIX/Linux系统管理技术手册阅读(十二)
转载源于:http://njwzjz.com/article/ghdpjh.html