Ksh

Unix - ksh 測試多個變數是否為 0

  • January 27, 2019

所以基本上我想測試 3 個變數是否為 0。如果其中一個不是,它應該報告它。這就是我得到的:

       if [[ $result -ne 0 && $resultmax -ne 0 && $resultmin -ne 0 ]]
       then
           echo "There is something terribly wrong."
       fi

這沒用。知道我在哪裡搞砸了嗎?

如果要測試這些變數之一不為 0,則需要||operator。不是&&

$ if [[ 1 -ne 0 && 0 -ne 0 && 0 -ne 0 ]] ; then echo "There is something terribly wrong.";  fi

$ if [[ 1 -ne 0 || 0 -ne 0 || 0 -ne 0 ]] ; then echo "There is something terribly wrong.";  fi
There is something terribly wrong.

現在您正在測試是否所有變數都不為 0 以報告錯誤。嘗試:

if [[ $result -ne 0 || $resultmax -ne 0 || $resultmin -ne 0 ]]
then
   echo "There is something terribly wrong."
fi

引用自:https://unix.stackexchange.com/questions/196335