2017-01-06 59 views
0

我無法獲得bash if語句只有在三個函數全部返回零時才執行。我做了一個簡單的例子,並且不能讓底部的if語句運行(代碼的else部分運行)。我是否設置了if語句錯誤?在bash中使用AND鏈接多個函數返回值

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 

回答

4

不要使用[[,它測試條件,但不運行任何東西。直接使用功能:

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

謝謝,它的工作! – isaac9A