if判斷常用

2021-08-19 19:33:12 字數 4358 閱讀 9661

語法

if ....; then

....

elif ....; then

....

else

....

fi幾類用法:

1.單個

[ ]是內建條件判斷符號。字串比較符號》 <  要加轉義符號'\',多條件用 -a(and,表示&&) -o(or,表示||)連線;

a=4b=2

c=3if [ $a -le $b ] || [ $b -le $c ];then

echo "ok"

else

echo "no"

fi或者

if [ $a -le $b -o $b -le $c ];then

echo "ok"

else

echo "no"fi}

2.兩個

[[ ]]是關鍵字,可以直接支援 > < = && ||等符號,不用轉義(只有字串才可以用這些符號比較,數字還是要用-gt/-lt等比較)

a=4b=2

c=3if [[ $a -le $b || $b -le $c ]];then

echo "ok"

else

echo "no"fi}

3.兩個(())

(())雙圓括號允許在比較中包含比較高階的數學公式,比如**等

一般情況下  裡配置乙個比較符號如=是對比字元,(())裡配置兩個比較符號如》=是用來比較數字的

if一些常用引數

1、字串判斷

str1 = str2      當兩個串有相同內容、長度時為真 

str1 != str2      當串str1和str2不等時為真 

-n str1        當串的長度大於0時為真(串非空) 

-z str1        當串的長度為0時為真(空串) 

str1           當串str1為非空時為真

2、數字的判斷

int1 -eq int2    兩數相等為真 

int1 -ne int2    兩數不等為真 

int1 -gt int2    int1大於int2為真 

int1 -ge int2    int1大於等於int2為真 

int1 -lt int2    int1小於int2為真 

int1 -le int2    int1小於等於int2為真

3、檔案的判斷

-r file     使用者可讀為真 

-w file     使用者可寫為真 

-x file     使用者可執行為真 

-f file     檔案為正規檔案為真 

-d file     檔案為目錄為真 

-c file     檔案為字元特殊檔案為真 

-b file     檔案為塊特殊檔案為真 

-s file     檔案大小非0時為真 

-t file     當檔案描述符(預設為1)指定的裝置為終端時為真

4、複雜邏輯判斷

-a         與 

-o        或 

!        非

附 表:

[ -a file ]  如果 file 存在則為真。  

[ -b file ]  如果 file 存在且是乙個塊特殊檔案則為真。  

[ -c file ]  如果 file 存在且是乙個字特殊檔案則為真。  

[ -d file ]  如果 file 存在且是乙個目錄則為真。  

[ -e file ]  如果 file 存在則為真。  

[ -f file ]  如果 file 存在且是乙個普通檔案則為真。  

[ -g file ] 如果 file 存在且已經設定了sgid則為真。 [ -h file ]  如果 file 存在且是乙個符號連線則為真。  

[ -k file ]  如果 file 存在且已經設定了粘制位則為真。  

[ -p file ]  如果 file 存在且是乙個名字管道(f如果o)則為真。  

[ -r file ]  如果 file 存在且是可讀的則為真。  

[ -s file ]  如果 file 存在且大小不為0則為真。  

[ -t fd ]  如果檔案描述符 fd 開啟且指向乙個終端則為真。  

[ -u file ]  如果 file 存在且設定了suid (set user id)則為真。  

[ -w file ]  如果 file 如果 file 存在且是可寫的則為真。  

[ -x file ]  如果 file 存在且是可執行的則為真。  

[ -o file ]  如果 file 存在且屬有效使用者id則為真。  

[ -g file ]  如果 file 存在且屬有效使用者組則為真。  

[ -l file ]  如果 file 存在且是乙個符號連線則為真。  

[ -n file ]  如果 file 存在 and has been mod如果ied since it was last read則為真。  

[ -s file ]  如果 file 存在且是乙個套接字則為真。  

[ file1 -nt file2 ]  如果 file1 has been changed more recently than file2, or 如果 file1 exists and file2 does not則為真。  

[ file1 -ot file2 ]  如果 file1 比 file2 要老, 或者 file2 存在且 file1 不存在則為真。  

[ file1 -ef file2 ]  如果 file1 和 file2 指向相同的裝置和節點號則為真。  

[ -o optionname ]  如果 shell選項 「optionname」 開啟則為真。  

[ -z string ]  「string」 的長度為零則為真。  

[ -n string ] or [ string ]  「string」 的長度為非零 non-zero則為真。  

[ string1 == string2 ]  如果2個字串相同。 「=」 may be used instead of 「==」 for strict posix compliance則為真。  

[ string1 != string2 ]  如果字串不相等則為真。 

[ string1 < string2 ]  如果 「string1」 sorts before 「string2」 lexicographically in the current locale則為真。  

[ string1 > string2 ]  如果 「string1」 sorts after 「string2」 lexicographically in the current locale則為真。  

[ arg1 op arg2 ] 「op」 is one of -eq, -ne, -lt, -le, -gt or -ge. these arithmetic binary operators return true if 「arg1」 is equal to, not equal to, less than, less than or equal to, greater than, or greater than or equal to 「arg2」, respectively. 「arg1」 and 「arg2」 are integers.

selenium 常用判斷

獲取頁面 title 的方法可以直接用 driver.title 獲取到,然後也可以把獲取到的結果用做斷言。1.首先匯入 expected conditions 模組 from selenium.webdriver.support import expected conditions 2.由於這個模...

指標常用值判斷

常見的值可做判斷 0xcdcdcdcd created but not initialised 0xdddddddd deleted 0xfeeefeee freed memory set by nt s heap manager 0xcccccccc uninitialized locals in...

Application常用判斷函式

returns internet reachability status.返回網路可達性狀態。如果遊戲從unity編輯器中執行,返回真 如果從其他部署目標執行返回假。print we are running this from inside of the editor 是否正在載入某些關卡。方法 l...