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

20.5 shell脚本中的逻辑判断

shell脚本中逻辑判断通常使用if语句,其中if能够理解为“若是”,then能够理解为“而后”,else能够理解为“不然”,fi为if语句结束的标志mysql

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

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

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

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

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

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

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

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

20.6 文件目录属性判断

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

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

示例

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

[ -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就会更改这个文件的三个time3d

[ -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脚本的用户的权限code

判断语句与逻辑符号结合

而且 &&

[ -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 都不能做用在文件上,只能做用在变量上。blog

示例

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判断的格式

示例

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

#!/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 脚本中,一般会根据上一个命令的 $? 值来进行一些流程控制。

相关文章
相关标签/搜索