SHELL条件测试和流程控制

本节所讲内容:

判断语句

if表达式

 

变量测试语句:

test

格式:

test 测试条件

测试范围: 整数,字符串,文件

 

字符串和变量:

test  $str1 == $str2  是否相等

test  $str1!= $str2 是否不相等

test   $str1  测试一个字符串内容是否不空

test  -z $str1 测试一个字符窜内容是否为空

如果返回值为1,说明$str1不为空,如果返回值为0,说明$str1为空

 [root@localhost ~]# a=1

[root@localhost ~]# b=1

[root@localhost ~]# c=2

[root@localhost ~]# test$a == $b

[root@localhost ~]# echo$?

0

[root@localhost ~]# test$a == $c

[root@localhost ~]# echo$?

1

[root@localhost ~]# test$a != $c

[root@localhost ~]# echo$?

0

 

test   $str1  测试一个字符串内容是否不空

如果返回值为1,说明$str1为空,如果返回值为0,说明$str1不为空

[root@localhost ~]# abc=abc                                #设置变量abc的内容

[root@localhost ~]# test $abc                               #测试$abc内容是否不空

[root@localhost ~]# echo $?                                 #查看测试结果

0                                                                           #返回值为0,说明$abc是不空

[root@localhost ~]# test $abcd                             #测试$abcd内容是否不空

[root@localhost ~]# echo $?                                 #查看测试结果

1                                                                           #返回值为1,说明$abcd 为空

 

test  -z $str1 测试一个字符窜内容是否为空

如果返回值为1,说明$str1不为空,如果返回值为0,说明$str1为空

[root@localhost ~]# test -z $abc                           #测试$abc内容是否为空

[root@localhost ~]# echo $?                                 #查看测试结果

1                                                                           #返回值为1,说明$abc不为空

 

[root@localhost~]# test -z $abcd                         #测试$abcd内容是否为空

[root@localhost ~]# echo $?                                 #查看测试结果

0                                                                           #返回值为0,说明$abcd为空

 

测试整数:

test  int1 -eq  int2     =

test  int1 -ge  int2   >=

test  int1 -gt  int2   >

test  int1 -le  int2   <=

test  int1 -lt  int2    <  

test  int1 -ne int2    不等于

 

[root@localhost ~]# test 1 -eq 1

[root@localhost ~]# echo $?

0

[root@localhost ~]# test 1 -eq 2

[root@localhost ~]# echo $?

1

[root@localhost ~]# test 1 -le 2

[root@localhost ~]# echo $?

0

[root@localhost ~]# test 1 -ge 2

[root@localhost ~]# echo $?

1

 

exit  0   1

 

文件测试:

test   -d    file  #测试是否为目录

test    -f  file

test    -x   file

test    -r   file

test    -w  file

test    -e  file   测试文件是否存在

test    -s   file  测试大小是为空。是否为空文件

 

test测试语句另一种常用的使用方法

[ 1 -eq 1 ]

 

[root@localhost ~]# [-d  /etc/passwd ]

[root@localhost ~]# echo$?

1

[root@localhost ~]# [-f  /etc/passwd ]

[root@localhost ~]# echo$?

0

[root@localhost ~]# [-x  /etc/passwd ]

[root@localhost ~]# echo$?

1

[root@localhost ~]# ll/etc/passwd

-rw-r--r--. 1 root root2002 Nov 22 03:46 /etc/passwd

 

&&  : 逻辑与,仅当两个条件都成立时,结果为真

|| : 逻辑或 。 两个条件有一个成立,结果为真

 

[root@localhost ~]# [-f  /etc/passwd ]  && [ -w  /etc/passwd ]

[root@localhost ~]# echo$?

0

[root@localhost ~]# [-f  /etc/passwd ]  && [ -x  /etc/passwd ]

[root@localhost ~]# echo$?

1

[root@localhost ~]# [-f  /etc/passwd ] ||  [ -x /etc/passwd ]

[root@localhost ~]# echo$?

0

 

条件测试语句在shell脚本中的应用

 

=====流程控制===

单分支if 语句

 

语法:

if 条件  

then

语句

fi

扩展 ; 分号,表示两个两个命令写在一行。互不影响。

[root@localhost ~]# ls /etc/passwd  ;  cd/etc/

/etc/passwd

[root@localhost etc]#

 

测试语句可以写成下面的格式

if   条件  ;   then

语句

fi

[root@localhost ~]# vimif1.sh

#!/bin/bash

if [ $USER != root ] ;then

        echo "err: not rootuser,permission deny"

        exit 127

fi

[root@localhost ~]#cpif1.sh  /tmp

[root@localhost ~]# chmod777 /tmp/if1.sh

[rm@localhost ~]$/tmp/if1.sh

err: not rootuser,permission deny

[rm@localhost ~]$ echo $?

127

 

双分支if语句 用法:

 

语法:

if  条件1 ; then

命令1

else

       命令2

fi

 

[root@localhost ~]# vimif2.sh

#!/bin/bash

ping -c 3 -i 0.2  $1 &> /dev/null

if [ $? -eq 0 ]

then

        echo "Host $1 is up."

else

        echo "Host $1 is down."

fi

[root@localhost ~]# chmodu+x if2.sh

[root@localhost ~]#./if2.sh 192.168.1.68

Host 192.168.1.68 is up.

[root@localhost ~]#./if2.sh 192.168.1.111

Host 192.168.1.111 isdown.

 

测试服务的运行状态

[root@localhost ~]# vim if3.sh

#!/bin/bash

systemctl status httpd&> /dev/null

if [ $? -eq 0 ]

then

        echo "http is running."

else

        echo "http is stopping."

fi

[root@localhost ~]#./if3.sh

http is stopping.

 

多分支if语句

 

更复杂的if语句:

语法:

if  条件1 ; then

命令1

elif  条件2 ; then

       命令2:

elif  条件3 ; then

       命令3

……………………

else

       命令n

fi

 

写一个测试体重的脚本

[root@localhost ~]# vimweight.sh

#!/bin/bash

read -p "Pleaseinput your weight: " weight

if [ $weight -lt 60 ] ;then

        echo "you are too thin."

elif [ $weight -ge 60 ]&& [ $weight -lt 70 ] ; then

        echo "you are perfect."

else

        echo "you are too fat."

fi

 

[root@localhost ~]#./weight.sh

Please input your weight:100

you are too fat.

[root@localhost ~]#./weight.sh

Please input your weight:67

you are perfect.

[root@localhost ~]#./weight.sh

Please input your weight:50

you are too thin.

 

测试一个文件的类型

[root@xuegod163 test]# catelif.sh

#!/bin/bash

echo  "input a file name:"

read -p "请输入你要测试的对象" file_name

if [ -d $file_name ] ; then

        echo " $file_name is a dir"

elif [ -f $file_name ] ; then

        echo " $file_name is file"

elif [ -c $file_name -o -b$file_name ] ; then

        echo " $file_name is a devicefile"

else

        echo " $file_name is an unknowfile "

fi

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值