接上篇文章《shell test 条件表达式说明》,其他参数说明:
0 1 2 3 4 5 6 7 8 9 |
equal:在[]中使用-eq、在(())[[]]中使用==; not-equal:在[]中使用-ne、在(())[[]]中使用!=; less-than:在[]中使用-lt、在(())[[]]中使用<; less-than-or-equal:在[]中使用-le、在(())[[]]中使用<=; greater-than:在[]中使用-gt、在(())[[]]中使用>; greater-than-or-equal:在[]中使用-ge、在(())[[]]中使用>=; 逻辑操作符: and:在[]中使用-a、在(())[[]]中使用&&; or:在[]中使用-o、在(())[[]]中使用||; !:在[]中使用!、在(())[[]]中使用!; |
试验:=、!=测试
0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 |
[root@LAMP script]# [ 2 == 2 ] && echo right || echo wrong #如果2=2,则打印right,否则wrong right [root@LAMP script]# [ 2 != 2 ] && echo right || echo wrong #如果2≠2,则打印right,否则wrong wrong [root@LAMP script]# [ 2 == 3 ] && echo right || echo wrong #如果2=3,则打印right,否则wrong wrong [root@LAMP script]# [ 2 -eq 2 ] && echo right || echo wrong right [root@LAMP script]# [ 2 -ne 2 ] && echo right || echo wrong wrong [root@LAMP script]# [ 2 -eq 3 ] && echo right || echo wrong wrong [root@LAMP script]# [[ 2 == 2 ]] && echo right || echo wrong right [root@LAMP script]# [[ 2 != 2 ]] && echo right || echo wrong wrong [root@LAMP script]# [[ 2 == 3 ]] && echo right || echo wrong wrong |
试验:<、>测试
0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 |
[root@LAMP script]# [ 2 -lt 3 ] && echo right || echo wrong right [root@LAMP script]# [ 2 -gt 3 ] && echo right || echo wrong wrong [root@LAMP script]# [[ 2 < 3 ]] && echo right || echo wrong right [root@LAMP script]# [[ 2 > 3 ]] && echo right || echo wrong wrong ########################特殊备注######################## [root@LAMP script]# [ 2 < 3 ] && echo right || echo wrong right [root@LAMP script]# [ 2 > 3 ] && echo right || echo wrong right [root@LAMP script]# [ 2 \< 3 ] && echo right || echo wrong right [root@LAMP script]# [ 2 \> 3 ] && echo right || echo wrong wrong ########################特殊备注######################## |
试验:<=、>=测试
0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 |
[root@LAMP script]# [ 2 -le 3 ] && echo right || echo wrong right [root@LAMP script]# [ 2 -ge 3 ] && echo right || echo wrong wrong [root@LAMP script]# [[ 2 -le 3 ]] && echo right || echo wrong right [root@LAMP script]# [[ 2 -ge 3 ]] && echo right || echo wrong wrong [root@LAMP script]# [ 2 <= 3 ] && echo right || echo wrong -bash: [: 2: unary operator expected wrong [root@LAMP script]# [ 2 >= 3 ] && echo right || echo wrong -bash: [: 2: unary operator expected wrong [root@LAMP script]# [[ 2 \<= 3 ]] && echo right || echo wrong -bash: conditional binary operator expected -bash: syntax error near `\<=' [root@LAMP script]# [[ 2 <= 3 ]] && echo right || echo wrong -bash: syntax error in conditional expression -bash: syntax error near `3' [root@LAMP script]# [[ 2 >= 3 ]] && echo right || echo wrong -bash: syntax error in conditional expression -bash: syntax error near `3' |
申明:本文由BYRD原创(基于GNU bash, version 4.1.2(1)),未经许可禁止转载! SourceByrd's Weblog-https://note.t4x.org/system/arithmetic-binary-operators/
申明:除非注明Byrd's Blog内容均为原创,未经许可禁止转载!详情请阅读版权申明!