[20210318]bash test (( )) [[ ]].txt

lfree發表於2021-03-19

[20210318]bash test (( )) [[ ]].txt

--//上午編寫bash shell指令碼時,遇到的問題,連結如下:http://blog.itpub.net/267265/viewspace-2763492/

$ cat fff.sh
#! /bin/bash
dba=4,151
kdbr_size=$(echo map dba $dba | rlbbed | grep "sb2 kdbr" | sed -e "s/^.*\[//;s/].*$//")
#echo $kdbr_size

begin=0
end=$[ kdbr_size -1 ]

while [ $begin -le $end ]
do
        kdbr_off=$(echo p dba $dba offset 0 kdbr | rlbbed | grep "sb2 kdbr\[$begin\]" | awk '{print $NF'})
        #echo $kdbr_off
#       if [ $kdbr_off -gt $kdbr_size ]
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~        
#       if (( $kdbr_off > $kdbr_size ))
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
        if [[ $kdbr_off > $kdbr_size ]]
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~            
        then
                echo -n "x /rnccntnnn dba $dba *kdbr[$begin]" | rlbbed  | grep "^col " | cut -c20- |  paste -sd'|'
        fi
        begin=$[ begin + 1 ]
done

--//我當時我知道第一種寫法是比較傳統的寫法,肯定正確。但是後面兩種我就不是很確定,當時實際上測試都透過了。
--//下午再看了文件,bash shell的文件太長,很煩。才知道上午寫的有點問題。
--//注:原始連結以修正。

$ man bash
...
((expression))
     The expression is evaluated according to the rules described below under ARITHMETIC EVALUATION.  If the value of
     the expression is non-zero, the return status is 0; otherwise the return status is 1.  This is exactly equivalent
     to let "expression".

[[ expression ]]
      Return a status of 0 or 1 depending on the evaluation of the conditional expression expression.  Expressions are
      composed of the primaries described below under CONDITIONAL EXPRESSIONS.  Word splitting and pathname expansion
      are not  performed  on the words between the [[ and ]]; tilde expansion, parameter and variable expansion,
      arithmetic expansion, command substitution, process substitution, and quote removal are performed.  Conditional
      operators such as -f must be unquoted to be recognized as primaries.

--//真心講沒怎麼看懂。

      When the == and != operators are used, the string to the right of the operator is considered a pattern and matched
      according to the rules described below under Pattern Matching.  If the shell option nocasematch  is  enabled,  the
      match  is performed without regard to the case of alphabetic characters.  The return value is 0 if the string
      matches (==) or does not match (!=) the pattern, and 1 otherwise.  Any part of the pattern may be quoted to force
      it to be matched as a string.

      An additional binary operator, =~, is available, with the same precedence as == and !=.  When it is used, the
      string to the right of the operator is considered an  extended  regular  expression  and  matched  accordingly
      (as  in regex(3)).   The  return  value  is  0 if the string matches the pattern, and 1 otherwise.  If the regular
      expression is syntactically incorrect, the conditional expression's return value is 2.  If the shell option
      nocasematch is enabled, the match is performed without regard to the case of alphabetic characters.  Substrings
      matched by parenthesized subexpressions within the regular expression are saved in the array variable
      BASH_REMATCH.  The element  of BASH_REMATCH with index 0 is the portion of the string matching the entire regular
      expression.  The element of BASH_REMATCH with index n is the portion of the string matching the nth parenthesized
      subexpression.

      Expressions may be combined using the following operators, listed in decreasing order of precedence:

      ( expression )
      Returns the value of expression.  This may be used to override the normal precedence of operators.
      ! expression
      True if expression is false.
      expression1 && expression2
      True if both expression1 and expression2 are true.
      expression1 || expression2
      True if either expression1 or expression2 is true.

      The && and || operators do not evaluate expression2 if the value of expression1 is sufficient to determine the
      return value of the entire conditional expression.

--//也就是使用[[ ]] 是有一些問題的。透過例子說明:

 $ cat aa.sh
# /bin/bash
# test (( )) [[ ]]
echo "[[]]"
if [[ 7788 > 28 ]]
then
        echo 7788
fi

if [[ 7788 > 88 ]]
then
        echo 7788xxxx
fi

echo "(())"
if (( 7788 > 28 ))
then
        echo 7788
fi

if (( 7788 > 88 ))
then
        echo 7788yyyy
fi

$ . ./aa.sh
[[]]
7788
(())
7788
7788yyyy

--//實際上[[ ]]適用於字串比較,而(( )) 使用於數字比較。
--//我當時的指令碼能透過測試,實際上kdbr_size=14,無論作為字串還是數字都是正確的。
--//正確的應該使用(( )).
--//給我的感覺就是原來test的語法有點怪異,因為數字比較使用 -eq -gt -ge -le -lt -ne之類的。才有了這兩種語法。

--//
[[ TEXPR ]]寫 bash 的人覺得 [ TEXPR ] 是個命令太憋屈了,所以做了個專門的語法出來。這玩意的好處是不用引號,有個可以像
case 一樣匹配萬用字元的 ==,可以用 && 和 ||,還可以搞正規表示式。[ TEXPR ] 能用的東西這玩意都能用。寫 bash 不想動太多腦子
的話認準這個。你去看 help '[[' 就是。

--//

到此為止,單中括號已經完全說清楚了,如果不相容 posix shell的話,可以用 bash獨有的 [[ expression ]] 進行字串判斷,((
express )) 進行整數判斷。更多 bash 技巧見:

$ help [[
[[ ... ]]: [[ expression ]]
    Returns a status of 0 or 1 depending on the evaluation of the conditional
    expression EXPRESSION.  Expressions are composed of the same primaries used
    by the `test' builtin, and may be combined using the following operators

        ( EXPRESSION )  Returns the value of EXPRESSION
        ! EXPRESSION    True if EXPRESSION is false; else false
        EXPR1 && EXPR2  True if both EXPR1 and EXPR2 are true; else false
        EXPR1 || EXPR2  True if either EXPR1 or EXPR2 is true; else false

    When the `==' and `!=' operators are used, the string to the right of the
    operator is used as a pattern and pattern matching is performed.  The
    && and || operators do not evaluate EXPR2 if EXPR1 is sufficient to
    determine the expression's value.

--//好亂,好煩!!

來自 “ ITPUB部落格 ” ,連結:http://blog.itpub.net/267265/viewspace-2763809/,如需轉載,請註明出處,否則將追究法律責任。

相關文章