[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之类的。才有了这两种语法。
--//https://www.zhihu.com/question/266787434
[[ TEXPR ]]写 bash 的人觉得 [ TEXPR ] 是个命令太憋屈了,所以做了个专门的语法出来。这玩意的好处是不用引号,有个可以像
case 一样匹配通配符的 ==,可以用 && 和 ||,还可以搞正则表达式。[ TEXPR ] 能用的东西这玩意都能用。写 bash 不想动太多脑子
的话认准这个。你去看 help '[[' 就是。
--//https://www.zhihu.com/question/266787434
到此为止,单中括号已经完全说清楚了,如果不兼容 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博客 ” ,链接:https://blog.itpub.net/267265/viewspace-2763809/ ,如需转载,请注明出处,否则将追究法律责任。