网创优客建站品牌官网
为成都网站建设公司企业提供高品质网站建设
热线:028-86922220
成都专业网站建设公司

定制建站费用3500元

符合中小企业对网站设计、功能常规化式的企业展示型网站建设

成都品牌网站建设

品牌网站建设费用6000元

本套餐主要针对企业品牌型网站、中高端设计、前端互动体验...

成都商城网站建设

商城网站建设费用8000元

商城网站建设因基本功能的需求不同费用上面也有很大的差别...

成都微信网站建设

手机微信网站建站3000元

手机微信网站开发、微信官网、微信商城网站...

建站知识

当前位置:首页 > 建站知识

shell编程之条件语句——if与case

shell编程之条件语句——if与case

一、文件测试

1.1test 命令

测试特定的表达式是否成立,成立返回值为0,反之,非0

创新互联建站坚信:善待客户,将会成为终身客户。我们能坚持多年,是因为我们一直可值得信赖。我们从不忽悠初访客户,我们用心做好本职工作,不忘初心,方得始终。十载网站建设经验创新互联建站是成都老牌网站营销服务商,为您提供成都网站建设、成都网站制作、网站设计、html5、网站制作、品牌网站设计、小程序定制开发服务,给众多知名企业提供过好品质的建站服务。

test 条件表达式

[ 条件表达式 ] ——内容与括号之间加空格

文件测试

[ 操作符 文件或者目录 ]

常用的测试操作符
●-d:测试是否为目录(Directory)
●-e:测试目录或文件是否存在(Exist)
●-f:测试是否为文件(File)
●-r:测试当前用户是否有权限读取(Read)
●-w:测试当前用户是否有权限写入(Write)
●-x: 测试当前用户是否有权限执行(Excute)

实例如下(新手务必自己动手敲一敲体验理解):

[root@localhost ~]# ls -l shell/
总用量 16
-rw-r--r--. 1 root root 134 11月 25 19:59 demo.sh
-rwxr-xr-x. 1 root root 274 11月 25 18:58 state.sh
-rwxr-xr-x. 1 root root 208 11月 25 19:17 sujiaju.sh
-rwxr-xr-x. 1 root root 345 11月 25 19:11 welcome.sh

[root@localhost ~]#  [ -d shell/ ]          //第一种测试方法,对当前用户有效,或者test -d shell/
[root@localhost ~]# echo $?
0
[root@localhost ~]#  [ -d shell/state.sh ]
[root@localhost ~]# echo $?
1
[root@localhost ~]# test -d shell/           //第二种测试方法,对当前用户有效
[root@localhost ~]# echo $?
0
[root@localhost ~]# test -d shell/welcome.sh 
[root@localhost ~]# echo $?
1

[root@localhost ~]# test -x shell/welcome.sh 
[root@localhost ~]# echo $?
0
[root@localhost ~]# test -x shell/demo.sh 
[root@localhost ~]# echo $?
1

//上述过程可以使用&&一次执行
[root@localhost ~]# test -x shell/welcome.sh && echo "yes"
yes
[root@localhost ~]# echo $?
0
[root@localhost ~]#  [ -d shell/state.sh ]&& echo "yes"
[root@localhost ~]# echo $?
1

test 命令也可以用于判断文件之间的关系:

  • -nt ——判断A文件是否比B文件新
  • -ot——判断A文件是否比B文件旧

  • -ef——判断两个文件是否为同一个文件(inode是否相同)

1.2整数值比较

[ 整数1 操作符 整数2]

■常用的测试操作符
●-eq:等于(Equal)
●-ne:不等于(Not Equal)
●-gt:大于(Greater Than)
●-lt: 小于(Lesser Than)
●-le: 小于或等于(Lesser or Equal)
●-ge:大于或等于(Greater or Equal)

参照实例:

[root@localhost shell]# [ 5 -gt 3 ]&&echo "yes"
yes
[root@localhost shell]# [ 5 -lt 3 ]&&echo "yes"
[root@localhost shell]# 

1.3字符串比较

●格式1
[ 字符串1 = 字符串2 ]
[ 字符串1 != 字符串2 ]
●格式2
[ -Z字符串]
■常用的测试操作符
●=:字符串内容相同
●!=:字符串内容不同,!号表示相反的意思
●-Z:字符串内容为空

[root@localhost shell]# [ hello = hello ]&&echo "yes"
yes
[root@localhost shell]# [ hello = hell ]&&echo "yes"
[root@localhost shell]# [ hello != hell ]&&echo "yes"
yes
[root@localhost shell]# [ -z ]&&echo "yes"
yes
[root@localhost shell]# [ -z hello ]&&echo "yes"
[root@localhost shell]# 

1.4逻辑测试

​ 格式1:[表达式1]操作符[表达式2]
​ 格式2:命令1操作符命令2 ...
常用的测试操作符
​ ●-a或&&:逻辑与,'而且”的意思
​ ●-o或||: 逻辑或,“或者” 的意思
​ ●!:逻辑否

实例参照上面的字符串测试实例即可。

五、if语句的结构

5.1单分支结构:

if 条件测试操作

​ then 命令序列

fi

5.2双分支结构

if 条件测试操作

​ then 命令序列1

​ else 命令序列2

fi

5.3多分支结构

if 条件测试操作1

​ then 命令序列1

elif 条件测试操作2

​ then 命令序列2

else

命令序列3

fi

六、case语句结构

结构格式:

case 变量值 in

模式1)

    命令序列1

    ;;

模式2)

    命令序列2

    ;;

*)

    默认命令序列

esac

总结:if条件判断一般是用于区间判断,case一般用于固定值或字符串判断。


当前名称:shell编程之条件语句——if与case
本文来源:http://bjjierui.cn/article/jidedh.html

其他资讯