退出、測試、判斷及操作符

1、退出狀態
狀態值 含義
0 表示運行成功,程序執行未遇到任何問題
1~125 表示運行失敗,腳本命令、系統命令錯誤或參數傳遞錯誤
126 找到了該命令但無法執行
127 爲找到要運行的命令
>128 命令被系統強行結束

2、測試
2.1測試結構
 (1)test expression
 (2)[ expression ]
如果測試條件爲真,返回0;如果爲假,返回1
其中expression是一個表達式,可由數字、字符串、文本和文件屬性的比較,同時可加入各種算術、字符串、文本等運算符。
2.2整數比較運算符
   整數比較運算符 描述
 num1 -eq num2 如果 num1 =  num2,返回0
 num1 -ge num2 如果 num1 >= num2,返回0
 num1 -gt num2 如果 num1 >  num2,返回0
 num1 -le num2 如果 num1 <= num2,返回0
 num1 -lt num2 如果 num1 <  num2,返回0
 num1 -ne num2 如果 num1 != num2,返回0
2.3字符串運算符
   字符串運算符 描述
 string 測試字符串string是否不爲空
 -n string 測試字符串string是否不爲空
 -z string 測試字符串string是否爲空
 string1  = string2 測試字符串string1是否與字符串string2相同
 string1 != string2 測試字符串string1是否與字符串string2不同
2.4文件操作符
   格式:(1)test file_operator File
(2)[ file_operator File ]
其中file_operator爲文件操作符,file爲文件名、目錄名或文件路徑等。
   文件操作符 描述
 -d file 測試file是否爲目錄
 -e file 測試file是否存在
 -f file 測試file是否爲普通文件
 -r file 測試file是否是進程可讀文件
 -s file 測試file的長度是否爲0
 -w file 測試file是否是進程可寫文件
 -x file 測試file是否是進程可執行文件
 -L file 測試file是否符號化鏈接
2.5邏輯運算符
   邏輯操作符 描述
 !expression 邏輯非
 expression1 -a expression2 邏輯與
 expression1 -o expression2 邏輯或
例:(1)expre1 -a expre2 -a expre3
  (2)expre1 -o expre2 -o expre3
  (3)[ ! -e file_name ] //是否不存在文件名爲file_name的文件
  (4)[ -e file_name -a -x file_name ] //是否文件file_name存在且可讀
  (5)[ "$int1" -lt 20 -o "$int1" -gt 30 ] //判斷整數int1是否大於20或大於30

3、判斷
3.1 簡單if結構
   if expression
   then
command
command
command
...
   fi
3.2 exit命令
   格式:exit status
其中status用0~255之間的數字表示,一般返回該狀態之的同時伴隨這腳本的退出,同退出狀態一樣,參數被保存在shell變量$?中。
   例:
#!/bin/bash
echo "Please input a string:"
read str1
#判斷字符串str1是否爲空
if [ -z "$str1" ]
then 
   "What you input is null!!" 
   exit 1
fi
3.3 if/else機構
格式:if expression1
   then
command
...
command
   else
command
...
command
   fi
   例(1):
#!/bin/bash
if [ ! -e "$1" ]
then
echo "file $1 do not exist"
exit 1
else
echo "file $1 exits"
fi
   例(2):
#!/bin/bash
echo "Please input the file which you want to delete:"
read file
#通過if/else結構判斷文件是否被刪除
if rm -f "$file"
then
echo "Delete the file $file secessfully!"
else
echo "Delete the file $file failed!"
fi
3.4 if/else語句嵌套
   例:
#!/bin/bash
#測試用戶輸入是否爲空,然後判斷當前目錄是否存在該文件
if [ "$1" ]
then 
echo "Waht yo input is not null!"
if [ -e "$1" ]
then
echo "The file $1 is existence!"
else
echo "The file $1 is not existence!"
fi
else
echo "what you input is null!"
fi
3.5 if/elif/else結構
格式:   if expression1
then
command
command
...
elif expression2
then
command
command
...
elif expression3
then 
command
command
...
else
command
command
...
fi
例(1):
#!/bin/bash
echo "please input a integer(0-100):"
read score
if [ "$score" -le 0 -o "$score" -gt 100 ]
then
echo "The score what you input is not integer or the score is not in (0-100)."
elif [ "$score" -ge 90 ]
then
echo "The grade is A!"
elif [ "$score" -ge 80 ]
then
echo "The grade is B!"
elif [ "$score" -ge 70 ]
then
echo "The grade is C!"
elif [ "$score" -ge 60 ]
then
echo "The grade is D!"
else
echo "The grade is E!"
fi
例(2):
#!/bin/bash
echo "Please input a year:"
read year
#設置取餘參數
let "n1=$year % 4"
let "n2=$year % 100"
let "n3=$year % 400"
#判斷輸入的年份是否是閏年
if [ ! "$n1" -eq 0 ]
then
leap=0
elif [ ! "$n2" -eq 0 ]
then
leap=1
elif [ ! "$n3" -eq 0 ]
then
leap=0
else
leap=1
fi

#輸出用戶輸入的年份是否是閏年
if [ "$leap" -eq 1 ]
then
echo "$year is a leap year!"
else
echo "$year is not a leap year!"
fi
3.6 case結構
例:腳本提示輸入一個數字(1~12),然後顯示其對應月份的英文
#!/bin/bash
echo "Please input a month(0-12):"
read month
#判斷數字對應的月份
case "$month" in
1)
echo "The month is January!";;
2)
echo "The month is February!";;
3)
echo "The month is March!";;
4)
echo "The month is April!";;
5)
echo "The month is May!";;
6)
echo "The month is June!";;
7)
echo "The month is July!";;
8)
echo "The month is August!";;
9)
echo "The month is September!";;
10)
echo "The month is October!";;
11)
echo "The month is November!";;
12)
echo "The month is December!";;
*)
echo "The month is not in (0-12).";;
esac
#顯示case腳本執行完成,開始執行esac後面的命令
echo "The 'case' command ends!"

4、運算符
4.1 算術運算符
   運算符 舉例 結果
 + (加運算) 3+5 8
 - (減運算) 5-3 2
 * (乘運算) 5*3 15
 / (除運算) 8/3 2
 % (取餘  ) 15%4 3
 **(冪運算) 5**3 125
let 命令的替代表示形式是: ((算術表達式)) 
例如,let ″j=i*6+2″等價於((j=i*6+2))。 
如果表達式的值是非0,那麼返回的狀態值是0;否則,返回的狀態值是1。 
當表達式中有Shell的特殊字符時,必須用雙引號將其括起來。
例如,let ″val=a|b″。如果不括起來,Shell會把命令行let val=a|b中的“|”看成管道符,將其左右兩邊看成不同的命令,因而無法正確執行。 
算術複合賦值運算符
+= -= *= /= %=
4.2 位運算符
   運算符   舉例   解釋和value值
<<(左移    ) value=4<<2 4左移2位,value值爲16
>>(右移    ) value=8>>2 8右移2位,value值爲2
& (按位與  ) value=8&4 8按位與4,value值爲0
| (按位或  ) value=8|4 8按位或4,value值爲12
~ (按位非  ) value=~8 按位非8,value值爲-9
^ (按位異或) value=10^3 10按位異或3,value值爲9
複合運算符
<<= >>= &= |= ^=

4.3 自增自減運算符
++variavle,--variable,variable++,variable--

4.4 數字常量
Linux Shell 腳本或命令默認將數字以十進制的方式進行處理,如果要使用其他進制的方式進行處理,則需要對這個數字進行特定的標記或加前綴。當使用0作爲前綴時,表示八進制;當使用0x進行標記時,表示十六進制,同時還可以使用num#這種形式標記進制數。
例:let "num1=40" #表示十進制
  let "num2=040" #表示八進制
  let "num3=0x40" #表示十六進制
  let "num4=2#110110" #表示二進制
   
發佈了45 篇原創文章 · 獲贊 3 · 訪問量 4萬+
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章