【问题标题】:rpm's %pretrans, %pre, etc. scriptlet section doesn't seem to support /bin/shrpm 的 %pretrans、%pre 等 scriptlet 部分似乎不支持 /bin/sh
【发布时间】:2015-10-08 01:11:50
【问题描述】:

rpm 的 %pretrans、%pre、%post 等 scriptlet 部分是否不支持所有 /bin/sh?

%pretrans
[ -n "$VERBOSE" ] && echo "pretrans called with argument \`$1'" >&2
[ -n "$VERBOSE" ] && set -x

%preun

上面安装rpm时出现如下错误

# rpm -i dist/mapr-bogus-0.0.0-1.noarch.rpm 
error: %pretrans(mapr-bogus-0.0.0-1.noarch) scriptlet failed, exit status 1

如果我在条件语句下添加“echo hello”,则 rpm 安装时不会出错。 rpm scriptlet 中是否有关于短路条件的规则?

bash-4.1# rpmbuild --version
RPM version 4.8.0
bash-4.1# lsb_release -a
LSB Version:    :base-4.0-amd64:base-4.0-noarch:core-4.0-amd64:core-4.0-noarch
Distributor ID: CentOS
Description:    CentOS release 6.1 (Final)
Release:    6.1
Codename:   Final

【问题讨论】:

    标签: rpm rpmbuild


    【解决方案1】:

    当最后一个命令失败时,RPM scriptlet 会失败(就像每个 shell 脚本一样)。当 '$VERBOSE' 为空时,'[' 返回 1:

    $ [ -n "$VERBOSE" ]
    $ echo $?
    1
    

    因为您使用 && 没有执行任何其他操作。所以最后一个命令以状态码 1 退出,因此整个小脚本以状态码 1 结束。此 RPM 评估该小脚本失败。

    正确的解决方案可以是:

    %pretrans
    [ -n "$VERBOSE" ] && echo "pretrans called with argument \`$1'" >&2
    [ -n "$VERBOSE" ] && set -x
    :
    

    其中 ':' 是 'true' 的常用别名。

    【讨论】:

      【解决方案2】:

      @msuchy 回答的补充:

      另一种以“正确”方式抑制错误的方法,如果您在“set -e”下运行,也可以使用,即反转测试,使其始终为真:

      [ -z "$VERBOSE" ] || echo "thingywhatsit"
      

      如果 $VERBOSE 为空,则第一个测试将为真,并且不运行回显。如果它是假的,它将运行回显,并且只有当回显返回错误时才会出错。这正是您想要的。

      请记住,在诸如“if”语句之类的东西中运行不会触发失败:$?为 0,除非“then”或“else”块中的某些内容产生错误。

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 2018-03-20
        • 1970-01-01
        • 2018-03-10
        • 2012-06-02
        • 2020-07-17
        • 1970-01-01
        • 1970-01-01
        • 2015-03-22
        相关资源
        最近更新 更多