shell學(xué)習(xí)筆記之六(測(cè)試和判斷)
來(lái)源:程序員人生 發(fā)布時(shí)間:2015-05-15 08:31:04 閱讀次數(shù):3224次
測(cè)試和判斷
測(cè)試
利用命令履行后的$?來(lái)判斷命令是不是履行正常。$?==0 ? 正常:毛病
測(cè)試結(jié)構(gòu):
1、test expression
2、[ expression ] #注意表達(dá)式兩側(cè)的空格
方式2增加了代碼的可讀性,且更容易與if,case,while這些條件判斷的關(guān)鍵字聯(lián)用。
文件測(cè)試:
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 文件存在且為可寫(xiě)文件
-r 文件存在且為可讀文件
-l 文件存在且為連接文件
-p ...管道文件
-S ...socket文件
-s 大小不為0
FILE1 nt FILE2 FILE1比FILE2新返回真
FILE1 ot FILE2 FILE1比FILE2舊返回真
例:測(cè)試文件的讀,寫(xiě),履行屬性
#!/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和[]之間要有空格
字符串測(cè)試:
字符串比較(字典序)主要有大于,小于,等于,不等于,空,不空
> < = != -z -n
例:
str=""
test -z "$str"
echo $? #0
test -n "$str"
echo $? #1
test -n $str
echo $? #0,最好加上雙引號(hào)("")
str="hello"
[ -z "$str" ]
echo $? #1
[ -n "$str" ]
echo $? #0
str1="123"
str2="234"
[ "$str1" = "$str2"] #test $str1 = $str2,注意空格,否則1直返回0
echo $? #1
[ "$str1" != "$str2" ]
echo $?
[ "$str1" > "$str2" ]
echo $?
[ "$str1" < "$str2" ]
echo $?
[[ "$str1" > "$str2" ]] #不使用轉(zhuǎn)義字符
echo $?
整數(shù)比較
-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
#測(cè)試相等
[ $num1 -eq $num2]
echo $?
test $num3 -eq $num2
echo $?
注意:1定要注意空格,否則得到的有可能毛病
邏輯測(cè)試符和邏輯運(yùn)算符
邏輯與,邏輯或,邏輯非
-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 ]
注意兩種不同的寫(xiě)法
判斷
if語(yǔ)句
if expression;then
command1
command2
...
fi
例:判斷學(xué)生成績(jī)
#!/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
學(xué)太高級(jí)語(yǔ)言以后,其實(shí)就是學(xué)習(xí)shell的語(yǔ)法和編程特點(diǎn),高級(jí)語(yǔ)言重視于大型項(xiàng)目,而shell重視于工作的便利,讓工作自動(dòng)化
if/else語(yǔ)句
if expression ;then
command
else
command
fi
例:判斷1個(gè)文件是不是存在
#!/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語(yǔ)句
例:修改學(xué)生成績(jī)腳本
#!/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
相當(dāng)于switch...case
結(jié)構(gòu):
case VAR in
var1) command ;;
var2) command ;;
var3) command ;;
...
*) command ;;
注意:case的var1、var2、var3等這些值只能是常量或正則表達(dá)式,還能是正則啊,不錯(cuò)
例:檢測(cè)當(dāng)前系統(tǒng)的類(lèi)型
#!/bin/bash
type=`uname -s` #print the kernel name
case $type in
L*)
echo "Linux"
;;
SunOS)
echo "SunOS"
;;
Darwin)
echo "Darwin"
;;
*)
echo "other"
;;
esac
檢測(cè)用戶(hù)輸入中是不是含有大寫(xiě)字母,小寫(xiě)字母或數(shù)字
#!/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
生活不易,碼農(nóng)辛苦
如果您覺(jué)得本網(wǎng)站對(duì)您的學(xué)習(xí)有所幫助,可以手機(jī)掃描二維碼進(jìn)行捐贈(zèng)