应用程序包开发者指南

使用 sed 类和 postinstall 脚本修改文件

此案例研究在软件包安装期间修改安装计算机上存在的一个文件。它使用三种修改方法之一。另外两种方法将在使用标准类和类操作脚本修改文件使用 build 类修改文件中进行介绍。修改的文件是 /etc/inittab

技术

此案例研究展示以下技术:

方法

要使用 sed 类在安装时修改 /etc/inittab,您必须完成以下任务:

这种在安装期间编辑 /etc/inittab 的方法有一个缺点:虽然只是为了执行 init q 命令也必须提供一个完整的脚本(postinstall 脚本)。

案例研究文件

pkginfo 文件

PKG=case4
NAME=Case Study #4
CATEGORY=applications
BASEDIR=/opt
ARCH=SPARC
VERSION=Version 1d05
CLASSES=sed

prototype 文件

i pkginfo
i postinstall
e sed /etc/inittab ? ? ?

sed 类操作脚本 (/etc/inittab)

!remove
# remove all entries from the table that are associated
# with this package, though not necessarily just
# with this package instance
/^[^:]*:[^:]*:[^:]*:[^#]*#ROBOT$/d
!install
# remove any previous entry added to the table
# for this particular change
/^[^:]*:[^:]*:[^:]*:[^#]*#ROBOT$/d
# add the needed entry at the end of the table;
# sed(1) does not properly interpret the '$a'
# construct if you previously deleted the last
# line, so the command
# $a\
# rb:023456:wait:/usr/robot/bin/setup #ROBOT
# will not work here if the file already contained
# the modification. Instead, you will settle for
# inserting the entry before the last line!
$i\
rb:023456:wait:/usr/robot/bin/setup #ROBOT

postinstall 脚本

# make init re-read inittab
/sbin/init q ||
exit 2
exit 0