1、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 (>=) -lt(<=) -eq(=) -ne(不等于 !=)
可以使用 &&(并且) ||(或者) 结合多个条件来判断:
if [ $a -gt 5 ] || [ $a -lt 19 ];then
if [ $b -ge 5 ] && [ $b -lt 10 ];then
shell脚本实践:
1:for语句循环: for i in `seq 1 5`
[root@localhost_02 ~]# for i in `seq 1 5`;do echo $i;done
1
2
3
4
5
[root@localhost_02 ~]# for i in `seq 1 5`
> do
> echo $i
> done
1
2
3
4
5
2:if判断里面第一种格式: if 条件 ;then 语句;fi
[root@localhost_02 shell]# cat if.sh
#!/bin/bash
a=3
if [ $a -gt 2 ]
then
echo ok
fi
[root@localhost_02 shell]# sh if.sh
ok
3:if判断里的第二种格式:if 条件;then 语句;else 语句;fi
[root@localhost_02 shell]# cat if2.sh
#!/bin/bash
a=5
if [ $a -lt 3 ]
then
echo ok
else
echo nook
fi
[root@localhost_02 shell]# sh if2.sh
nook
4:if判断力的第三种格式:if 条件;then 语句;elif 条件 then 语句 else 语句;fi
[root@localhost_02 shell]# cat if3.sh
#!/bin/bash
a=8
if [ $a -gt 10 ]
then
echo ">10"
elif [ $a -gt 5 ] || [ $a -lt 10 ]
then
echo ">5 && <10"
else
echo nook
fi
[root@localhost_02 shell]# sh -x if3.sh
+ a=8
+ '[' 8 -gt 10 ']'
+ '[' 8 -gt 5 ']'
+ echo '>5 && <10'
>5 && <10
注释:在一个脚本里,elif可以写多个的,else是除去定义之外的其他条件:
若要使用符号(<>=)等,可以使用如下:
[root@localhost_02 shell]# a=3
[root@localhost_02 shell]# if (($a>2));then echo ok;fi
ok
注释:逻辑判断表达式:
-gt(>) 大于 -lt(<) 小于 -ge(>=) 大于等于 -le(<=) 小于等于 -eq(=) 等于 -ne(!=) 不等于
2、 if 判断文件目录、属性:
[ -f file ]:判断是否为普通文件,且存在:
[ -d file ]:判断是否是目录,且存在:
[ -e file ]:判断文件或目录是否存在(可以是文件或者目录):
[ -r file ]:判断文件是否可读:
[ -w file ]:判断文件是否可写:
[ -x file ]:判断文件是否可执行:
[ -z 变量 ]:判断变量是否为空:
实践:
if [ -f file ] 判断是否为普通文件且存在:
[root@localhost_02 shell]# cat if5.sh #判断f是否是文件且存在,不存在则创建:
#!/bin/bash
f='/tmp/fenye'
if [ -f $f ]
then
echo $f exist
else
touch $f
fi
[root@localhost_02 shell]# sh -x if5.sh #第一次运行时,不存在则创建:
+ f=/tmp/fenye
+ '[' -f /tmp/fenye ']'
+ touch /tmp/fenye
[root@localhost_02 shell]# sh if5.sh #第二次运行时,提示文件已存在:
/tmp/fenye exist
if [ -d file ] 判断是否为目录,且存在:
[root@localhost_02 shell]# cat if6.sh
#!/bin/bash
d='/tmp/fenye'
if [ -d $d ]
then
echo $d exits;
else
mkdir $d
fi
[root@localhost_02 shell]# sh -x if6.sh
+ d=/tmp/fenye
+ '[' -d /tmp/fenye ']'
+ mkdir /tmp/fenye
mkdir: 无法创建目录"/tmp/fenye": 文件已存在
注释:文件和文件都可以touch 的,touch的目的是如果这个文件或目录不存在,它会创建这个文件,如果这个文件或目录存在了,在touch 就会更改这个文件的三个 time:
if [ -e file] 判断文件或目录是否存在:
[root@localhost_02 shell]# cat if7.sh
#!/bin/bash
if [ -e /tmp/fenye ]
then
echo file is exist;
else
touch $d
fi
[root@localhost_02 shell]# sh if7.sh
file is exist
if [ -r file ] 判断文件是否可读:
[root@localhost_02 shell]# cat if7.sh
#!/bin/bash
d='/tmp/fenye'
if [ -r $d ]
then
echo file is read;
fi
[root@localhost_02 shell]# sh -x if7.sh
+ d=/tmp/fenye
+ '[' -r /tmp/fenye ']'
+ echo file is read
file is read
if [ -w file ] 判断文件是否可写:
[root@localhost_02 shell]# cat if8.sh
#!/bin/bash
d='/tmp/fenye'
if [ -w $d ]
then
echo file $d is writ;
fi
[root@localhost_02 shell]# sh -x if8.sh
+ d=/tmp/fenye
+ '[' -w /tmp/fenye ']'
+ echo file /tmp/fenye is writ
file /tmp/fenye is writ
注释:通过去判断文件的可读可写,就可以判断当前执行shell脚本的是那个用户:
[root@localhost_02 shell]# cat if8.sh
#!/bin/bash
d='/tmp/fenye'
if [ -x $d ]
then
echo file $d is writ;
else
echo file $d no exec;
fi
[root@localhost_02 shell]# sh -x if8.sh
+ d=/tmp/fenye
+ '[' -x /tmp/fenye ']'
+ echo file /tmp/fenye no exec
file /tmp/fenye no exec
使用常用的: && 并且 或者 ||
[root@localhost_02 shell]# cat if9.sh
#!/bin/bash
f='/tmp/fenye'
[ -f $f ] && rm -fr &f #前面的命令执行成功,才会执行后面的命令;
等同于下面的表达式:
if [ -f $f ]
then
rm -fr $f;
fi
[root@localhost_02 shell]# cat if10.sh
#!/bin/bash
f='/tmp/fenye'
[ ! -f $f ] || touch $f #表示前面的命令执行不成功,才会执行后面的命令;
等同于下面的命令:
if [ -f $f ] #表示第一条命令执行不成功,才会往下执行;
then
touch $f
fi
if 判断的一些特殊用法:
if [ -z $a ] 表示当判断变量a的值为空时会怎么样:
if [ -n $a ] 表示当判断变量a的值不为空会怎么样:
if grep -q "123" passwd;then 表示判断passswd文本中含有123的行时会怎么样:
if [ ! -e file ] 表示文件不存在时则怎么样:
if (($a>5));then..... 等同于 if [ $a -gt 5 ];then .....
[ ]中不能使用<,>,==,!=,>=,<=这样的符合(需要结合两个圆括号使用):
实践:
注释: -z 或者 -n 不能作用在文件上,只能作用在变量上:两个正好相反:建议使用这两个参数时变量都加上双引号:
!-z === -n
!-n === -z
[root@localhost_02 shell]# cat if11.sh
#!/bin/bash
n=`wc -l /tmp/lala`
if [ -z "$n" ]
then
echo error
exit
elif [ $n -gt 100 ]
then
echo abck
fi
[root@localhost_02 shell]# sh -x if11.sh
++ wc -l /tmp/lala
wc: /tmp/lala: 没有那个文件或目录
+ n=
+ '[' -z '' ']'
+ echo error
error
+ exit
改良后:加入判断:
[root@localhost_02 shell]# cat if12.sh
#!/bin/bash
if [ -f tmp/lala ]
then
echo "The /tmp/lala not exits"
exit
fi
n=`wc -l /tmp/lala`
if [ -z "$n" ]
then
echo error
exit
elif [ $n -gt 100 ]
then
echo adbc
fi
[root@localhost_02 shell]# sh if12.sh
wc: /tmp/lala: 没有那个文件或目录
[ -n $a ] 判断变量不为空时发生什么: 等同于 ! -z
注释:用 “-n”选项时,如果是变量时,则要用双引号引起了,如果是文件则不需要:(一般不能作用在文件上)
[root@localhost_02 shell]# a=1
[root@localhost_02 shell]# if [ -n "$a" ];then echo ok;else echo nook;fi #a声明了变量:
ok
[root@localhost_02 shell]# if [ -n "$b" ];then echo ok;else echo nook;fi #b没有声明变量:
nook
3: 在逻辑判断中,有时候还可以使用一个命令的结果作为判断条件:
判断某个文件中是否含有某个字符:(某个系统中是否含有某个用户):
grep -wq "zabbix" /etc/passwd
-w:后面跟单词,表示精确匹配后面的单词: -q:表示静默输出,仅仅过滤,不输出结果,一般用于脚本中:
[root@localhost_02 shell]# if grep -wq "zabbix" /etc/passwd;then echo zabbix exist;else echo zabbix not exist; fi
zabbix exist
如上:判断zabbix用户是否存在:
也或者,有时想用户不存在,则创建这个用户,直接取反,如下:
[root@localhost_02 shell]# if ! grep -wq "fenye" /etc/passwd;then useradd fenye;else zabbix exits;fi
[root@localhost_02 shell]# grep "fenye" /etc/passwd
fenye:x:1002:1002::/home/fenye:/bin/bash
4、case判断:格式如下:
case 变量名 in
value1)
commond
;;
value2)
commond
;;
*)
commond
;;
esac
注释:在case程序中,可以在条件中使用 | ,表示或的意思,比如:
‘2|3')
commond
;;
shell脚本案例:输入成绩:
[root@localhost_02 shell]# vim case1.sh
#!/bin/bash
read -p "Please input number : " n
if [ -z "$n" ]
then
echo "Please input number"
exit 1
fi
n1=`echo $n|sed 's/[0-9]//g'`
if [ ! -z "$n1" ]
then
echo "Please input a muber"
exit 1
fi
if [ $n -le 60 ] && [ $n -gt 0 ]
then
tag=1
elif [ $n -le 80 ] && [ $n -gt 60 ]
then
tag=2
elif [ $n -le 90 ] && [ $n -gt 80 ]
then
tag=3
elif [ $n -le 100 ] && [ $n -gt 90 ]
then
tag=4
else
tag=0
fi
case $tag in
1)
echo “不及格”
;;
2)
echo "及格"
;;
3)
echo "良好"
;;
4)
echo "优秀"
;;
*)
echo "The is number range is 0-100"
;;
esac
[root@localhost_02 shell]# sh case1.sh
Please input number : 50
“不及格”
[root@localhost_02 shell]# sh case1.sh
Please input number : 90
良好
[root@localhost_02 shell]# sh case1.sh
Please input number : 101
The is number range is 0-100
注释:
shell 中 exit0 exit1 的区别:
exit(0):正常运行程序并退出程序;
exit(1):非正常运行导致退出程序;
exit 0 可以告知你的程序的使用者:你的程序是正常结束的。如果 exit 非 0 值,那么你的程序的使用者通常会认为你的程序产生了一个错误。
在 shell 中调用完你的程序之后,用 echo $? 命令就可以看到你的程序的 exit 值。在 shell 脚本中,通常会根据上一个命令的 $? 值来进行一些流程控制;