&2`" ] #true。1>&2表示将标准输出输出到文件描述符2(标准错误输出:屏幕)3 [ 2 -eq 2 ] &"/> &2`" ] #true。1>&2表示将标准输出输出到文件描述符2(标准错误输出:屏幕)3 [ 2 -eq 2 ] &">

《Advanced Bash-scripting Guide》学习(十八):[[ ]]与[ ]的一些特殊情况

PHP中文网
Lepaskan: 2017-07-11 18:16:08
asal
1476 orang telah melayarinya

本文所选的例子来自于《Advanced Bash-scripting Gudie》一书,译者 杨春敏 黄毅

 

<span style="color: #008080">1</span> <span style="color: #000000">例1:
</span><span style="color: #008080">2</span> [ <span style="color: #800080">1</span> -eq <span style="color: #800080">1</span> ] && [ -n <span style="color: #800000">"</span><span style="color: #800000">`echo true 1>&2`</span><span style="color: #800000">"</span> ]            #<span style="color: #0000ff">true。1>&2表示将标准输出输出到文件描述符2(标准错误输出:屏幕)</span>
<span style="color: #008080">3</span> [ <span style="color: #800080">2</span> -eq <span style="color: #800080">2</span> ] && [ -n <span style="color: #800000">"</span><span style="color: #800000">`echo true 1>&2`</span><span style="color: #800000">"</span><span style="color: #000000"> ]            #(not output)
</span><span style="color: #008080">4</span> 
<span style="color: #008080">5</span> [ <span style="color: #800080">1</span> -eq <span style="color: #800080">2</span> -a -n <span style="color: #800000">"</span><span style="color: #800000">`echo true 1>&2`</span><span style="color: #800000">"</span> ]                #<span style="color: #0000ff">true,这是错误的结果,难道是因为单括号数值计算中有两个条件判断吗?</span>
<span style="color: #008080">6</span> <span style="color: #008080">7</span> [[ <span style="color: #800080">1</span> -eq <span style="color: #800080">2</span> && -n <span style="color: #800000">"</span><span style="color: #800000">`echo true 1>&2`</span><span style="color: #800000">"</span> ]               #(not output),有些-a 或 -o 不正确的情况,显然&&或||要稳妥些
Salin selepas log masuk

-a 和 -o一般与[ ]使用,如:[ "$exp1" -a "$exp2" ]

&& 和 || 一般与[[ ]] 使用,如:[[ condition1 && condition2 ]]

 

<span style="color: #008080">1</span> <span style="color: #000000">例2
</span><span style="color: #008080">2</span> [ $a == z*<span style="color: #000000"> ]      #<span style="background-color: #00ff00">模式匹配:如果$a以a开头,则为true
</span></span><span style="color: #008080">3</span> [ $a == <span style="color: #800000">"</span><span style="color: #800000">z*</span><span style="color: #800000">"</span> ]    #<span style="background-color: #00ff00">字符匹配:如果$a的值等于z*<span style="color: #000000">,则为true
</span></span><span style="color: #008080">4</span> 
<span style="color: #008080">5</span> [ $a = z*<span style="color: #000000"> ]       #<span style="background-color: #00ff00">file globbing and word splitting 将会发生,什么意思?
</span></span><span style="color: #008080">6</span> [ $a = <span style="color: #800000">"</span><span style="color: #800000">z*</span><span style="color: #800000">"</span> ]     #<span style="background-color: #00ff00">字符匹配:如果$a的值等于z*,则为true</span>
Salin selepas log masuk

以上属于字符串的比较,只不过==的功能在[[]][]中的行为是不同的

 

<span style="color: #008080"> 1</span> <span style="color: #000000">例3
</span><span style="color: #008080"> 2</span> <span style="color: #000000">#只能用[[ ]] 可以进行进制转换比较
</span><span style="color: #008080"> 3</span> 
<span style="color: #008080"> 4</span> <span style="color: #0000ff">decimal</span>=<span style="color: #800080">15</span><span style="color: #000000">                                     #十进制
</span><span style="color: #008080"> 5</span> octial=<span style="color: #800080">017</span><span style="color: #000000">                                     #八进制
</span><span style="color: #008080"> 6</span> hex=<span style="color: #800080">0x0f</span><span style="color: #000000">                                       #十六进制
</span><span style="color: #008080"> 7</span> <span style="color: #0000ff">if</span> [ <span style="color: #800000">"</span><span style="color: #800000">$decimal</span><span style="color: #800000">"</span> -eq <span style="color: #800000">"</span><span style="color: #800000">$octal</span><span style="color: #800000">"</span><span style="color: #000000"> ]
</span><span style="color: #008080"> 8</span> <span style="color: #0000ff">then</span>
<span style="color: #008080"> 9</span>     <span style="color: #0000ff">echo</span> <span style="color: #800000">"</span><span style="color: #800000">$decimal equals $octal</span><span style="color: #800000">"</span>
<span style="color: #008080">10</span> <span style="color: #0000ff">else</span>
<span style="color: #008080">11</span>     <span style="color: #0000ff">echo</span> <span style="color: #800000">"</span><span style="color: #800000">$decimal is not equal to $octal</span><span style="color: #800000">"</span>     # <span style="color: #800080">15</span> is not equal to <span style="color: #800080">017</span>
<span style="color: #008080">12</span> <span style="color: #0000ff">fi</span>              <br>                                                  # 不能用单括号[ ]计算!
<span style="color: #008080">13</span> <span style="color: #0000ff">if</span> [[ <span style="color: #800000">"</span><span style="color: #800000">$decimal</span><span style="color: #800000">"</span> -eq <span style="color: #800000">"</span><span style="color: #800000">$octal</span><span style="color: #800000">"</span><span style="color: #000000"> ]]
</span><span style="color: #008080">14</span> <span style="color: #0000ff">then</span>
<span style="color: #008080">15</span> <span style="color: #0000ff">echo</span> <span style="color: #800000">"</span><span style="color: #800000">$decimal equals $octal</span><span style="color: #800000">"</span>                  # <span style="color: #800080">15</span> equals <span style="color: #800080">017</span>
<span style="color: #008080">16</span> <span style="color: #0000ff">else</span>
<span style="color: #008080">17</span> <span style="color: #0000ff">echo</span> <span style="color: #800000">"</span><span style="color: #800000">$decimal is not equal to $octal</span><span style="color: #800000">"</span>
<span style="color: #008080">18</span> <span style="color: #0000ff">fi</span>                                             # 要用双括号[[ ]]计算!
<span style="color: #008080">19</span> <span style="color: #0000ff">if</span> [[ <span style="color: #800000">"</span><span style="color: #800000">$decimal</span><span style="color: #800000">"</span> -eq <span style="color: #800000">"</span><span style="color: #800000">$hex</span><span style="color: #800000">"</span><span style="color: #000000"> ]]
</span><span style="color: #008080">20</span> <span style="color: #0000ff">then</span>
<span style="color: #008080">21</span> <span style="color: #0000ff">echo</span> <span style="color: #800000">"</span><span style="color: #800000">$decimal equals $hex</span><span style="color: #800000">"</span>                    # <span style="color: #800080">15</span> equals <span style="color: #800080">0x0f</span>
<span style="color: #008080">22</span> <span style="color: #0000ff">else</span>
<span style="color: #008080">23</span> <span style="color: #0000ff">echo</span> <span style="color: #800000">"</span><span style="color: #800000">$decimal is not equal to $hex</span><span style="color: #800000">"           #<span class="fontstyle0">[[ $hexadecimal ]]也能计算<br></span></span>
<span style="color: #008080">24</span> <span style="color: #0000ff">fi</span> 
Salin selepas log masuk

 

Atas ialah kandungan terperinci 《Advanced Bash-scripting Guide》学习(十八):[[ ]]与[ ]的一些特殊情况. Untuk maklumat lanjut, sila ikut artikel berkaitan lain di laman web China PHP!

sumber:php.cn
Kenyataan Laman Web ini
Kandungan artikel ini disumbangkan secara sukarela oleh netizen, dan hak cipta adalah milik pengarang asal. Laman web ini tidak memikul tanggungjawab undang-undang yang sepadan. Jika anda menemui sebarang kandungan yang disyaki plagiarisme atau pelanggaran, sila hubungi admin@php.cn
Tutorial Popular
Lagi>
Muat turun terkini
Lagi>
kesan web
Kod sumber laman web
Bahan laman web
Templat hujung hadapan
Tentang kita Penafian Sitemap
Laman web PHP Cina:Latihan PHP dalam talian kebajikan awam,Bantu pelajar PHP berkembang dengan cepat!