20.5 shell脚本中的逻辑判断
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脚本中的逻辑判断,shell注意点
- for语句循环
[root@hf-01 ~]# for i in `seq 1 5`; do echo $i;done
1
2
3
4
5
[root@hf-01 ~]#
[root@hf-01 ~]# for i in `seq 1 5`
> do
> echo $i
> done
1
2
3
4
5
[root@hf-01 ~]#
if语句第一种格式
- 格式1:if 条件 ; then 语句; fi
[root@hf-01 shell]# vim if1.sh
#! /bin/bash
a=5
if [ $a -gt 3 ]
then
echo OK
fi
[root@hf-01 shell]# sh 03.sh
OK
[root@hf-01 shell]#
if语句第二种格式
- 格式2:if 条件; then 语句; else 语句; fi
[root@hf-01 shell]# cp if1.sh if2.sh
[root@hf-01 shell]# vim if2.sh
[root@hf-01 shell]# sh -x if1.sh
+ a=1
+ '[' 1 -gt 3 ']'
+ echo nook
nook
[root@hf-01 shell]# cat if2.sh
#! /bin/bash
a=1
if [ $a -gt 3 ]
then
echo OK
else
echo nook
fi
[root@hf-01 shell]#
if语句第三种格式
- 格式3:if …; then … ;elif …; then …; else …; fi
[root@hf-01 shell]# vim if3.sh
[root@hf-01 shell]# cat if3.sh
#! /bin/bash
a=6
if [ $a -lt 5 ]
then
echo "<5"
elif [ $a -gt 5 ] && [ $a -lt 9 ]
then
echo "5<a<9"
else
echo ">9"
fi
[root@hf-01 shell]# sh -x if3.sh
+ a=6
+ '[' 6 -lt 5 ']'
+ '[' 6 -gt 5 ']'
+ '[' 6 -lt 9 ']'
+ echo '5<a<9'
5<a<9
[root@hf-01 shell]#
- 逻辑判断表达式
- 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
20.6 文件目录属性判断
if文件目录属性判断
- if 判断文件、目录属性
- [ -f file ]判断是否是普通文件,且存在
- [ -d file ] 判断是否是目录,且存在
- [ -e file ] 判断文件或目录是否存在
- [ -r file ] 判断文件是否可读
- [ -w file ] 判断文件是否可写
- [ -x file ] 判断文件是否可执行
文件目录属性判断
- if 判断文件、目录属性
- [ -f file ]判断是否是普通文件,且存在
[root@hf-01 shell]# vim file1.sh
[root@hf-01 shell]# cat file1.sh
#! /bin/bash
f="/tmp/hanfeng"
if [ -f $f ]
then
echo $f exist
else
touch $f
fi
[root@hf-01 shell]# sh -x file1.sh 第一次执行,会创建该文件
+ f=/tmp/hanfeng
+ '[' -f /tmp/hanfeng ']'
+ touch /tmp/hanfeng
[root@hf-01 shell]# sh -x file1.sh 第二次执行,会提示该文件已存在
+ f=/tmp/hanfeng
+ '[' -f /tmp/hanfeng ']'
+ echo /tmp/hanfeng exist
/tmp/hanfeng exist
[root@hf-01 shell]#
- if 判断文件、目录属性
- [ -d file ] 判断是否是目录,且存在
[root@hf-01 shell]# vim file2.sh
[root@hf-01 shell]# cat !$
cat file2.sh
#! /bin/bash
f="/tmp/hanfeng"
if [ -d $f ]
then
echo $f exist
else
mkdir $f
fi
[root@hf-01 shell]# sh -x file2.sh
+ f=/tmp/hanfeng
+ '[' -d /tmp/hanfeng ']'
+ mkdir /tmp/hanfeng
[root@hf-01 shell]#
- if 判断文件、目录属性
- [ -e file ] 判断文件或目录是否存在
- 目录和文件都可以touch 的,touch的目的是 如果这个文件或目录不存在,它会创建这个文件,如果这个文件或目录存在了,在touch 就会更改这个文件的三个 time
[root@hf-01 shell]# vim file2.sh
[root@hf-01 shell]# sh -x file2.sh
+ f=/tmp/hanfeng
+ '[' -e /tmp/hanfeng ']'
+ echo /tmp/hanfeng exist
/tmp/hanfeng exist
[root@hf-01 shell]#
- if 判断文件、目录属性
- [ -r file ] 判断文件是否可读
[root@hf-01 shell]# cat file2.sh
#! /bin/bash
f="/tmp/hanfeng"
if [ -r $f ]
then
echo $f readable
fi
[root@hf-01 shell]# sh file2.sh 会看到文件可读的
/tmp/hanfeng readable
[root@hf-01 shell]#
- if 判断文件、目录属性
- [ -w file ] 判断文件是否可写
- 去判断是否刻度可写,就判断执行shell脚本的当前用户
[root@hf-01 shell]# cat file2.sh
#! /bin/bash
f="/tmp/hanfeng"
if [ -w $f ]
then
echo $f writeable
fi
[root@hf-01 shell]# sh file2.sh
/tmp/hanfeng writeable
[root@hf-01 shell]#
- if 判断文件、目录属性
- [ -x file ] 判断文件是否可执行
[root@hf-01 shell]# cat file2.sh
#! /bin/bash
f="/tmp/hanfeng"
if [ -x $f ]
then
echo $f exeable
fi
[root@hf-01 shell]# sh file2.sh
/tmp/hanfeng exeable
常用案例
- 并且 &&
f="/tmp/aminglinux"
[ -f $f ] && rm -f $f //前一条命令执行成功才会继续执行之后的命令
等同于下面的表达方式
if [ -f $f ]
then
rm -rf $f
fi
- 或者 ||
f="/tmp/aminglinux"
[ -f $f ] || touch $f //前面命令不成功时,执行后面的命令
if [ ! -f $f ] // “!”表示了如果这条命令不成功,就往下执行
then
touch $f
fi
20.7 if特殊用法
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@hf-01 shell]# vim file1.sh
[root@hf-01 shell]# cat !$
cat file1.sh
#! /bin/bash
n=`wc -l /tmp/lala`
if [ -z "$n" ]
then
echo error
exit
elif [ $n -gt 100 ]
then
echo djsjdd
fi
[root@hf-01 shell]# sh -x file1.sh
++ wc -l /tmp/lala
wc: /tmp/lala: 没有那个文件或目录
+ n=
+ '[' -z '' ']'
+ echo error
error
+ exit
[root@hf-01 shell]#
[root@hf-01 shell]# vim file1.sh
[root@hf-01 shell]# cat !$
cat file1.sh
#! /bin/bash
if [ ! -f /tmp/lala ]
then
echo "/tmp/lala not exit."
exit
fi
n=`wc -l /tmp/lala`
if [ -z "$n" ]
then
echo error
exit
elif [ $n -gt 100 ]
then
echo djsjdd
fi
[root@hf-01 shell]# sh file1.sh
/tmp/lala not exit.
[root@hf-01 shell]#
- if [ -n "$a" ] 表示当变量a的值不为空,或者说这个文件内容不为空
- -n 判断变量的时候,需要用""双引号引起来,若是文件的时候,则不需要用双引号引起来
[root@hf-01 shell]# if [ -n 01.sh ]; then echo ok; fi
ok
[root@hf-01 shell]# echo $b
[root@hf-01 shell]# if [ -n "$b" ]; then echo $b; else echo "b is null"; fi
b is null
[root@hf-01 shell]#
- 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@hf-01 shell]# grep -w 'zabbix' /etc/passwd
zabbix:x:998:995:Zabbix Monitoring System:/var/lib/zabbix:/sbin/nologin
[root@hf-01 shell]# if grep -wq 'zabbix' /etc/passwd; then echo "zabbix exist"; fi
zabbix exist
[root@hf-01 shell]#
-
if [ ! -e file ]; then 表示文件不存在时会怎么样
-
if (($a<1)); then …等同于 if [ $a -lt 1 ]; then…
-
[ ] 中不能使用<,>,==,!=,>=,<=这样的符号
- 一个等于号= 是赋值
20.8/20.9 case判断
case判断
- 格式 case
变量名 in
value1)
command
;;
value2)
command
;;
*)
commond
;;
esac
- 在case程序中,可以在条件中使用|,表示或的意思, 比如
2|3)
command
;;
shell脚本案例:
- 脚本目的是 输入一个数字,然后用脚本去判断这个数字的范围
[root@hf-01 shell]# read -p "dfd" z
dfdgb
[root@hf-01 shell]# read -p "dfd: " z
dfd: fgdg
[root@hf-01 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 脚本中,通常会根据上一个命令的 $? 值来进行一些流程控制。