2016.11.7
2 Scripting and the Shell
Good system administrators write scripts. Scripts standardize and automate the performance of administrative chores and free up admins’ time for more important and more interesting tasks. In a sense, scripts are also a kind of low-rent documentation in that they act as an authoritative outline of the steps needed to complete a particular task.
好的系统管理员都要写脚本。脚本以标准和自动的方式履行系统管理员的繁杂事务,藉此把管理员的时间节省出来,以花在更重要和更有意思的任务上。从某种意思上讲,脚本也是一种低质量的文档,因为它们充当了一种权威提纲,提纲里列出完成特殊任务所需的步骤。
In terms of complexity, administrative scripts run the gamut from simple ones that encapsulate a few static commands to major software projects that manage host configurations and administrative data for an entire site. In this book we’re primarily interested in the smaller, day-to-day scripting projects that sysadmins normally encounter, so we don’t talk much about the support functions (e.g., bug tracking and design review) that are needed for larger projects.
从复杂性来看,系统管理脚本的范围很广,简单得只封装几条静态命令,大到一处重要的软件项目,为整个站点管理主机配置和管理性数据。在本书里,我们所感兴趣的主要是系统管理员通常会碰到的较小的日常脚本项目。因此,对于大的项目才需要的支持功能(例如,bug追踪和设计评审),我们不会讲得太多。
Administrative scripts should emphasize programmer efficiency and code clarity rather than computational efficiency. This is not an excuse to be sloppy, but simply a recognition that it rarely matters whether a script runs in half a second or two seconds. Optimization can have an amazingly low return on investment, even for scripts that run regularly out of cron.
系统管理脚本应该注重两点,即编程人员的开发效率和代码的清晰可读性。计算效率不应该成为关注重点,但这不应该成为草率行事的借口,而是要认识到,很少需要在意一个脚本是在半秒还是两秒内运行完。优化脚本获得的回报都非常低,甚至对通过cron定期运行的脚本来说也不例外。
For a long time, the standard language for administrative scripts was the one defined by the shell. Most systems’ default shell is bash (the “Bourne-again” shell),but sh (the original Bourne shell) and ksh (the Korn shell) are used on a few UNIX systems. Shell scripts are typically used for light tasks such as automating a sequence of commands or assembling several filters to process data.
长期以来,编写系统管理脚本的标准语言是shell所定义的语言。在大多数系统上,默认的shell都是由bash(即“Bourne agin”shell),但是在几种不多的UNIX系统上,也用sh(最初的Bourne shell)和ksh(Korn shell)。shell脚本一般用于轻量级的任务,如自动执行一系列命令,或者把几个过滤器组合起来处理数据。
The shell is always available, so shell scripts are relatively portable and have few dependencies other than the commands they invoke. Whether or not you choose the shell, the shell may choose you: most environments include a hefty complement of existing sh scripts, and those scripts frequently need to be read, understood, and tweaked by administrators.
各种操作系统上都有shell,所以shell脚本可移值性相当好,除了它们调用的命令之外,要依赖的东西也不多。无论是否选择shell来编写脚本,都会碰到shell;大多数环境都包括已有sh脚本的强大补充,系统管理员会频频阅读、理解和调整这些脚本。
For more sophisticated scripts, it’s advisable to jump to a real programming language such as Perl or Python, both of which are well suited for administrative work. These languages incorporate a couple of decades’ worth of language design advancements relative to the shell, and their text processing facilities (invaluable to administrators) are so powerful that sh can only weep and cower in shame.
对于更为复杂高端的脚本来说,建议转而采用一种真正的编程语言来写,像perl或者python这样的语言,它们两者都很适合于系统管理工作。这两种语言融入的设计理念比shell领先20年,它们的字处理功能(对于系统管理员来说,价值难以估量)如此强大,sh在它们面前黯然失色。
The main drawback to Perl and Python is that their environments can be a bit
fussy to set up, especially when you start to use third-party libraries that have compiled components. The shell skirts this particular issue by having no module structure and no third-party libraries.
Perl和Python的主要缺点在于,建立它们的环境要麻烦一点,尤其是要用到的第三方库,而库里又包含已经编译好的部件的时候。shell没有模块结构,也没有第三方的库,因此避开了这个特殊的问题。
This chapter takes a quick look at bash, Perl, and Python as languages for scripting, along with regular expressions as a general technology.
本章简要介绍了bash、Perl和Python作为脚本编程语言的用法,以及正则表达式这种通用的技术。
2.1 SHELL BASICS
Before we take up shell scripting, let’s review some of the basic features and syntax of the shell. The material in this section applies to all major shells in the sh lineage (including bash and ksh, but not csh or tcsh), regardless of the exact platform you are using. Try out the forms you’re not familiar with, and experiment!
2.1 shell的基础知识
在我们开始介绍Shell的脚本编辑之前,让我们先看看shell的一此基本特性和语法。不管读者正在使用的是何种平台,本节都适用于sh大家庭里的所有主流shell(包括bash和ksh,但不包括csh或者tesh)。尝试一下自己不熟悉的sh形式,做做实验吧!