在bash中使用AND链接多个函数返回值

时间:2017-01-06 18:50:08

标签: bash if-statement

我无法获取bash if语句只在​​三个函数都返回零时执行。我做了一个简单的例子,无法在底部运行if语句(代码的else部分运行)。我设置if语句错了吗?

test() {
  NUM=3
  BUM=4
  if [ $(NUM) -lt $(BUM) ];
  then 
    return 0;
  else
    return 1;
  fi;
}

test2() {
  NUM=4
  BUM=5
  if [ $(NUM) -lt $(BUM) ];
  then 
    return 0;
  else
    return 1;
  fi;
}

test3() {
  NUM=13
  BUM=133
  if [ $(NUM) -lt $(BUM) ];
  then 
    return 0;
  else
    return 1;
  fi;
}

if [[ ${test} && ${test2} && ${test3} ]]; then 
  echo "successfully chained function values"
else
  echo "ands did not chain successfully"
fi

1 个答案:

答案 0 :(得分:4)

不要使用[[,它会测试条件,但不会运行任何操作。直接使用这些功能:

if test && test2 && test3 ; then
    echo Success
fi