|NO.Z.00018|——————————|LinuxShell|——|Linux&Shell&条件判断.V05|——|字符串判断|

一、字符串判断
  作用
-z字符串 判断字符串是否为空(为空返回真)
-n字符串 判断字符串是否为非空(非空返回真)
字符串1=字符串2 判断字符串1是否和字符串2相等(相等返回真)
字符串!1=字符串2 判断字符串1是否和字符串2不相等(不相等返回真)
二、字符串判断实验
### --- 判断name变量是否为空,因为不为空,所以返回no
### --- 为name变量赋值

[root@localhost ~]# name=sc
### --- 判断name变量

[root@localhost ~]# [ -z "$name" ] && echo "yes" || echo "no"
no
### --- 再来看看如何判断两个字符串相等:
### --- 为2个变量赋值

[root@localhost ~]# aa=11
[root@localhost ~]# bb=22
### --- 判断两个变量的值是否相等,明显不相等,所以返回no

[root@localhost ~]# [ "$aa" == "bb" ] && echo "yes" || echo "no"
no

 
 
 
 
 
 
 
 
 

Walter Savage Landor:strove with none,for none was worth my strife.Nature I loved and, next to Nature, Art:I warm'd both hands before the fire of life.It sinks, and I am ready to depart
                                                                                                                                                   ——W.S.Landor

 

posted on 2022-03-26 23:07  yanqi_vip  阅读(10)  评论(0)    收藏  举报

导航