11

根据Debian Policy Manual,我的 postinst 脚本在升级和配置时被调用,作为“postinst configure old-version ”,其中old-version是以前安装的版本(可能为 null)。我想确定new-version,即当前正在配置(升级到)的版本。

环境变量$DPKG_MAINTSCRIPT_PACKAGE包含包名;似乎没有等效的_VERSION字段。 /var/lib/dpkg/status在 postinst 运行后更新,所以我似乎也无法从那里解析它。

有任何想法吗?

4

7 回答 7

6

.postinst这是我发现解决此问题的最佳方法是在您的(或其他控制文件)中使用占位符变量:

case "$1" in
    configure)
        new_version="__NEW_VERSION__"
        # Do something interesting interesting with $new_version...
        ;;
    abort-upgrade|abort-remove|abort-deconfigure)
        # Do nothing
        ;;
    *)
        echo "Unrecognized postinst argument '$1'"
        ;;
esac

然后在 中debian/rules,在构建时将占位符变量替换为正确的版本号:

# Must not depend on anything. This is to be called by
# binary-arch/binary-indep in another 'make' thread.
binary-common:
    dh_testdir
    dh_testroot
    dh_lintian
    < ... snip ... >

    # Replace __NEW_VERSION__ with the actual new version in any control files
    for pkg in $$(dh_listpackages -i); do \
        sed -i -e 's/__NEW_VERSION__/$(shell $(SHELL) debian/gen_deb_version)/' debian/$$pkg/DEBIAN/*; \
    done

    # Note dh_builddeb *must* come after the above code
    dh_builddeb

在 中找到的结果.postinst片段debian/<package-name>/DEBIAN/postinst将如下所示:

case "$1" in
    configure)
        new_version="1.2.3"
        # Do something interesting interesting with $new_version...
        ;;
    abort-upgrade|abort-remove|abort-deconfigure)
        # Do nothing
        ;;
    *)
        echo "Unrecognized postinst argument '$1'"
        ;;
esac
于 2012-06-25T23:08:02.243 回答
4
VERSION=$(zless /usr/share/doc/$DPKG_MAINTSCRIPT_PACKAGE/changelog* \
     | dpkg-parsechangelog -l- -SVersion')

与其他解决方案相比的优势:

  • 无论更改日志是否压缩都有效
  • 使用 dpkg 的 changelog 解析器代替正则表达式、awk 等。
于 2014-02-22T21:34:26.973 回答
4

将以下内容添加到debian/rules

override_dh_installdeb:
    dh_installdeb
    for pkg in $$(dh_listpackages -i); do \
        sed -i -e 's/__DEB_VERSION__/$(DEB_VERSION)/' debian/$$pkg/DEBIAN/*; \
    done

它将__DEB_VERSION__用版本号替换您的 debian 脚本中出现的任何 。

于 2015-09-15T09:29:42.530 回答
3

我在 postinst 脚本中使用了以下有点脏的命令:

NewVersion=$(zcat /usr/share/doc/$DPKG_MAINTSCRIPT_PACKAGE/changelog.gz | \
  head -1 | perl -ne '$_=~ /.*\((.*)\).*/; print $1;')
于 2011-06-13T19:00:14.170 回答
3

当 postinst 运行时,所有的包文件都已经安装并且 dpkg 的数据库已经更新,所以你可以得到刚刚安装的版本:

dpkg-query --show --showformat='${Version}' packagename
于 2018-04-16T08:12:59.837 回答
0

为什么不能在打包时将版本硬编码到 postinst 脚本中?

于 2009-04-03T20:45:16.480 回答
0

尝试这个:

VERSION=`dpkg -s $DPKG_MAINTSCRIPT_PACKAGE | sed -n 's/^Version: //p'`
于 2018-02-14T12:10:22.137 回答