您的位置:首页 > 运维架构 > Shell

shell学习笔记之六(测试和判断)

2015-04-30 16:38 211 查看
测试和判断
测试
利用命令执行后的$?来判断命令是否执行正常。$?==0 ? 正常:错误
测试结构:
1、test  expression
2、[ expression ]    #注意表达式两侧的空格
方式2增加了代码的可读性,且更容易与if,case,while这些条件判断的关键字联用。
文件测试:
1、test file_operator FILE
2、[ file_operator FILE ]
例:
test -e /var/log/message
echo $?                #文件存在返回0,不存在返回非0值
[ -e /var/log/message ]
echo $?
文件比较符
-b  文件存在且是块文件
-c  文件存在且是字符设备
-e  文件或目录是否存在
-d  文件存在且是目录
-f  文件存在且是普通文件
-x  文件存在且是可执行文件
-w  文件存在且为可写文件
-r  文件存在且为可读文件
-l  文件存在且为连接文件
-p  ...管道文件
-S  ...socket文件
-s  大小不为0
FILE1 nt FILE2  FILE1比FILE2新返回真
FILE1 ot FILE2  FILE1比FILE2旧返回真
例:测试文件的读,写,执行属性
#!/bin/bash
read -p "input a filename which you want to test:" filename
if [ ! -e "$filename" ];then
echo "The file doesn't exist."
exit 1
fi
if [ -w "$filename" ];then
echo "The file is writeable."
fi
if [ -r "$filename" ];then
echo "The file is readable."
fi
if [ -x "$filename" ];then
echo "The file is executable"
fi
注意:
if [ expression ] 中if和[]之间要有空格
字符串测试:
字符串比较(字典序)主要有大于,小于,等于,不等于,空,不空
>     <     =     !=    -z   -n
例:
str=""
test -z "$str"
echo $?        #0

test -n "$str"
echo $?        #1

test -n $str
echo $?        #0,最好加上双引号("")

str="hello"
[ -z "$str" ]
echo $?        #1

[ -n "$str" ]
echo $?        #0

str1="123"
str2="234"
[ "$str1" = "$str2"]  #test $str1 = $str2,注意空格,否则一直返回0
echo $?        #1

[ "$str1" != "$str2" ]
echo $?

[ "$str1" \> "$str2" ]
echo $?

[ "$str1" \< "$str2" ]
echo $?

[[ "$str1" > "$str2" ]]   #不使用转义字符
echo $?
整数比较
-eq =
-lt <
-gt >
-le <=
-ge >=
-ne !=
1、test num1 num_operator num2
2、[ num1 num_operator num2 ]
例:
num1=10
num2=10
num3=9
num4=11
#测试相等
[ $num1 -eq $num2]
echo $?
test $num3 -eq $num2
echo $?
注意:一定要注意空格,否则得到的有可能错误
逻辑测试符和逻辑运算符
逻辑与,逻辑或,逻辑非
-a     -o     !
非:!expression
与:expression1 -a expression2
或:expression1 -o expression2
&&     ||     !
[ -e /var/log/message ] && [ -e /var/log/message01 ]
=[ -e /var/log/message -a -e /var/log/message01 ]

[ -e /var/log/message ] || [ -e /var/log/message01 ]
=[ -e /var/log/message -o -e /var/log/message01 ]
注意两种不同的写法
判断
if语句
if expression;then
command1
command2
...
fi
例:判断学生成绩
#!/bin/bash
echo -n "input a score of a student:"   #-n的意思是不换行
read score
if [ $score -lt 60 ];then
echo "D"
fi
if [ $score -lt 80 -a $score -ge 60 ];then
echo "C"
fi
if [ $score -lt 90 -a $score -ge 80 ];then
echo "B"
fi
if [ $score -le 100 -a $score -ge 90 ];then
echo "A"
fi
if [ $score -gt 100 ];then
echo "the number is invalid"
fi
学过高级语言之后,其实就是学习shell的语法和编程特色,高级语言注重于大型项目,而shell注重于工作的便利,让工作自动化
if/else语句
if expression ;then
command
else
command
fi
例:判断一个文件是否存在
#!/bin/bash
echo -n "input a filename:"
read filename
if [ -e $filename ];then
echo "file exists."
else
echo "file doesn't exists."
fi
if/elif/else
多重的if/else语句
例:修改学生成绩脚本
#!/bin/bash
echo -n "input a score of a student:"
read score
if [ $score -lt 60 ];then
echo "D"
elif [ $score -lt 80 ];then
echo "C"
elif [ $score -lt 90 ];then
echo "B"
elif [ $score -le 100 ];then
echo "A"
elif [ $score -gt 100 ];then
echo "the number is invalid"
fi
case
相当于switch...case
结构:
case VAR in
var1) command ;;
var2) command ;;
var3) command ;;
...
*)    command ;;
注意:case的var1、var2、var3等这些值只能是常量或正则表达式,还能是正则啊,不错
例:检测当前系统的类型
#!/bin/bash
type=`uname -s` #print the kernel name
case $type in
L*)
echo "Linux"
;;
SunOS)
echo "SunOS"
;;
Darwin)
echo "Darwin"
;;
*)
echo "other"
;;
esac
检测用户输入中是否含有大写字母,小写字母或者数字
#!/bin/bash
echo -n "Input a string: "
read str
case $str in
*[[:lower:]]*)
echo "The string contains lower letters."
;;
*[[:upper:]]*)
echo "The string contains upper letters."
;;
*[[:digit:]]*)
echo "The string contains digit."
;;
*)
echo "other"
;;
esac
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: