首页 > 代码库 > UNIX/Linux 系统管理技术手册阅读(十二)

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 <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给出了一些比较常见的选项,但要查看手册页才能了解到其他选项。


本文出自 “赵东伟的博客” 博客,请务必保留此出处http://zhaodongwei.blog.51cto.com/4233742/1873907

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