shell脚本中的逻辑判断&文件目录属性判断&if特殊用法&case判断

20.5 shell脚本中的逻辑判断

shell脚本中逻辑判断一般使用if语句,其中if可以理解为“如果”,then可以理解为“然后”,else可以理解为“否则”,fi为if语句结束的标志

逻辑判断表达式的书写格式

if [ $a -gt $b ]; 
if [ $a -lt 5 ]; 
if [ $b -eq 10 ]等 
语句后的逻辑表达式需要用方括号【】括起来,注意到处都是空格,语句与方括号之间,方括号与表达式变量之间,变量与变量之间均需要用空格进行间隔

-gt 代表大于号(>); -lt代表小于号(<);

-ge代表大于等于号(>=); -le代表小于等于号(<=);

-eq代表等于号(==); -ne代表不等于号(!=)

if语句的第一种格式

if 条件 ; then 语句; fi
[root@linux-5 shell]# cat 01.sh
#!/bin/bash
a=4
if [ $a -gt 3 ]
then 
   echo ok
fi
[root@linux-5 shell]# sh 01.sh
ok

if语句的第二种格式

if 条件; then 语句; else 语句; fi
[root@linux-5 shell]# cat 01.sh
#!/bin/bash
a=2
if [ $a -lt 1 ]
then 
   echo ok
else 
   echo not ok
fi
[root@linux-5 shell]# sh -x 01.sh
+ a=2
+ '[' 2 -lt 1 ']'
+ echo not ok
not ok

if语句的第三种格式

if …; then … ;elif …; then …; else …; fi
[root@linux-5 shell]# cat 01.sh
#!/bin/bash
a=3
if [ $a -lt 1 ]
then 
   echo ok
elif [ $a -gt 5 ]
then
   echo okk
else 
   echo not ok
fi
[root@linux-5 shell]# sh -x 01.sh
+ a=3
+ '[' 3 -lt 1 ']'
+ '[' 3 -gt 5 ']'
+ echo not ok
not ok

还可以使用&& || 结合多个条件

if [ $a -gt 5 ] && [ $a -lt 10 ]; then
if [ $b -gt 5 ] || [ $b -lt 3 ]; then

20.6 文件目录属性判断

if语句常和文件或目录属性相结合,进行判断,并对文件目录属性执行相关操作

[ -f file ] 判断是否是普通文件,且存在
[ -d file ] 判断是否是目录,且存在
[ -e file ] 判断文件或目录是否存在
[ -r file ] 判断文件是否可读
[ -w file ] 判断文件是否可写
[ -x file ] 判断文件是否可执行

示例

检查/tmp/目录下lem123456文件是否存在,如果存在打印结果,不存在则创建该文件

[ -f file ]判断是否是普通文件,且存在

[root@linux-5 shell]# cat !$
cat 02.sh
#!/bin/bash
f=/tmp/lem123456
if [ -f $f ]
then 
    echo $f exist
else
    touch $f
fi
[root@linux-5 shell]# sh -x 02.sh    ##第一次执行,会创建该文件
+ f=/tmp/lem123456
+ '[' -f /tmp/lem123456 ']'
+ touch /tmp/lem123456
[root@linux-5 shell]# ls /tmp
lem123456       
[root@linux-5 shell]# sh -x 02.sh    ##第二次执行,会提示该文件已存在
+ f=/tmp/lem123456
+ '[' -f /tmp/lem123456 ']'
+ echo /tmp/lem123456 exist
/tmp/lem123456 exist

文件是可以touch 的,touch的目的是,如果这个文件或目录不存在,它会创建这个文件(创建目录则用mkdir),如果这个文件或目录存在了,再touch就会更改这个文件的三个time

[ -d file ] 判断是否是目录,且存在

[root@linux-5 shell]# cat !$
cat 02.sh
#!/bin/bash
f=/tmp/lem123456
if [ -d $f ]
then 
    echo $f exist
else
    mkdir $f
fi
[root@linux-5 shell]# sh -x 02.sh
+ f=/tmp/lem123456
+ '[' -d /tmp/lem123456 ']'
+ mkdir /tmp/lem123456
[root@linux-5 shell]# sh -x 02.sh
+ f=/tmp/lem123456
+ '[' -d /tmp/lem123456 ']'
+ echo /tmp/lem123456 exist
/tmp/lem123456 exist

[ -e file ] 判断文件或目录是否存在

[root@linux-5 shell]# cat 02.sh
#!/bin/bash
f=/tmp/lem123456
if [ -e $f ]
then 
    echo $f exist
else
    mkdir $f
fi
[root@linux-5 shell]# sh 02.sh
/tmp/lem123456 exist

[ -r file ] 判断文件是否可读

#!/bin/bash
f=/tmp/lem123456
if [ -r $f ]
then 
    echo $f read
fi
[root@linux-5 shell]# sh 02.sh
/tmp/lem123456 read

[ -w file ] 判断文件是否可写

[root@yong-01 shell]# vim file4.sh

#! /bin/bash
f="/tmp/yongge"
if [ -w $f ]
then
        echo $f writeadable
fi
[root@yong-01 shell]# sh file4.sh 
/tmp/yongge writeadable

[ -x file ] 判断文件是否可执行

[root@linux-5 shell]# cat 02.sh
#!/bin/bash
f=/tmp/lem123456
if [ -x $f ]
then 
    echo $f exe
fi
[root@linux-5 shell]# sh 02.sh
/tmp/lem123456 exe

注:判断是否可读可写可执行,判断的是当前执行shell脚本的用户的权限

判断语句与逻辑符号结合

并且 &&

[ -f $f ] && rm -f $f     //前一条命令执行成功才会继续执行之后的命令
等同于下面的表达方式
if [ -f $f ]     
then
      rm -rf $f
fi

或者 ||

if [ -f $f ] || touch $f fi   //前面命令不成功时,执行后面的命令
等同于
if [ -f $f ]
then
    echo exist
else 
    touch $f
fi

取反"!"

if [ ! -f $f ]        //  “!”表示取反,判断是否不是普通文件,且不存在
then
      touch $f
fi

20.7 if特殊用法

if [ -z "$a" ]  这个表示当变量a的值为空时会怎么样
if [ -n "$a" ] 表示当变量a的值不为空
if grep -q '123' 1.txt; then  表示如果1.txt中含有'123'的行时会怎么样
if [ ! -e file ]; then 表示文件不存在时会怎么样
if (($a<1)); then …等同于 if [ $a -lt 1 ]; then… 
[ ] 中不能使用<,>,==,!=,>=,<=这样的符号
!-z=-n
!-n=-z

注:if -z或者if -n 都不能作用在文件上,只能作用在变量上。

示例

if [ -z "$a" ]

[root@linux-5 shell]# cat 03.sh
#!/bin/bash
n=`wc -l /tmp/lalala`
if [ -z $n ]
then 
    echo error
fi
[root@linux-5 shell]# sh -x 03.sh
++ wc -l /tmp/lalala
wc: /tmp/lalala: 没有那个文件或目录
+ n=
+ '[' -z ']'
+ echo error
error

if [ -n "$a" ]

[root@linux-5 shell]# cat 03.sh
#!/bin/bash
n=`wc -l /tmp/lalala`
if [ -n "$n" ]
then 
    echo ok
else
    echo error
fi
[root@linux-5 shell]# sh -x 03.sh
++ wc -l /tmp/lalala
wc: /tmp/lalala: 没有那个文件或目录
+ n=
+ '[' -n '' ']'
+ echo error
error

注:变量中包含命令需要加反引号``

if语句与grep结合

grep -wq

其中-w为精确匹配,意为w后跟一个单词,而不是包含指定字符串,避免出现需要检索user1却检索出user1和user11的情况,-q仅仅做一个过滤,而不显示过滤的内容

如果系统进程中含有mysql的进程时会怎么样

[root@linux-5 shell]# cat 03.sh
#!/bin/bash
if ps aux|grep -wq '3306'
then 
    echo ok
else
    echo error
fi
[root@linux-5 shell]# sh -x 03.sh
+ grep -wq 3306
+ ps aux
+ echo ok
ok

如果取反则在grep前加入!即可

20.8/20.9 case判断 

case判断的格式

d7fe1983c8d91a8748bc82c05403a9b951e.jpg

示例

该脚本目的是:输入一个数字,然后用脚本去判断这个数字的范围

#!/bin/bash
#判断是否输入有数值,空直接结束整个文本
read -p "Please input a number: " n    
#read -p捕获用户所输入的字符串;赋值给最后一个变量;这里的赋值是“n”
if [ -z "$n" ]                    ##变量n 为空
then
    echo "Please input a number."
    exit 1   ##结束脚本,返回值1
fi
#n1将输入的字符串所包含的数字清空,并检查变量是否为空,如果不为空,就证明输入有其他的字符,告知用户,请输入一个数字
n1=`echo $n|sed 's/[0-9]//g'`   //确定,n变量是否为数字
if [ -n "$n1" ]
then
echo "Please input a number."
exit 1
fi

if [ $n -lt 60 ] && [ $n -ge 0 ]
then
    tag=1
elif [ $n -ge 60 ] && [ $n -lt 80 ]
then
    tag=2
elif [ $n -ge 80 ]  && [ $n -lt 90 ]
then
    tag=3
elif [ $n -ge 90 ] && [ $n -le 100 ]
then
    tag=4
else
    tag=0
fi
case $tag in
    1)
         echo "not ok"
        ;;
    2)
         echo "ok"
        ;;
    3)
         echo "okok"
        ;;
    4)
         echo "okokok"
        ;;
    *)
         echo "The number range is 0-100."
        ;;
esac

注:

exit(0):正常运行程序并退出程序;

exit(1):非正常运行导致退出程序;

exit 0 可以告知你的程序的使用者:你的程序是正常结束的。如果 exit 非 0 值,那么你的程序的使用者通常会认为你的程序产生了一个错误。在 shell 中调用完你的程序之后,用 echo $? 命令就可以看到你的程序的 exit 值。在 shell 脚本中,通常会根据上一个命令的 $? 值来进行一些流程控制。

转载于:https://my.oschina.net/u/3804357/blog/1848638

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值