您的位置:首页 > 运维架构 > Shell

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

2018-07-12 15:53 1071 查看

shell脚本中的逻辑判断

格式1:if 条件 ; then 语句; fi

格式2:if 条件; then 语句; else 语句; fi

格式3:if …; then … ;elif …; then …; else …; fi

逻辑判断表达式:if [ $a -gt $b ]; if [ $a -lt 5 ]; if [ $b -eq 10 ]等 -gt (>); -lt(<); -ge(>=); -le(<=);-eq(==); -ne(!=) 注意到处都是空格

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

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

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

shell脚本中的逻辑判断实战

for语句循环

[root@yong-01 ~]# for i in `seq 1 5`;do echo $i;done
1
2
3
4
5
[root@yong-01 ~]# for i in `seq 1 5`
> do
> echo $i
> done
1
2
3
4
5


if语句第一种格式

格式1:if 条件 ; then 语句; fi

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

#!/bin/bash
a=5
if [ $a -gt 3 ]
then
echo OK
fi
[root@yong-01 shell]# sh if1.sh
OK


if语句第二种格式

格式2:if 条件; then 语句; else 语句; fi

[root@yong-01 shell]# cp if1.sh if2.sh
[root@yong-01 shell]# vim if2.sh

#! /bin/bash
a=1
if [ $a -gt 3 ]
then
echo OK
else
echo nook
fi
[root@yong-01 shell]# sh -x if2.sh
+ a=1
+ '[' 1 -gt 3 ']'
+ echo NOOK
NOOK


if语句第三种格式

格式3:if …; then … ;elif …; then …; else …; fi

[root@yong-01 shell]# vim if3.sh
[root@yong-01 shell]# cat if3.sh
#! /bin/bash
a=6
if  [ $a -lt 5 ]
then
echo "a<5"
elif [ $a -gt 5 ] && [ $a -lt 9 ]
then
echo "5<a<9"
else
echo "a>9"
fi
[root@yong-01 shell]# sh -x if3.sh
+ a=6
+ '[' 6 -lt 5 ']'
+ '[' 6 -gt 5 ']'
+ '[' 6 -lt 9 ']'
+ echo '5<a<9'
5<a<9


逻辑判断表达式

if [ $a -gt $b ] 表示,大于

if [ $a -lt 5 ] 表示,小于

if [ $b -eq 10 ] 表示,等于10

-ne(!=) 表示,不等于

-ge(>=) 表示,大于等于

-le(<=) 表示,小于等于

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

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

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

if文件目录属性判断

if 判断文件、目录属性

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

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

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

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

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

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

文件目录属性判断实战

if 判断文件、目录属性

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

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

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


if 判断文件、目录属性

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

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

#! /bin/bash
f="/tmp/yongge"
if [ -d $f ]
then
echo $f exist
else
mkdir $f
fi
[root@yong-01 shell]# sh -x file2.sh
+ f=/tmp/yongge
+ '[' -d /tmp/yongge ']'
+ mkdir /tmp/yongge


if 判断文件、目录属性

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

目录和文件都可以touch 的,touch的目的是 如果这个文件或目录不存在,它会创建这个文件,如果这个文件或目录存在了,在touch 就会更改这个文件的三个 time

[root@yong-01 shell]# vim file3.sh
[root@yong-01 shell]# sh -x file3.sh
+ f=/tmp/yongge
+ '[' -e /tmp/yongge ']'
+ echo /tmp/yongge exist
/tmp/yongge exist


if 判断文件、目录属性

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

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

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


if 判断文件、目录属性

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

去判断是否刻度可写,就判断执行shell脚本的当前用户

[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


if 判断文件、目录属性

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

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

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


常用案例

并且 &&

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


或者 ||

f="/tmp/yongge"
[ -f $f ] || touch $f    //前面命令不成功时,执行后面的命令
if [ ! -f $f ]        //  “!”表示了如果这条命令不成功,就往下执行
then
touch $f
fi


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…

[ ] 中不能使用<,>,==,!=,>=,<=这样的符号

if 特殊用法实战

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

if [ -z "$a" ] 这个表示当变量a的值为空时会怎么样

-z 表示为空

!-z=-n

!-n=-z

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

#! /bin/bash
n=`wc -l /tmp/lala`
if [ -z "$n" ]
then
echo error
exit
elif [ $n -gt 100 ]
then
echo abcd
fi
[root@yong-01 shell]# sh -x file1.sh
++ wc -l /tmp/lala
wc: /tmp/lala: 没有那个文件或目录
+ n=
+ '[' -z '' ']'
+ echo error
error
+ exit

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

#! /bin/bash
if [ ! -f /tmp/lala ]
then
echo "/tmp/lala not exist."
exit
fi
n=`wc -l /tmp/lala`
if [ -z "$n" ]
elif [ $n -gt 100 ]
then
echo abcd
fi
[root@yong-01 shell]# sh file1.sh ]
/tmp/lala not exist.


if [ -n "$a" ] 表示当变量a的值不为空,或者说这个文件内容不为空

-n 判断变量的时候,需要用""双引号引起来,若是文件的时候,则不需要用双引号引起来

[root@yong-01 shell]# if [ -n 01.sh ]; then echo ok; fi
ok
[root@yong-01 shell]# echo $b

[root@yong-01 shell]# if [ -n "$b" ]; then echo $b; else echo "b is null"; fi
b is null


if grep -q '123' 1.txt; then 表示如果1.txt中含有'123'的行时会怎么样

grep -wq 其中-w 后跟一个单词,-q仅仅做一个过滤

比如,若是想创建一个用户,直接取反即可,如if ! grep -wq 'zabbix' /etc/passwd; then useradd zabbix; fi zabbix exist

[root@yong-01 shell]# grep -w 'zabbix' /etc/passwd
zabbix:x:997:994:Zabbix Monitoring System:/var/lib/zabbix:/sbin/nologin
[root@yong-01 shell]# if grep -wq 'zabbix' /etc/passwd; then echo "zabbix exist"; fi
zabbix exist


if [ ! -e file ]; then 表示文件不存在时会怎么样

if (($a<1)); then …等同于 if [ $a -lt 1 ]; then…

[ ] 中不能使用<,>,==,!=,>=,<=这样的符号 一个等于号= 是赋值

case判断



shell脚本案例:

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

#!/bin/bash
#判断是否输入有数值,空直接结束整个文本
read -p "Please input a number: " n
#read 让用户输出一些字符串;赋值给最后一个变量;这里的赋值是“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 "ook"
;;
4)
echo "oook"
;;
*)
echo "The input value exceeds the calculation range.The number range is 0-100."
;;
esac


知识点 1

shell 中 exit0 exit1 的区别:

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

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

exit 0 可以告知你的程序的使用者:你的程序是正常结束的。如果 exit 非 0 值,那么你的程序的使用者通常会认为你的程序产生了一个错误。

在 shell 中调用完你的程序之后,用 echo $? 命令就可以看到你的程序的 exit 值。在 shell 脚本中,通常会根据上一个命令的 $? 值来进行一些流程控制。
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签:  zabbix GT
相关文章推荐