(2)整數操作符具有和字符操作符類(lèi)似的功能.只是他們的操作是針對整數
test表達式 | 含義
---------------------------------------------
Int1 -eq int2|當int1等于int2時(shí),返回True
Int1 -ge int2|當int1大于/等于int2時(shí),返回True
Int1 -le int2|當int1小于/等于int2時(shí),返回True
Int1 -gt int2|當int1大于int2時(shí),返回True
Int1 -ne int2|當int1不等于int2時(shí),返回True
-----------------------------------------
(3)用于文件操作的操作符,他們能檢查:文件是否存在,文件類(lèi)型等
test表達式 | 含義
------------------------------------------------
-d file |當file是一個(gè)目錄時(shí),返回 True
-f file |當file是一個(gè)普通文件時(shí),返回 True
-r file |當file是一個(gè)刻讀文件時(shí),返回 True
-s file |當file文件長(cháng)度大于0時(shí),返回 True
-w file |當file是一個(gè)可寫(xiě)文件時(shí),返回 True
-x file |當file是一個(gè)可執行文件時(shí),返回 True
------------------------------------------------
(4)shell的邏輯操作符用于修飾/連接包含整數,字符串,文件操作符的表達式
test表達式 | 含義
----------------------------------------------------------
! expr |當expr的值是False時(shí),返回True
Expr1 -a expr2|當expr1,expr2值同為T(mén)rue時(shí),返回True
Expr1 -o expr2|當expr1,expr2的值至少有一個(gè)為T(mén)rue時(shí),返回True
-----------------------------------------------------------
注意:
tcsh shell 不使用test命令,但是tcsh中的表達式同樣能承擔相同的功能.tcsh
支持的表達式于C中的表達式相同.通常使用在if和while命令中.
tcsh表達式 | 含義
-------------------------------------------------------
Int1 <= int2 |當int1小于/等于int2時(shí),返回True
Int1 >= int2 |當int1大于/等于int2時(shí),返回True
Int1 < int2 |當int1小于int2時(shí),返回True
Int1 > int2 |當int1大于int2時(shí),返回True
Str1 == str2 |當str1與str2相同時(shí),返回True
Str1 != str2 |當str1與str2不同時(shí),返回True
-r file |當file是一個(gè)可讀文件時(shí),返回True
-w file |當file是一個(gè)可寫(xiě)文件時(shí),返回True
-x file |當file是一個(gè)可執行文件時(shí),返回True
-e file |當file存在時(shí),返回True
-o file |當file文件的所有者是當前用戶(hù)時(shí),返回True
-z file |當file長(cháng)度為0時(shí),返回True
-f file |當file是一個(gè)普通文件時(shí),返回True
-d file |當file是一個(gè)目錄時(shí),返回True
Exp1 || exp2 |當exp1和exp2的值至少一個(gè)為T(mén)rue時(shí),返回True
Exp1 && exp2 |當exp1和exp2的值同為T(mén)rue時(shí),返回True
! exp |當exp的值為False時(shí),返回True
-------------------------------------------------------
5.條件語(yǔ)句
同其他高級語(yǔ)言程序一樣,復雜的shell程序中經(jīng)常使用到分支和循環(huán)控制結構,
bash,pdksh和tcsh分別都有兩種不同形式的條件語(yǔ)句:if語(yǔ)句和case語(yǔ)句.
(1)if語(yǔ)句
語(yǔ)法格式:
bash/pdksh用法:
if [expression1]
then
commands1
elif [expression2]
commands2
else
commands3
if
tcsh用法:
if (expression1) then
commands1
else if (expression2) then
commands2
else
commands3
endif
含義:當expression1的條件為T(mén)rue時(shí),shell執行then后面的commands1命令;當
expression1的條件為false并且expression2的條件滿(mǎn)足為T(mén)rue時(shí),shell執行
commands2命令;當expression1和expressin2的條件值同為false時(shí),shell執行
commands3命令.if語(yǔ)句以他的反寫(xiě)fi結尾.
聯(lián)系客服