如果bash中有多个组的声明?

时间:2016-01-15 19:35:15

标签: bash travis-ci

我想要的是

if((TRAVIS_BRANCH != "master") || (TRAVIS_BRANCH == "master" && TRAVIS_PULL_REQUEST == true)

我如何在bash中执行此操作?我认为这是最接近的,但它仍然不起作用

if [ "$TRAVIS_BRANCH" != "master" ] || ["$TRAVIS_BRANCH" == "master" &&  "$TRAVIS_PULL_REQUEST" == "true"]

3 个答案:

答案 0 :(得分:1)

在命令名称周围添加空格([是命令名称),不要在单个&&命令中尝试使用||[

if [ "$TRAVIS_BRANCH" != "master" ] || { [ "$TRAVIS_BRANCH" = "master" ] && [ "$TRAVIS_PULL_REQUEST" = "true"]; }

但是,您可以&&内使用||[[ ]],并且也不需要在那里引用:

if [[ $TRAVIS_BRANCH != master ]] || [[ $TRAVIS_BRANCH = master && $TRAVIS_PULL_REQUEST = true ]]

答案 1 :(得分:1)

  • 确保[]字符被空格包围
  • Double equals在Bash中有效,但与POSIX不兼容
  • 您必须在-a
  • 内使用&&,而不是[
  • 由于shell代码中的difficulty in working out the precedence rules,建议每行代码不要使用多个布尔运算符
  • 建议在Bash中使用[[而不是[

结果:

is_master_pull_request() {
    [[ "$TRAVIS_BRANCH" = "master" ]] && [[ "$TRAVIS_PULL_REQUEST" = "true" ]]
}
if [[ "$TRAVIS_BRANCH" != "master" ]] || is_master_pull_request

答案 2 :(得分:0)

尝试:

if [ "$TRAVIS_BRANCH" != "master" ] || [[ "$TRAVIS_BRANCH" == "master" && "$TRAVIS_PULL_REQUEST" == "true" ]]; then