20.5 shell脚本中的逻辑判断
格式1:if 条件 ; then 语句; fi
[[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
改成shell脚本
[[email protected] script]# vim if1.sh
#!/bin/bash
a=5
[ $a -gt 3 ]
then
echo ok
fi
调试;
[[email protected] script]# sh -x !$
sh -x if1.sh
+ a=5
+ ‘[‘ 5 -gt 3 ‘]‘
+ echo ok
ok
[[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
格式2:if 条件; then 语句; else 语句; fi
[[email protected] script]# vim if2.sh
#!/bin/bash
a=3
if [ $a -gt 3 ]
then
echo ok
else
echo nook!
fi
[[email protected] script]# sh -x !$
sh -x if2.sh
+ a=3
+ ‘[‘ 3 -gt 3 ‘]‘
+ echo ‘nook!‘
nook!
格式3:if …; then … ;elif …; then …; else …; fi
[[email protected] script]# vim if3.sh
#!/bin/bash
a=3
if [ $a -gt 4 ]
then
echo ">4"
elif [ $a -lt 6 ]
then
echo " <6 && >1"
else
echo nook!
fi
[[email protected] script]# sh -x !$
sh -x if3.sh
+ a=3
+ ‘[‘ 3 -gt 4 ‘]‘
+ ‘[‘ 3 -lt 6 ‘]‘
+ echo ‘ <6 && >1‘
<6 && >1
逻辑判断表达式:
if [ $a -gt $b ]; if [ $a -lt 5 ]; if [ $b -eq 10 ]等
-gt(>); -lt(<); -ge(>=); -le(<=);-eq(==); -ne(!=) 注意到处都是空格
参数 | 说明 |
---|---|
-eq | 等于则为真 |
-ne | 不等于则为真 |
-gt | 大于则为真 |
-ge | 大于等于则为真 |
-lt | 小于则为真 |
-le | 小于等于则为真 |
[[email protected] script]# a=3
[[email protected] script]# if (($a>1));then echo ok;fi
ok
可以使用 && || 结合多个条件
if [ $a -gt 5 ] && [ $a -lt 10 ]; then
if [ $b -gt 5 ] || [ $b -lt 3 ]; then
20.6 文件目录属性判断
[ -f file ]判断是否是普通文件,且存在
[[email protected] script]# vim test1.sh
#!/bin/bash
f=‘/tmp/aiker‘
if [ -f $f ]
then
echo $f exist
else
touch $f
fi
[[email protected] script]# sh -x test1.sh
+ f=/tmp/aiker
+ ‘[‘ -f /tmp/aiker ‘]‘
+ touch /tmp/aiker
[[email protected] script]# sh -x test1.sh
+ f=/tmp/aiker
+ ‘[‘ -f /tmp/aiker ‘]‘
+ echo /tmp/aiker exist
/tmp/aiker exist
[ -d file ] 判断是否是目录,且存在
[[email protected] script]# vim testd.sh
#!/bin/bash
f=‘/tmp/aiker‘
if [ -d $f ]
then
echo $f exist
else
touch $f
fi
[[email protected] script]# sh -x !$
sh -x testd.sh
+ f=/tmp/aiker
+ ‘[‘ -d /tmp/aiker ‘]‘
+ touch /tmp/aiker
[ -e file ] 判断文件或目录是否存在
#!/bin/bash
f=‘/tmp/aiker‘
if [ -e $f ]
then
echo $f exist
else
touch $f
fi
[[email protected] script]# sh -x !$
sh -x teste.sh
+ f=/tmp/aiker
+ ‘[‘ -e /tmp/aiker ‘]‘
+ echo /tmp/aiker exist
/tmp/aiker exist
[ -r file ] 判断文件是否可读
[[email protected] script]# vim testr.sh
#!/bin/bash
f=‘/tmp/aiker‘
if [ -r $f ]
then
echo $f is readable
else
echo $f is unreadable
fi
[[email protected] script]# sh -x !$
sh -x testr.sh
+ f=/tmp/aiker
+ ‘[‘ -r /tmp/aiker ‘]‘
+ echo /tmp/aiker is readable
/tmp/aiker is readable
[ -w file ] 判断文件是否可写
[[email protected] script]# vim testw.sh
#!/bin/bash
f=‘/tmp/aiker‘
if [ -w $f ]
then
echo $f is writeable
else
echo $f is not writeable
fi
[[email protected] script]# sh -x !$
sh -x testw.sh
+ f=/tmp/aiker
+ ‘[‘ -w /tmp/aiker ‘]‘
+ echo /tmp/aiker is writeable
/tmp/aiker is writeable
[ -x file ] 判断文件是否可执行
[[email protected] script]# vim testx.sh
#!/bin/bash
f=‘/tmp/aiker‘
if [ -x $f ]
then
echo $f is exe file
else
echo $f is not exe file
fi
[[email protected] script]# sh -x !$
sh -x testx.sh
+ f=/tmp/aiker
+ ‘[‘ -x /tmp/aiker ‘]‘
+ echo /tmp/aiker is not exe file
/tmp/aiker is not exe file
[[email protected] script]# cat !$
cat testf2.sh
#!/bin/bash
f=‘/tmp/aiker‘
[ -f $f ] && rm -f $f ##前面的执行成功就执行后面的
[ -f $f ] || touch $f ##前面的不成功才执行后面的
if [ -f $f ]
then
rm -f $f
else
touch $f
fi
if [ ! -f $f ] ##!是取非,!f不存在
then
touch $f
fi
[[email protected] script]# sh -x !$
sh -x testf2.sh
+ f=/tmp/aiker
+ ‘[‘ -f /tmp/aiker ‘]‘
+ rm -f /tmp/aiker
+ ‘[‘ -f /tmp/aiker ‘]‘
+ touch /tmp/aiker
+ ‘[‘ -f /tmp/aiker ‘]‘
+ rm -f /tmp/aiker
+ ‘[‘ ‘!‘ -f /tmp/aiker ‘]‘
+ touch /tmp/aiker
<table>
<tbody>
<tr>
<td width="100">测试的标志</font></td><td>代表意义</font></td>
</tr>
<tr>
<td colspan="2">1. 关於某个档名的『文件类型』判断,如 test -e filename 表示存在否</font></td>
</tr>
<tr>
<td>-e</td><td>该『档名』是否存在?(常用)</td>
</tr>
<tr>
<td>-f</td><td>该『档名』是否存在且为文件(file)?(常用)</td>
</tr>
<tr>
<td>-d</td><td>该『档名』是否存在且为目录(directory)?(常用)</td>
</tr>
<tr>
<td>-b</td><td>该『档名』是否存在且为一个 block device 装置?</td>
</tr>
<tr>
<td>-c</td><td>该『档名』是否存在且为一个 character device 装置?</td>
</tr>
<tr>
<td>-S</td><td>该『档名』是否存在且为一个 Socket 文件?</td>
</tr>
<tr>
<td>-p</td><td>该『档名』是否存在且为一个 FIFO (pipe) 文件?</td>
</tr>
<tr>
<td>-L</td><td>该『档名』是否存在且为一个连结档?</td>
</tr>
<tr>
<td colspan="2">2. 关於文件的权限侦测,如 test -r filename 表示可读否 (但 root 权限常有例外)</font></td>
</tr>
<tr>
<td>-r</td><td>侦测该档名是否存在且具有『可读』的权限?</td>
</tr>
<tr>
<td>-w</td><td>侦测该档名是否存在且具有『可写』的权限?</td>
</tr>
<tr>
<td>-x</td><td>侦测该档名是否存在且具有『可运行』的权限?</td>
</tr>
<tr>
<td>-u</td><td>侦测该档名是否存在且具有『SUID』的属性?</td>
</tr>
<tr>
<td>-g</td><td>侦测该档名是否存在且具有『SGID』的属性?</td>
</tr>
<tr>
<td>-k</td><td>侦测该档名是否存在且具有『Sticky bit』的属性?</td>
</tr>
<tr>
<td>-s</td><td>侦测该档名是否存在且为『非空白文件』?</td>
</tr>
<tr>
<td colspan="2">3. 两个文件之间的比较,如: test file1 -nt file2</font></td>
</tr>
<tr>
<td>-nt</td><td>(newer than)判断 file1 是否比 file2 新</td>
</tr>
<tr>
<td>-ot</td><td>(older than)判断 file1 是否比 file2 旧</td>
</tr>
<tr>
<td>-ef</td><td>判断 file1 与 file2 是否为同一文件,可用在判断 hard link 的判定上。
主要意义在判定,两个文件是否均指向同一个 inode 哩!</td>
</tr>
<tr>
<td colspan="2">4. 关於两个整数之间的判定,例如 test n1 -eq n2</font></td>
</tr>
<tr>
<td>-eq</td><td>两数值相等 (equal)</td>
</tr>
<tr>
<td>-ne</td><td>两数值不等 (not equal)</td>
</tr>
<tr>
<td>-gt</td><td>n1 大於 n2 (greater than)</td>
</tr>
<tr>
<td>-lt</td><td>n1 小於 n2 (less than)</td>
</tr>
<tr>
<td>-ge</td><td>n1 大於等於 n2 (greater than or equal)</td>
</tr>
<tr>
<td>-le</td><td>n1 小於等於 n2 (less than or equal)</td>
</tr>
<tr>
<td colspan="2">5. 判定字串的数据</font></td>
</tr>
<tr>
<td>test -z string</td><td>判定字串是否为 0 ?若 string 为空字串,则为 true</td>
</tr>
<tr>
<td>test -n string</td><td>判定字串是否非为 0 ?若 string 为空字串,则为 false。<br>注: -n 亦可省略</td>
</tr>
<tr>
<td>test str1 = str2</td><td>判定 str1 是否等於 str2 ,若相等,则回传 true</td>
</tr>
<tr>
<td>test str1 != str2</td><td>判定 str1 是否不等於 str2 ,若相等,则回传 false</td>
</tr>
<tr>
<td colspan="2">6. 多重条件判定,例如: test -r filename -a -x filename</font></td>
</tr>
<tr>
<td>-a</td><td>(and)两状况同时成立!例如 test -r file -a -x file,则 file 同时具有 r 与
x 权限时,才回传 true。</td>
</tr>
<tr>
<td>-o</td><td>(or)两状况任何一个成立!例如 test -r file -o -x file,则 file 具有 r 或
x 权限时,就可回传 true。</td>
</tr>
<tr>
<td>!</td><td>反相状态,如 test ! -x file ,当 file 不具有 x 时,回传 true</td>
</tr>
</tbody>
</table>
if-then语句,格式如下:
if command_1
then command_2
command_3
fi command_4
在if-then语句中使用了命令返回码$?,即当command_1执行成功时才执行command_2和 command_3,而command_4总是执行 ,例:
在备份成功时删除原始文件:
if l s - a | cpi o - o > / dev/ rm t / 0h
then
rm - rf *
fi
测试条件之否定,使用!
测试条件之逻辑运算
-a And
-o Or
例:
test -r empty -a -s empty
进行test测试的标准方法
因为test命令在 shell编程中占有很重要的地位,为了使shell能 同其他编程语言一样 便于阅读和组织, Bourne Shell在使用test 测试时使用了另一种方法:用方括号将整个 test测试括起来:
例:
int1=4
[ $int1 -gt 2 ]
echo $?
0
20.7 if特殊用法
if [ -z "$a" ] 这个表示当变量a的值为空时会怎么样
[[email protected] ~]# vim !$
vim testz0.sh
#!/bin/bash
n=`wc -l /tmp/aikerv | awk ‘{print $1}‘`
if [ -z "$n" ]
then
echo error
exit 1
else
if [ $n -gt 100 ]
then
echo $n over 100 hang.
fi
fi
[[email protected] ~]# sh -x testz0.sh
++ awk ‘{print $1}‘
++ wc -l /tmp/aikerv
+ n=1
+ ‘[‘ -z 1 ‘]‘
+ ‘[‘ 1 -gt 100 ‘]‘
或者
[[email protected] ~]# vim testz.sh
#!/bin/bash
n=`wc -l /tmp/aikerv | awk ‘{print $1}‘`
if [ -z "$n" ]
then
echo error
exit 1
elif [ $n -gt 100 ]
then
echo $n over 100 hang.
fi
[[email protected] ~]# sh -x testz.sh
++ awk ‘{print $1}‘
++ wc -l /tmp/aikerv
+ n=1
+ ‘[‘ -z 1 ‘]‘
+ ‘[‘ 1 -gt 100 ‘]‘
if [ -n "$a" ] 表示当变量a的值不为空
[[email protected] ~]# if [ -n autosshd ]; then echo OK; fi #判断文件内容不为空,如果为文件,不用双引号引起
OK
[[email protected] ~]# 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‘的行时会怎么样
[[email protected] ~]# grep -w ‘operator‘ /etc/passwd #-w精确过滤
operator:x:11:0:operator:/root:/sbin/nologin
[[email protected] ~]# if grep -w ‘operator‘ /etc/passwd; then echo "operator is exist."; fi ##grep输出了结果
operator:x:11:0:operator:/root:/sbin/nologin
operator is exist.
[[email protected] ~]# if grep -wq ‘operator‘ /etc/passwd; then echo "operator is exist."; fi ##-q不输出过程
operator is exist.
if ! grep -wq ‘operator‘ /etc/passwd; then echo "operator is not exist" && useradd operator; fi ##不存在operator
if [ ! -e file ]; then 表示文件不存在时会怎么样
[[email protected] script]# vim testne.sh ##!取反
#!/bin/bash
f=‘/tmp/aiker‘
if [ ! -e $f ]
then
echo $f is not exist
touch $f
else
echo $f is exist
fi
[[email protected] script]# sh -x testne.sh
+ f=/tmp/aiker
+ ‘[‘ ‘!‘ -e /tmp/aiker ‘]‘
+ echo /tmp/aiker is exist
/tmp/aiker is exist
if (($a<1)); then …等同于 if [ $a -lt 1 ]; then…
[[email protected] script]# vim testlt.sh
#!/bin/bash
a=6
if (($a<5))
then
echo $a ‘is < 5‘
elif [ $a -lt 10 ]
then
b=$(($a+1))
echo $b
fi
[[email protected] script]# sh -x testlt.sh
+ a=6
+ (( 6<5 ))
+ ‘[‘ 6 -lt 10 ‘]‘
+ b=7
+ echo 7
7
[ ] 中不能使用<,>,==,!=,>=,<=这样的符号,(())可以使用,[[]]可以使用,可以用[[]]代替[]
if嵌套及elif结构
if command
then command
else
if command
then command
else
if command
then command
fi
fi
fi
改进:使用elif结构
if command
then command
elif command
then command
elif command
then command
fi
20.8/20.9 case判断
格式
case 变量名 in
value1)
command
;;
value2)
command
;;
*)
commond
;;
esac
在case程序中,可以在条件中使用|,表示或的意思, 比如
2|3)
command
;;
shell脚本案例
#!/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" ] ##判断是否为空,如果不为空就提示输入数字,注意-n 变量需要“”
then
echo "Please input a number."
exit 1
fi
if [ $n -lt 60 ] && [ $n -ge 0 ]
then
tag=1 ##tag打标记
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 "is bad"
;;
2)
echo "ok"
;;
3)
echo "good"
;;
4)
echo "very good"
;;
*)
echo "The number range is 0-100."
;;
esac
[[email protected] script]# cat !$
cat case0.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
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 "is bad"
;;
2)
echo "ok"
;;
3)
echo "good"
;;
4)
echo "very good"
;;
*)
echo "The number range is 0-100."
;;
esac
[[email protected] script]# sh -x !$
sh -x case0.sh
+ read -p ‘Please input a number: ‘ n
Please input a number: 49
+ ‘[‘ -z 49 ‘]‘
++ sed ‘s/[0-9]//g‘
++ echo 49
+ n1=
+ ‘[‘ -n ‘‘ ‘]‘
+ ‘[‘ 49 -lt 60 ‘]‘
+ ‘[‘ 49 -ge 0 ‘]‘
+ tag=1
+ case $tag in
+ echo ‘is bad‘
is bad
[[email protected] script]# sh -x case0.sh
+ read -p ‘Please input a number: ‘ n
Please input a number: 6+^H0
+ ‘[‘ -z $‘6+\b0‘ ‘]‘
++ sed ‘s/[0-9]//g‘
++ echo $‘6+\b0‘
+ n1=$‘+\b‘
+ ‘[‘ -n $‘+\b‘ ‘]‘
+ echo ‘Please input a number.‘
Please input a number.
+ exit 1
[[email protected] script]# sh -x case0.sh
+ read -p ‘Please input a number: ‘ n
Please input a number: 60
+ ‘[‘ -z 60 ‘]‘
++ sed ‘s/[0-9]//g‘
++ echo 60
+ n1=
+ ‘[‘ -n ‘‘ ‘]‘
+ ‘[‘ 60 -lt 60 ‘]‘
+ ‘[‘ 60 -ge 60 ‘]‘
+ ‘[‘ 60 -lt 80 ‘]‘
+ tag=2
+ case $tag in
+ echo ok
ok
[[email protected] script]# sh -x case0.sh
+ read -p ‘Please input a number: ‘ n
Please input a number: 500
+ ‘[‘ -z 500 ‘]‘
++ sed ‘s/[0-9]//g‘
++ echo 500
+ n1=
+ ‘[‘ -n ‘‘ ‘]‘
+ ‘[‘ 500 -lt 60 ‘]‘
+ ‘[‘ 500 -ge 60 ‘]‘
+ ‘[‘ 500 -lt 80 ‘]‘
+ ‘[‘ 500 -ge 80 ‘]‘
+ ‘[‘ 500 -lt 90 ‘]‘
+ ‘[‘ 500 -ge 90 ‘]‘
+ ‘[‘ 500 -le 100 ‘]‘
+ tag=0
+ case $tag in
+ echo ‘The number range is 0-100.‘
The number range is 0-100.
case结构 :
case value in
pattern1)
command
command;;
pattern2)
command
command;;
...
patternn)
command
esac
case语句只执行第一个匹配模式,
例:
case "$CHOICE" in
1|R) echo "Restore";;
2|B) echo "Backup";;
3|U) echo "Unload";;
*) echo "Sorry $CHOICE is not a valid choice
exit 1
esac
原文地址:http://blog.51cto.com/235571/2139516