大資料Shell之條件判斷知識點盤點

2021-10-06 02:55:05 字數 1219 閱讀 3361

1.基本語法

[ condition ](注意condition前後要有空格)

注意:條件非空即為true,[ atguigu ]返回true, 返回false。

2. 常用判斷條件

(1)兩個整數之間比較

= 字串比較

-lt 小於(less than) -le 小於等於(less equal)

-eq 等於(equal) -gt 大於(greater than)

-ge 大於等於(greater equal) -ne 不等於(not equal)

(2)按照檔案許可權進行判斷

-r 有讀的許可權(read) -w 有寫的許可權(write)

-x 有執行的許可權(execute)

(3)按照檔案型別進行判斷

-f 檔案存在並且是乙個常規的檔案(file)

-e 檔案存在(existence) -d 檔案存在並是乙個目錄(directory)

3.案例實操

(1)23是否大於等於22

[atguigu@hadoop101 datas]$ [ 23 -ge 22 ]

[atguigu@hadoop101 datas]$ echo

$?0

(2)helloworld.sh是否具有寫許可權

[atguigu@hadoop101 datas]$ [ -w helloworld.sh ]

[atguigu@hadoop101 datas]$ echo

$?0

(3)/home/atguigu/cls.txt目錄中的檔案是否存在

[atguigu@hadoop101 datas]$ [ -e /home/atguigu/cls.txt ]

[atguigu@hadoop101 datas]$ echo

$?1

[atguigu@hadoop101 ~]$ [ condition ]

&&echo ok ||

echo notok

ok[atguigu@hadoop101 datas]$ [ condition ]&&[

]||echo notok

notok

shell之條件判斷

目錄 二 流程控制語句 2.2 應用示例 1.1 條件判斷語法格式 注意 如何使用格式2或格式3,條件表示式兩側必須有空格!1.2 條件判斷相關引數 1 判斷檔案型別 判斷引數 含義 e 判斷檔案是否存在 任何型別檔案 f判斷檔案是否存在並且是乙個普通檔案 d判斷檔案是否存在並且是乙個目錄 l判斷檔...

Shell條件判斷

b file 若檔案存在且是乙個塊特殊檔案,則為真 c file 若檔案存在且是乙個字元特殊檔案,則為真 d file 若檔案存在且是乙個目錄,則為真 e file 若檔案存在,則為真 f file 若檔案存在且是乙個規則檔案,則為真 g file 若檔案存在且設定了sgid位的值,則為真 h fi...

Shell 條件判斷

傳統if 從句子 以條件表示式作為 if條件 if 條件表示式 then command command command else command command fi條件表示式 檔案表示式 if f file 如果檔案存在 if d 如果目錄存在 if s file 如果檔案存在且非空 if r ...