Shell脚本中的逻辑判断
格式1:if 条件 ; then 语句; fi (常用)
#以命令的方式表达
[[email protected] ~]# for i in `seq 1 5`; do echo $i; done
1
2
3
4
5
[[email protected] ~]# for i in `seq 1 5`
> do
> echo $i
> done
1
2
3
4
5
[[email protected] ~]# a=5
[[email protected] ~]# if [ $a -gt 3 ]
> then
> echo ok
> fi
ok
[[email protected] ~]# if [ $a -gt 3 ]; then echo ok; fi
ok
#脚本执行
[[email protected] ~]# cd shell/
[[email protected] shell]# vi if1.sh
[[email protected] shell]# cat if1.sh
#!/bin/bash
a=5
if [ $a -gt 3 ]
then
echo ok
fi
[[email protected] shell]# sh if1.sh
ok
格式2:if 条件; then 语句; else 语句; fi
[[email protected] shell]# cp if1.sh if2.sh
[[email protected] shell]# vi if2.sh
[[email protected] shell]# cat if2.sh
#!/bin/bash
a=1
if [ $a -gt 3 ]
then
echo ok
else
echo nook
fi
[[email protected] shell]# sh -x if2.sh
+ a=1
+ ‘[‘ 1 -gt 3 ‘]‘
+ echo nook
nook
[[email protected] shell]# sh if2.sh
nook
格式3:if …; then … ;elif …; then …; else …; fi
[[email protected] shell]# cp if2.sh if3.sh
[[email protected] shell]# vi if3.sh
[[email protected] shell]# cat if3.sh
#!/bin/bash
a=3
if [ $a -gt 4 ]
then
echo ">1"
elif [ $a -gt 6 ]
then
echo "<6 && >1"
else
echo nook
fi
[[email protected] shell]# sh -x if3.sh
+ a=3
+ ‘[‘ 3 -gt 4 ‘]‘
+ ‘[‘ 3 -gt 6 ‘]‘
+ echo nook
nook
[[email protected] shell]# sh if3.sh
nook
逻辑判断表达式:if [ $a -gt $b ]; if [ $a -lt 5 ]; if [ $b -eq 10 ]等 -gt (>); -lt(=); -le(<=);-eq(==); -ne(!=) 注意到处都是空格
可以使用 && || 结合多个条件
if [ $a -gt 5 ] && [ $a -lt 10 ]; then
if [ $b -gt 5 ] || [ $b -lt 3 ]; then
文件目录属性判断
[ -f file ]判断是否是普通文件,且存在
[ -d file ] 判断是否是目录,且存在
[ -e file ] 判断文件或目录是否存在
[ -r file ] 判断文件是否可读
[ -w file ] 判断文件是否可写
[ -x file ] 判断文件是否可执行
[[email protected] shell]# vi filel.sh
[[email protected] shell]# cat filel.sh
#!/bin/bash
f="/tmp/aminglinux"
if [ -f $f ]
then
echo $f exist
else
touch $f
fi
[[email protected] shell]# sh -x filel.sh
+ f=/tmp/aminglinux
+ ‘[‘ -f /tmp/aminglinux ‘]‘
+ touch /tmp/aminglinux
[[email protected] shell]# sh -x filel.sh
+ f=/tmp/aminglinux
+ ‘[‘ -f /tmp/aminglinux ‘]‘
+ echo /tmp/aminglinux exist
/tmp/aminglinux exist
[[email protected] shell]# cp filel.sh filel2.sh
[[email protected] shell]# vi filel2.sh
[[email protected] shell]# cat filel2.sh
#!/bin/bash
f="/tmp/aminglinux"
if [ -d $f ]
then
echo $f exist
else
touch $f
fi
[[email protected] shell]# sh -x filel2.sh
+ f=/tmp/aminglinux
+ ‘[‘ -d /tmp/aminglinux ‘]‘
+ touch /tmp/aminglinux
[[email protected] shell]# vi filel2.sh
[[email protected] shell]# cat filel2.sh
#!/bin/bash
f="/tmp/aminglinux"
if [ -e $f ]
then
echo $f exist
else
touch $f
fi
[[email protected] shell]# sh -x filel2.sh
+ f=/tmp/aminglinux
+ ‘[‘ -e /tmp/aminglinux ‘]‘
+ echo /tmp/aminglinux exist
/tmp/aminglinux exist
[[email protected] shell]# vi filel2.sh
[[email protected] shell]# cat filel2.sh
#!/bin/bash
f="/tmp/aminglinux"
if [ -r $f ]
then
echo $f readable
fi
[[email protected] shell]# sh -x filel2.sh
+ f=/tmp/aminglinux
+ ‘[‘ -r /tmp/aminglinux ‘]‘
+ echo /tmp/aminglinux readable
/tmp/aminglinux readable
[[email protected] shell]# vi filel2.sh
[[email protected] shell]# cat filel2.sh
#!/bin/bash
f="/tmp/aminglinux"
if [ -w $f ]
then
echo $f writeable
fi
[[email protected] shell]# sh -x filel2.sh
+ f=/tmp/aminglinux
+ ‘[‘ -w /tmp/aminglinux ‘]‘
+ echo /tmp/aminglinux writeable
/tmp/aminglinux writeable
[email protected] shell]# ls -l /tmp/aminglinux
-rw-r--r-- 1 root root 0 Feb 3 14:52 /tmp/aminglinux
[[email protected] shell]# vi filel2.sh
[[email protected] shell]# cat filel2.sh
#!/bin/bash
f="/tmp/aminglinux"
if [ -x $f ]
then
echo $f exeable
fi
[[email protected] shell]# sh filel2.sh //因为不可执行,所以没有输出
if特殊用法
if [ -z "$a" ]??这个表示当变量a的值为空时会怎么样
[[email protected] shell]# vi if4.sh
[[email protected] shell]# sh -x if4.sh
++ wc -l /tmp/lalal
wc: /tmp/lalal: 没有那个文件或目录
+ n=
+ ‘[‘ -gt 100 ‘]‘
if4.sh: 第 3 行:[: -gt: 期待一元表达式
[[email protected] shell]# vi if4.sh
[[email protected] shell]# cat if4.sh
#!/bin/bash
n=`wc -l /tmp/lalal`
if [ -z "$n" ]
then
echo error
exit
elif [ $n -gt 100 ]
then
echo aladafaf
fi
[[email protected] shell]# sh -x if4.sh
++ wc -l /tmp/lalal
wc: /tmp/lalal: 没有那个文件或目录
+ n=
+ ‘[‘ -z ‘‘ ‘]‘
+ echo error
error
+ exit
[[email protected] shell]# vi if4.sh
[[email protected] shell]# cat if4.sh
#!/bin/bash
if [ ! -f /tmp/lalal ]
then
echo "/tmp/lalal not exist."
exit
fi
n=`wc -l /tmp/lalal`
if [ -z "$n" ]
then
echo error
exit
elif [ $n -gt 100 ]
then
echo alsdflljk
fi
[[email protected] shell]# sh -x if4.sh
+ ‘[‘ ‘!‘ -f /tmp/lalal ‘]‘
+ echo ‘/tmp/lalal not exist.‘
/tmp/lalal not exist.
+ exit
[[email protected] shell]# sh if4.sh
/tmp/lalal not exist.
if [ -n "$a" ] 表示当变量a的值不为空
[[email protected] shell]# ls
01.sh filel2.sh filel.sh for1.sh if1.sh if2.sh if3.sh if4.sh
[[email protected] shell]# if [ -n 01.sh ]; then echo ok; fi
ok
[[email protected] 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‘的行时会怎么样
if [ ! -e file ]; then 表示文件不存在时会怎么样
if (($a<1)); then …等同于 if [ $a -lt 1 ]; then…
[ ] 中不能使用,==,!=,>=,<=这样的符号
[[email protected] shell]# grep -w ‘user1‘ /etc/passwd
user1:x:1002:1002::/home/user1:/bin/bash
[[email protected] shell]# if grep -w ‘user1‘ /etc/passwd; then echo "user1 exist"; fi
user1:x:1002:1002::/home/user1:/bin/bash
user1 exist
[[email protected] shell]# if grep -wq ‘user1‘ /etc/passwd; then echo "user1 exist"; fi
user1 exist
[[email protected] shell]# if ! grep -wq ‘user1‘ /etc/passwd; then useradd user1; fi
[[email protected] shell]#
case判断
可以查看vi /etc/init.d/network文件的case判断例子
case判断脚本格式
shell脚本案例
[[email protected] shell]# vi case.sh
[[email protected] shell]# cat case.sh
#!/bin/bash
read -p "Please input a number: " n
if [ -z "$n" ]
then
echo "Please input a number."
exit 1
fi
n1=`echo $n|sed ‘s/[0-9]//g‘`
if [ -n "$n1" ]
then
echo "Please input a number."
exit 1
#elif [ $n -lt 0 ] || [ $n -gt 100 ]
#then
# echo "The number range is 0-100."
# 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 number range is 0-100."
;;
esac
[[email protected] shell]# sh case.sh
Please input a number: 101
The number range is 0-100.
[[email protected] shell]# sh -x case.sh
+ read -p ‘Please input a number: ‘ n
Please input a number: 101
+ ‘[‘ -z 101 ‘]‘
++ echo 101
++ sed ‘s/[0-9]//g‘
+ n1=
+ ‘[‘ -n ‘‘ ‘]‘
+ ‘[‘ 101 -lt 60 ‘]‘
+ ‘[‘ 101 -ge 60 ‘]‘
+ ‘[‘ 101 -lt 80 ‘]‘
+ ‘[‘ 101 -ge 80 ‘]‘
+ ‘[‘ 101 -lt 90 ‘]‘
+ ‘[‘ 101 -ge 90 ‘]‘
+ ‘[‘ 101 -le 100 ‘]‘
+ tag=0
+ case $tag in
+ echo ‘The number range is 0-100.‘
The number range is 0-100.