(七)Shell條件測試

2021-10-09 10:28:06 字數 1739 閱讀 5143

分為:檔案測試、整型測試、字串測試、邏輯測試

#!/bin/bash

#test.sh

if [ -d /root/shelltest ]

then

echo "this is a 目錄"

else

echo "這不是目錄"

fi

[ -d /root/shelltest ]       #是否為目錄

[ -f /root/shelltest/test1.sh ] #是否為檔案

-e 目錄或檔案是否存在

-r 當前使用者是否有許可權讀取

-w 當前使用者是否有許可權寫入

-x 當前使用者是否有許可權執行

-l 是否為符號鏈結檔案

[ -d /root/shelltest/ ] && echo 'yes'
-eq    等於

-ne 不等於

-gt 大於

-lt 小於

-le 小於等於

-ge 大於等於

echo '磁碟空間使用大於10%'

else

echo '磁碟使用空間小於等於10%'

fi

[ 字串1 = 字串2 ]

[ 字串1 != 字串2 ]

[ -z 字串 ]

#!/bin/bash

#test5.sh

read -p 'please input your name:' name

read -p 'please input your age:' age

if [ $name = 'admin' ] && [ $age -eq 18 ]

then

echo '登陸成功'

else

echo '登陸失敗'

shell條件測試

shell條件測試通常都會用在for while until if等控制流結構中,用於判斷檔案的相關性質或變數的相互關係。條件測試用法 test 表示式 結果 成立返回0,不成立返回非0 檢視結果 echo 以下是幾類常用的測試表示式 1 檔案狀態測試 b filename 當filename 存在...

shell條件測試

shell條件測試 檔案狀態測試 b filename 當filename 存在並且是塊檔案時返回真 返回0 c filename 當filename 存在並且是字元檔案時返回真 d pathname 當pathname 存在並且是乙個目錄時返回真 e pathname 當由pathname 指定的...

shell條件測試

1.數值測試 數值判斷的格式如下 數值1 關係運算子 數值2 方括號與條件之間必須要有空格 eq 兩個數值相等 lt 第乙個數值小於第二個數值 ne 兩個數值不相等 ge 第乙個數值大於第二個數值 gt 第乙個數值不小於第二個數值 le 第乙個數值不大於第二個數值 例 100 eq 100 echo...