標籤:linux;shell指令碼
第二章shell指令碼(二)
Test檔案測試的常見選項有
-d:測試是否為目錄(directory)
-e:測試目錄或檔案是否存在(exist)
-f:測試是否為檔案(file)
-r:測試目前使用者是否有許可權讀取(read)
-w:測試目前使用者是否有寫入許可權(write)
-x:測試是否設定有可執行許可權(excute)
Test常用表達示有兩種
test 表達示 [表達示]
常用的表達示為後者因為更加貼近編程習慣,如下
[[email protected] ~]# [ -d /media/Server/ ] ; echo $? #配合$?查看,傳回值為非0說明沒有這個目錄
1
[[email protected] ~]# ld /media/Server/ #驗證
ld: /media/Server/: No such file: No suchfile or directory
整數值比較包含的選項
-eq:等於(equal)
-ne:不等於(not equal)
-lt:小於(lesser than)
-gt:大於(greater than)
-le:小於等於(lesser or equal)
-ge:大於等於(greater or equal)
整數值比較在指令碼中應用較多,如判斷已登入使用者數量。
[[email protected] shelltest]# stati=`who | wc-l`
[[email protected] shelltest]# [ $stati -le 5 ]&& echo "Active users less than 5 peopl"
Active users less than 5 peopl
字串比較
=:第一個字串與第2個字串相同
!=:第1個字串與第2個字串不相同,“!”為取反的意思
-z:檢查字元是否為空白。
邏輯測試
&&:邏輯與,表示“而且”的意思,當兩個條件都成立時才會傳回值為0,使用test測試時“&&”可以改為“-a”
||:邏輯或,表示“或者”的意思,只要前後有一個條件成立,整個測試命令的傳回值即為0,Test時可改為“-o”
!:邏輯否,表示“不”的意思,只有當指定條件不成立時,整個測試命令的傳回值即為0。
用&&做例子:
[[email protected] ~]# [ 6 -ne 4 ] &&echo "yes"
Yes
If 語句
if語句的選擇結構分為三種基本類型
-
if單分支結構,格式如下
if 條件測試操作
then
命令序列
fi
650) this.width=650;" width="553" height="195" src="/e/u261/themes/default/images/spacer.gif" style="background:url("/e/u261/lang/zh-cn/images/localimage.png") no-repeat center;border:1px solid #ddd;" alt="spacer.gif" />
單分支案例
[[email protected] ~]# vim test.sh
#!/bin/bash
#########if單分支案例############
who=`who |wc -l`
if [ $who -le 5 ];then
echo "Active users less than 5people"
fi
[[email protected] ~]# sh test.sh
Active users greater than 5 people
雙分支If語句
雙分支if語句要求針對“條件成立”和“條件不成立”兩種情況執行兩種不同的操作
650) this.width=650;" width="554" height="180" src="/e/u261/themes/default/images/spacer.gif" style="background:url("/e/u261/lang/zh-cn/images/localimage.png") no-repeat center;border:1px solid #ddd;" alt="spacer.gif" />
[[email protected] ~]# vim test.sh
#!/bin/bash
#########if多分支案例############
who=`who |wc -l`
if [ $who -le 5 ];then
echo "Active users less than 5people"
else
echo "Active users greater than people"
fi
[[email protected]~]# sh test.sh
Activeusers greater than 5 people
多分支if語句
多分支if語句根據測試結果,執行不同的操作,所以能夠嵌套使用,進行多次判斷。格式如下:
-
if單分支結構,格式如下
if 條件測試操作1
then
命令序列1
elif 條件測試操作2
then
命令序列2
Else
命令序列3
fi
650) this.width=650;" width="432" height="189" src="/e/u261/themes/default/images/spacer.gif" style="background:url("/e/u261/lang/zh-cn/images/localimage.png") no-repeat center;border:1px solid #ddd;" alt="spacer.gif" />
多分支案例:
[[email protected] ~]# vim Scores_query.sh
#!/bin/bash
###########分數優、良、差查詢指令碼###########
read -p " 請輸入您的分數(1-100):" score
if [ $score -ge 85 ] && [ $score -le100 ]
then
echo " 您的分數為$score分, 為優秀"
elif [ $score -ge 70 ] && [ $score -le84 ]
then
echo " 您的分數為$score,良"
elif [ $score -ge 60 ] && [ $score-le 69 ]
then
echo " 您的分數為$score分, 為及格"
elif [ $score -le 59 ]
then
echo " 您的分數為$score分, 為不及格"
else
echo " 請輸入1-100之間的分數"
fi
[[email protected] ~]# sh Scores_query.sh
請輸入您的分數(1-100):100
您的分數為100分, 為優秀
[[email protected] ~]# sh Scores_query.sh
請輸入您的分數(1-100):80
您的分數為80分, 為良
本文出自 “癮” 部落格,轉載請與作者聯絡!
linux_shell指令碼筆記之二