shell 腳本中的運算子「-gt」是什麼意思?

shell 腳本中的運算子「-gt」是什麼意思?

您好,我有這句話,我想知道這是什麼意思。

if [[ -z "$1" ]]; then   #  --> this is if the value of the parameter $1 is zero
    PASO=1
elif [[ "$1" -gt 1 ]] ; then  # but i don't know what this flags mean? .."-gt"
    LOG "[$(date +%T)] Parametros incorrectos"
    exit 255
else
    PASO=$1
fi

這是什麼-gt意思?

答案1

-gt意思是「大於」。它用於比較整數的不等式,這通常>用其他語言編寫(在某些 shell 中,使用test實用程式 or inside[ ... ]比較>兩個字串的字典順序,因此它與 具有非常不同的含義-gt)。

-gttest記錄在或的手冊中[,或記錄在您的 shell 手冊中(如果這些是內建實用程式),如

n1 -gt n2

若該整數n1在代數上大於該整數,則為 True n2;否則為假。

(以上摘自test有關該實用程式的POSIX 標準文本

Fortran 也在其.GT.數位關係運算子中使用此縮寫。

在 shell 中比較整數的其他相關運算子test[ ... ]( -ge“大於或等於”)、-lt(“小於”)、-le(“小於或等於”)、-eq(“等於”) 和-ne(“不等於”)。

有趣的是,全部其中在 Fortran 中是相同的(.GT..GE..LT..LE..EQ..NE.

答案2

$ help test
test: test [expr]
    Evaluate conditional expression.
...
      arg1 OP arg2   Arithmetic tests.  OP is one of -eq, -ne,
                     -lt, -le, -gt, or -ge.

    Arithmetic binary operators return true if ARG1 is equal, not-equal,
    less-than, less-than-or-equal, greater-than, or greater-than-or-equal
    than ARG2.

答案3

您可以從 開始help test,它將顯示該運算符支援的語法的 POSIX 子集的幫助[[

綜合文檔位於CONDITIONAL EXPRESSIONS部分man bash

具體來說:

Other operators:
  ...
  arg1 OP arg2   Arithmetic tests.  OP is one of -eq, -ne,
                 -lt, -le, -gt, or -ge.

Arithmetic binary operators return true if ARG1 is equal, not-equal,
less-than, less-than-or-equal, greater-than, or greater-than-or-equal
than ARG2.

相關內容