【问题标题】:Understanding a bashism in a configure.ac file了解 configure.ac 文件中的 bashism
【发布时间】:2015-04-13 05:08:48
【问题描述】:

我正在尝试了解项目的 configure.ac 文件中的常见模式。我相当确定这是一个我很难解析的 bashism。这是一个例子:

LDFLAGS="${LDFLAGS:+$LDFLAGS }$OTHERFLAG"

其意图似乎是通过 OTHERFLAG 中的内容来扩展 LDFLAGS 的当前值。看起来写起来会更简单:

LDFLAGS="$LDFLAGS $OTHERFLAG"

相关section in the bash manual说:

${parameter:+word}
    If parameter is null or unset, nothing is substituted, otherwise the expansion of word is substituted.

我认为这里有一些防御性的东西,但我不确定到底是什么。

【问题讨论】:

标签: bash autoconf m4


【解决方案1】:

如果你写:

LDFLAGS="$LDFLAGS $OTHERFLAG"

$LDFLAGS 未设置,它的字符串将以空格开头,这可能是不可取的。

这就是为什么:

LDFLAGS="${LDFLAGS:+$LDFLAGS }$OTHERFLAG"

例子:

tiago@dell:~$ unset a ; b="test"; a="${a:+$a }$b"; echo "$a"
test
tiago@dell:~$ unset a ; b="test"; a="$a $b"; echo "$a"
 test

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2012-01-23
    • 1970-01-01
    • 2011-04-16
    • 1970-01-01
    • 1970-01-01
    • 2019-09-18
    • 2019-10-07
    • 2019-10-06
    相关资源
    最近更新 更多