1. 程式人生 > >Linux下Shell程式設計之算術運算和邏輯比較(數值型別、字串型別、檔案型別)

Linux下Shell程式設計之算術運算和邏輯比較(數值型別、字串型別、檔案型別)

shell程式設計中的算術運算和邏輯比較,主要根據不同運算物件,採用相應的比較、運算方式。

一、數值型

算術運算 ,以變數自增1為例,+ - * / % 等運算方式同理,運算的方式大體四種:

  • i=`expr $i + 1`
  • let i+=1 , let的操作成員不需要加$
  • i=$[$i+1]
  • i=$(( $i + 1 ))

邏輯運算——整型比較符,其他型別待續。

  • -eq     等於,如:if [ "$a" -eq "$b" ]  //注意空格  
  • -ne     不等於,如:if [ "$a" -ne "$b" ]
  • -gt     大於,如:if [ "$a" -gt "$b" ]
  • -ge     大於等於,如:if [ "$a" -ge "$b" ]
  • -lt     小於,如:if [ "$a" -lt "$b" ]
  • -le     小於等於,如:if [ "$a" -le "$b" ]
  • <       小於(需要雙括號),如:(("$a" < "$b"))
  • <=      小於等於(需要雙括號),如:(("$a" <= "$b"))
  • >       大於(需要雙括號),如:(("$a" > "$b"))

>=      大於等於(需要雙括號),如:(("$a" >= "$b"))

二、字串型

=       等於,如:if [ "$a" = "$b" ]
==      等於,如:if [ "$a" == "$b" ],與=等價
        注意:==的功能在[[]]和[]中的行為是不同的,如下:
        1 [[ $a == z* ]]    # 如果$a以"z"開頭(模式匹配)那麼將為true
        2 [[ $a == "z*" ]] # 如果$a等於z*(字元匹配),那麼結果為true
        
        4 [ $a == z* ]      # File globbing 和word splitting將會發生
        5 [ "$a" == "z*" ] # 如果$a等於z*(字元匹配),那麼結果為true

一點解釋,關於File globbing是一種關於檔案的速記法,比如"*.c"就是,再如~也是.但是file globbing並不是嚴格的正則表示式,雖然絕大多數情況下結構比較像。
!=      不等於,如:if [ "$a" != "$b" ]
        這個操作符將在[[]]結構中使用模式匹配.


<       小於,在ASCII字母順序下.如:
        if [[ "$a" < "$b" ]]
        if [ "$a" \< "$b" ]
        注意:在[]結構中"<"需要被轉義.


>       大於,在ASCII字母順序下.如:
        if [[ "$a" > "$b" ]]
        if [ "$a" \> "$b" ]
        注意:在[]結構中">"需要被轉義.
        具體參考Example 26-11來檢視這個操作符應用的例子.


-z      字串為"null".就是長度為0.
-n      字串不為"null"
        注意:
        使用-n在[]結構中測試必須要用""把變數引起來.使用一個未被""的字串來使用! -z
        或者就是未用""引用的字串本身,放到[]結構中(見Example 7-6)雖然一般情況下可
        以工作,但這是不安全的.習慣於使用""來測試字串是一種好習慣.

三、檔案型

-a file exists. 
-b file exists and is a block special file. 
-c file exists and is a character special file. 
-d file exists and is a directory. 
-e file exists (just the same as -a). 
-f file exists and is a regular file. 
-g file exists and has its setgid(2) bit set. 
-G file exists and has the same group ID as this process. 
-k file exists and has its sticky bit set. 
-L file exists and is a symbolic link. 
-n string length is not zero. 
-o Named option is set on. 
-O file exists and is owned by the user ID of this process. 
-p file exists and is a first in, first out (FIFO) special file or 
named pipe. 
-r file exists and is readable by the current process. 
-s file exists and has a size greater than zero. 
-S file exists and is a socket. 
-t file descriptor number fildes is open and associated with a 
terminal device. 
-u file exists and has its setuid(2) bit set. 
-w file exists and is writable by the current process. 
-x file exists and is executable by the current process. 
-z string length is zero. 

eg:

#shell判斷資料夾是否存在
 
#如果資料夾不存在,建立資料夾
if [ ! -d "/myfolder" ]; then
 mkdir /myfolder
fi
 
#shell判斷檔案,目錄是否存在或者具有許可權
 
 
folder="/var/www/"
file="/var/www/log"
 
# -x 引數判斷 $folder 是否存在並且是否具有可執行許可權
if [ ! -x "$folder"]; then
 mkdir "$folder"
fi
 
# -d 引數判斷 $folder 是否存在
if [ ! -d "$folder"]; then
 mkdir "$folder"
fi
 
# -f 引數判斷 $file 是否存在
if [ ! -f "$file" ]; then
 touch "$file"
fi
 
# -n 判斷一個變數是否有值
if [ ! -n "$var" ]; then
 echo "$var is empty"
 exit 0
fi
 
# 判斷兩個變數是否相等
if [ "$var1" = "$var2" ]; then
 echo '$var1 eq $var2'
else
 echo '$var1 not eq $var2'
fi

四、典例以及詳解
複製程式碼程式碼如下:

#./bin/bash 
#獲取英文月份對應的數字月份 
getmonthofenglish() 
{ 
case "$1" in 
"Jan") msg_month=1;; 
"Feb") msg_month=2;; 
"Mar") msg_month=3;; 
"Apr") msg_month=4;; 
"May") msg_month=5;; 
"Jun") msg_month=6;; 
"Jul") msg_month=7;; 
"Aug") msg_month=8;; 
"Sept") msg_month=9;; 
"Oct") msg_month=10;; 
"Nov") msg_month=11;; 
"Dec") msg_month=12;; 
esac 
} 
# 判斷是否有過期簡訊,有呼叫C刪除簡訊函式 
del_overtime_sms() 
{ 
num=`ipcs -q|grep "$1"|awk '{print $6}'` 
if (( "$num" > "1" )); then 
ipcs -q|grep "$1"|awk '{print $2,$6}'>overtimesms.ini 
./del_overtime_sms //shell中呼叫C函式的結果 
else 
echo "訊息佇列中沒有過期的簡訊" 
fi 
} 
#獲取當前月份對應的天數 
cd /mas/shell/zhangweiatest/clear_overtime_sms 
mday=`./getmonthofday` 
echo "當前月份前一個月對應的天數=$mday" 
#獲取系統時間對應的年,月 
buff=`date|awk '{print $2,$3}'` 
now_month=${buff:0:1} 
now_day=${buff:3:1} 
#獲取當前佇列的存活時間 
ipcs -qt|awk '{print $1,$3,$4}'>msglivelytime.ini 
sed -n '4,$p' msglivelytime.ini >msglivelytime1.ini //獲取從第四行開始到結束的字元 
mv msglivelytime1.ini msglivelytime.ini 
while read msgid msg_month msg_day 
do 
echo 
echo "訊息佇列ID=$msgid建立的時間為:$msg_month月$msg_day日" 
echo "系統時間為$now_month月的第$now_day天" 
getmonthofenglish "$msg_month" 
let "new_month = $msg_month + 1" 
if [ "$msg_month" = "Not" ]; then //為空NULL 
echo "佇列安全" 
elif [ "$msg_month" = "" ]; then //空行 
echo "NULL" 
elif [ "$msg_month" = "$now_month" ]; then 
let "msg_day1 = $msg_day + 2" 
if (( "$now_day" >= "$msg_day1" )); then 
if (( "$msg_day" < "$mday" )); then 
echo "訊息佇列不安全" 
del_overtime_sms "$msgid" 
fi 
fi 
elif [ "$now_month" = "$new_month" ]; then 
let "now_day1 = $now_day + $mday" 
let "msg_day1 = $msg_day + 2" 
echo "now_day1=$now_day1,msg_day1=$msg_day1" 
if (( "$now_day1" >= "$msg_day1" )); then 
# if (( "$msg_day1" < "$mday" )); then 
echo "訊息佇列不安全" 
del_overtime_sms "$msgid" 
# fi 
fi 
elif (( "$now_month" > "$new_month" )); then 
echo ""$msg_month" > "$now_month"" 
del_overtime_sms "$msgid" 
echo "訊息佇列不安全" 
else 
echo "msg queue safe" 
fi 
done < msglivelytime.ini

在 shell 中的四則運算必須使用 expr 這個指令來輔助。因為這是一個指令,所以如果要將結果指定給變數,必須使用 ` 包起來。請注意,在 + - * / 的二邊都有空白,如果沒有空白將產生錯誤: 
 

複製程式碼程式碼如下:


$ expr 5 -2 
3 
$ sum=`expr 5 + 10` 
$ echo $sum 
15 
$ sum=`expr $sum / 3` 
$ echo $sum 
5 

還有一個要特別注意的是乘號 * 在用 expr 運算時,不可只寫 *。因為 * 有其它意義,所以要使用 \* 來代表。另外,也可以用 % 來求餘數。 

複製程式碼程式碼如下:


$ count=`expr 5 \* 3` 
$ echo $count 
$ echo `expr $count % 3` 
5 


我們再列出更多使用 expr 指令的方式,下列表中為可以放在指令 expr 之後的表示式。有的符號有特殊意義,必須以 \ 將它的特殊意義去除,例如 \*,否則必須用單引號將它括起來,如 '*':

類別 語法 說明
條件判斷 expr1 \| expr2 如果 expr1 不是零或 null 則傳回 expr1,否則傳回 expr2。
expr1 \& expr2 如果 expr1 及 expr2 都不為零或 null,則傳回 expr1,否則傳回 0。
四則運算 expr1 + expr2 傳回 expr1 加 expr2 後的值。
expr1 - expr2 傳回 expr1 減 expr2 後的值。
expr1\* expr2 傳回 expr1 乘 expr2 後的值。
expr1 / expr2 傳回 expr1 除 expr2 後的值。
expr1 % expr2 傳回 expr1 除 expr2 的餘數。
大小判斷 expr1 \> expr2 如果 expr1 大於 expr2 則傳回 1,否則傳回 0。如果 expr1 及 expr2 都是數字,則是以數字大小判斷,否則是以文字判斷。以下皆同。
expr1 \< expr2 如果 expr1 小於 expr2 則傳回 1,否則傳回 0。
expr1 = expr2 如果 expr1 等於 expr2 則傳回 1,否則傳回 0。
expr1 != expr2 如果 expr1 不等於 expr2 則傳回 1,否則傳回 0。
expr1 \>= expr2 如果 expr1 大於或等於 expr2 則傳回 1,否則傳回 0。
expr1 \<= expr2 如果 expr1 小於或等於 expr2 則傳回 1,否則傳回 0。
文書處理 expr1 : expr2 比較一固定字串,即 regular expression。可以使用下列字元來輔助:

. 匹配一個字元。

$ 找字串的結尾。

[list] 找符合 list 中的任何字串。

* 找尋 0 個或一個以上在 * 之前的字。

\( \) 傳回括號中所匹配的字串。


我們針對比較複雜的文書處理部份再加以舉例:

複製程式碼程式碼如下:
$ tty
ttyp0
$ expr `tty` : ".*\(..\)\$"
p0
$ expr `tty` : '.*\(..\)$'
p0

上面執行 tty 的結果是 ttyp0,而在 expr 中,在 : 右側的表示式中,先找 .* 表示0個或一個以上任何字元,傳回之後在結尾 ($) 時的二個字元 \(..\)。在第一個 expr 的式子中,因為使用雙引號,所以在 $ 之前要用一個 \ 來去除 $ 的特殊意義,而第二個 expr 是使用單引號,在單引號內的字都失去了特殊意義,所以在 $ 之前不必加 \。

除了使用 expr 外,我們還可以使用下列這種特殊語法:

複製程式碼程式碼如下:
$ a=10
$ b=5
$ c=$((${a}+${b}))
$ echo $c
15
$ c=$((${a}*${b}))
$ echo $c
50

我們可以使用 $(()) 將表示式放在括號中,即可達到運算的功能。

參考地址

部分例子參考網路,地址遺失,謝謝原作者!如有冒犯,立刻刪除!