[20210318]bash test (( )) [[ ]].txt
[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/,如需轉載,請註明出處,否則將追究法律責任。
相關文章
- [20210318]awk分割符FS.txt
- [20210318]bbed讀取資料塊.txt
- [20210318]bbed讀取資料塊2.txt
- [20181203]bash here $.txt
- [20180930]bash shell &.txt
- [20190312]bash IFS例子.txt
- 執行shell指令碼報錯:-bash: ./test1.sh: /bin/bash^M: ...指令碼
- [20221104]bash exec使用技巧.txt
- [20210908]Reverse Shell with Bash.txt
- [20210207]bash history小技巧.txt
- [20180413]bash 位置引數.txt
- [20180926]bash與分號.txt
- [20210913]bash shell $* and $@ 的區別.txt
- [20191010]bash行計算器.txt
- [20201116]bash shell IO重定向.txt
- [20181212]bash shell 字串 補零.txt字串
- [20231123]函式與bash shell呼叫.txt函式
- [20230428]bash實現xor計算.txt
- [20230314]nc reverse bash shell alias.txt
- [20230310]nc reverse bash shell問題.txt
- [20201109]here-doc(EOF) in bash.txt
- [20201109]bash shell特殊算術方式.txt
- [20210218]bash echo 建立順序號.txt
- [20190412]bash顯示日期相減.txt
- [20181230]Git Bash啟動緩慢.txtGit
- [20190419]bash單雙引號問題.txt
- [20210324]bash shell value too great for base.txt
- [20181229]bash shell的算術運算 .txt
- [20230309]nc reverse bash shell or cmd.exe(windows).txtWindows
- [20221111]bash eval設定變數問題.txt變數
- [20200217]bash顯示path環境變數.txt變數
- [20191011]bash任意進位制編碼表.txt
- [20191230]rhel 7.7 bash_completion如何啟動.txt
- [20210618]記錄bash shell執行的命令.txt
- [20210330]bash使用source or ..呼叫shell指令碼注意txt指令碼
- [20210126]bash ln建立軟連結問題.txt
- Hyperledger Fabric的test-network啟動過程Bash原始碼詳解原始碼
- [20231023]生成bbed的執行指令碼(bash shell).txt指令碼