20.5 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
格式1:不带else
[root@DasonCheng sbin]# cat if1.sh
#! /bin/bash
read -p "Please input a number:" a
if [ $a -lt 60 ]
then
echo "You didn't pass the exam."
fi
[root@DasonCheng sbin]# sh if1.sh
Please input a number:90
[root@DasonCheng sbin]# sh if1.sh
Please input a number:59
You didn't pass the exam.
格式2:带else
[root@DasonCheng sbin]# cat if2.sh
#! /bin/bash
read -p "Please input a number:" a
if [ $a -lt 60 ]
then
echo "You didn't pass the exam."
else
echo "Congratulate you pass the exam."
fi
[root@DasonCheng sbin]# sh if2.sh
Please input a number:90
Congratulate you pass the exam.
[root@DasonCheng sbin]# sh if2.sh
Please input a number:59
You didn't pass the exam.
格式3:带elif
[root@DasonCheng sbin]# cat if3.sh
#! /bin/bash
read -p "Please input a score:" a
if [ $a -lt 60 ]
then
echo "You didn't pass the exam."
elif [ $a -ge 60 ] && [ $a -le 80 ]
then
echo "Congratulate you pass the exam."
else
echo "Very good! Your score is very hight !"
fi
[root@DasonCheng sbin]# sh if3.sh
Please input a score:90
Very good! Your score is very hight !
[root@DasonCheng sbin]# sh if3.sh
Please input a score:70
Congratulate you pass the exam.
[root@DasonCheng sbin]# sh if3.sh
Please input a score:59
You didn't pass the exam.
20.6 文件目录属性判断
- [ -f file ]判断是否是普通文件,且存在
- [ -d file ] 判断是否是目录,且存在
- [ -e file ] 判断文件或目录是否存在
- [ -r file ] 判断文件是否可读
- [ -w file ] 判断文件是否可写
- [ -x file ] 判断文件是否可执行
1、判断存在和属性:
[root@DasonCheng sbin]# cat file1.sh
#! /bin/bash
a=/tmp/aming
if [ -f $a ]
then
echo "$a exist!"
else
touch $a
echo "$a will be touchd"
fi
[root@DasonCheng sbin]# sh file1.sh
/tmp/aming will be touchd
[root@DasonCheng sbin]# sh file1.sh
/tmp/aming exist!
##或者直接在命令行执行:
[root@DasonCheng sbin]# a=/tmp/aming ;if [ -f $a ] ;then echo "$a exist" ;fi //-f判断是否为文件并存在;
/tmp/aming exist
[root@DasonCheng sbin]# a=/tmp/aming ;if [ -d $a ] ;then echo "$a exist" ;fi //-d判断是否为目录并存在;
[root@DasonCheng sbin]# a=/tmp/aming ;if [ -e $a ] ;then echo "$a exist" ;fi //-e判断文件或目录是否存在;
/tmp/aming exist
2、判断权限:
是否有相应权限,是指当前执行脚本的用户;
[root@DasonCheng sbin]# ll /tmp/aming
-rw-r--r--. 1 root root 0 9月 13 09:58 /tmp/aming
[root@DasonCheng sbin]# a=/tmp/aming ;if [ -r $a ] ;then echo readable ;fi
readable
[root@DasonCheng sbin]# a=/tmp/aming ;if [ -w $a ] ;then echo writeable ;fi
writeable
[root@DasonCheng sbin]# a=/tmp/aming ;if [ -x $a ] ;then echo exeable ;fi
[root@DasonCheng sbin]# chmod u+x /tmp/aming
[root@DasonCheng sbin]# a=/tmp/aming ;if [ -x $a ] ;then echo exeable ;fi
exeable
3、if判断优化:
[root@DasonCheng sbin]# cat file2.sh
#! /bin/bash
##it will delete $a if $a exist !
a=/tmp/aming
[ -f $a ] && rm -f $a //&&作用是~并且,前面执行成功则执行后面;可以替代if下面的四行!
#if [ -f $a ]
#then
# rm -f $a
#fi
[root@DasonCheng sbin]# cat file3.sh
#! /bin/bash
a=/tmp/aming
[ -f $a ] || touch $a //||作用是~或者,前面执行成功则不执行后面,反之;可以替代下面6行!
#if [ -f $a ]
#then
#echo "$a exist!"
#else
#touch $a
#echo "$a will be touched"
#fi
[root@DasonCheng sbin]# cat file4.sh
#! /bin/bash
a=/tmp/aming
[ ! -f $a ] && touch $a //判断里面的"!"是取反的意思;
#if [ -f $a ]
#then
# rm -f $a
#else
# touch $a
#fi
#or
#if [ ! -f $a ]
#then
# touch $a
#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…
- [ ] 中不能使用<,>,==,!=,>=,<=这样的符号
1、-z作用于变量;
[root@DasonCheng sbin]# cat if.sh //这就是脚本的内容,判断变量a的值是否大于100;
#! /bin/bash
a=`wc -l /tmp/blank`
if [ $a -gt 100 ]
then
echo "$a is so longer"
fi
[root@DasonCheng sbin]# sh if.sh //执行脚本报错,我们借用参数-x;
wc: /tmp/blank: 没有那个文件或目录
if.sh: 第 3 行:[: -gt: 期待一元表达式
[root@DasonCheng sbin]# sh -x if.sh //-x显示脚本执行过程!
++ wc -l /tmp/blank
wc: /tmp/blank: 没有那个文件或目录
+ a=
+ '[' -gt 100 ']'
//这里发现 在用空值和100比较,故报错了;所以我们在引用一个变量时,或许有必要判断其是否为空值;
if.sh: 第 3 行:[: -gt: 期待一元表达式
借用-z修改后的脚本:
[root@DasonCheng sbin]# cat if.sh
#! /bin/bash
a=`wc -l /tmp/blank`
if [ -z $a ] //判断这个值为空;
then
echo "$a noexist!"
else
if [ $a -gt 100 ] //这里用到了两个if,嵌套if判断;我们或许可以在下面优化一下:
then
echo "$a is so longer"
fi
fi
[root@DasonCheng sbin]# sh if.sh
wc: /tmp/blank: 没有那个文件或目录
noexist!
优化if嵌套判断:
[root@DasonCheng sbin]# cat if.sh //这样这个脚本就显得简单了很多
#! /bin/bash
a=`wc -l /tmp/blank`
if [ -z $a ]
then
echo "$a noexist!"
else
echo "$a is so longer"
fi
[root@DasonCheng sbin]# sh if.sh
wc: /tmp/blank: 没有那个文件或目录
noexist!
##在这里,我们或许可以在定义变量之前判断/tmp/blank是否存在;使脚本不再报错:
[root@DasonCheng sbin]# cat if.sh
#! /bin/bash
if [ ! -e /tmp/blank ] //先判断这个脚本不存在
then
echo "/tep/blank noexist!"
exit //直接退出脚本
fi
a=`wc -l /tmp/blank`
if [ -z "$a" ] //这里注意:-z和-n针对脚本判断的话,一定要加上""双引号!
then
echo "$a noexist!"
else
echo "$a is so longer"
fi
[root@DasonCheng sbin]# sh if.sh
/tep/blank noexist!
20.8 cace判断
格式: case 变量名 in
value1)
command
;;
value2)
command
;;
*)
commond
;;
esac
在case程序中,可以在条件中使用|,表示或的意思, 比如
2|3)
command
;;
case案例:
需求:输入一个score分数,计算其是否合格!
考虑:
1、用户不输入任何字符
2、用户输入非数字字符
3、用户输入的数字不在0-100范围内;
[root@DasonCheng sbin]# cat case.sh
#!/bin/bash
read -p "Please input a number: " n
if [ -z "$n" ] //1、判断变量为空;针对变量,"-n"和"-z"都给我加双引号;
then
echo "Please input a number."
exit 1
## exit退出该脚本,后面的1是返回值;如果脚本这里退出了,执行echo $?返回值就为设定的1;
fi
n1=`echo $n|sed 's/[0-9]//g'` //判断变量为非数字字符;通过sed替换数字为空,再判断n1不为空;
if [ -n "$n1" ] //2、判断变量为非数字字符;针对变量,"-n"和"-z"都给我加双引号;
then
echo "Please input a number."
exit 1
fi
if [ $n -lt 60 ] && [ $n -ge 0 ]
then
tag=1 //符合0<n<60的标记为tag=1,后面case会用到;以此类推
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"
;;
*) //*代替了除了上面包含的tag,都执行下面命令;
echo "The number range is 0-100." //3、这里针对用户输入的数字范围不在0-100;
;;
esac
脚本执行效果:
[root@DasonCheng sbin]# sh case.sh
Please input a number:
Please input a number.
[root@DasonCheng sbin]# sh case.sh
Please input a number: sadfsad
Please input a number.
[root@DasonCheng sbin]# sh case.sh
Please input a number: sadfds13423
Please input a number.
[root@DasonCheng sbin]# sh case.sh
Please input a number: 101
The number range is 0-100.
[root@DasonCheng sbin]# sh case.sh
Please input a number: 10
not ok
[root@DasonCheng sbin]# sh case.sh
Please input a number: 60
ok
[root@DasonCheng sbin]# sh case.sh
Please input a number: 80
ook
[root@DasonCheng sbin]# sh case.sh
Please input a number: 99
oook