1. 程式人生 > >shell指令碼:命令

shell指令碼:命令

命令連線符

;表示不管前面是否執行成功都要執行

&&表示前面執行成功才執行後面

||表示前面執行失敗才執行後面

read命令

read [選項] 值

read -p(提示語句) -n(字元個數) -t(時間秒) -s(不顯示)

運算子

expr 3 + 2

結果賦值

sum=`expr 3 + 2` 或者 sum=$((3 + 2))

乘法

expr 3 \* 2 #需要轉義,*代表任意字元

expr `expr 3 + 2` \* 5   ==> (3 + 2)* 5

sum=`expr \`expr 3 + 2\` \* 5`   或者 sum=$(((3 + 2) * 5))

$()和${}的區別

$()的用途和``一樣,迎來表示優先執行的命令

${}就是去變數

$(())適用於數值運算

條件判斷

內建test命令

內建test命令常用操作符號[]表示,將表示式寫在[]中

[ expression ] #expression前後各有空格

或者 test expression

測試範圍:整數、字串、檔案

字串測試

test str1 == str2

test str1 != str2

test str 測試字串是否不為空 test $num5;echo $?

test -n str1 測試字元不為空 需要給str1 加雙引號

test -z str1 測試字串為空

字串測試可以加邏輯符號

test -z str1 && echo invlid || echo ok

整數測試

test int1 -eq int2  --> ==

test int1 gt int2   -->  >

test int1 lt int2   --> <

test int1 ge int2   --> >=

test int1 le int2   -->  <=

test int1 ne int2   --> !=

檔案測試

test -d file 指定檔案是否目錄

test -e file 檔案是否存在

test -f file 指定檔案是否常規檔案

test -L file 檔案存在並且是一個符號連結

test -r file 指定檔案是否可讀

test -w file 指定檔案是否可寫

test -x file 制定檔案是否可執行

多重條件測試

條件1 -a 條件2 邏輯與 兩個都成立,則為真

條件1 -o 條件2 邏輯或

!條件 邏輯非 取反