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

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形式,做做实验吧!

时间: 2024-10-12 08:11:06

UNIX/Linux 系统管理技术手册阅读(十一)的相关文章

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

从现在开始,给自己定了一个任务,每天看一段<UNIX/Linux 系统管理技术手册 第4版 >,中英对照着看,以此加强自己的英文阅读能力! 2016.8.8 16:30-17:30 SECTION ONE BASIC ADMINISTRATION 1 Where to Start 第一部分 基本管理技术 第1章 从何处入手 An awful lot of UNIX and Linux information is available these days,so we've designed t

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

2016.8.12 23:20-23:50 1.2 SUGGESTED BACKGROUND 1.2 读者的知识背景 We assume in this book that you have a certain amount of Linux or UNIX experience. In particular, you should have a general concept of how the system looks and feels from the user's perspecti

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

2016.8.17 16:50-17:30 Some important questions to ask are Is this distribution going to be around in five years? Is this distribution going to stay on top of the latest security patches? Is this distribution going to release updated software promptly

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

2016.8.14 21:40-22:101.3 FRICTION BETWEEN UNIX AND LINUX1.3 UNIX他Linux之间的摩擦  Because they are similar, this book covers both UNIX and Linux systems. Unfortunately, mentioning UNIX and Linux together in the same sentence can sometimes be like stepping

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

2016.11.4 9:30-10:15 Adding new software If you do need to install additional software, you first need to determine the canonical name of the relevant software package. For example, you'd need to translate "I want to install locate" to "I n

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

2016.8.31 10:00-10:30 1.12 WAYS TO FIND AND INSTALL SOFTWARE Chapter 12, Software Installation and Management, addresses software provisioning in detail. But for the impatient, here's a quick primer on how to find out what's installed on your system

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 mis

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

2016.8.22 9:00-9:40 The keywords database can become out of date. If you add additional man pages to your system, you may need to rebuild this file with mandb (Ubuntu, SUSE), makewhatis (Red Hat), or catman -w (Solaris, HP-UX, AIX). keyword指代的库可能已经过期

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

2016.8.10 20:00-21:20 1.1.3 Performing backups 1.1.3 执行备份 Performing backups is perhaps the most important job of the system administrator, and it is also the job that is most often ignored or sloppily done. Backups are time consuming and boring, but