Linux shell中的if判斷

2021-08-11 11:01:03 字數 1807 閱讀 8268

-e file	如果 file存在,則為真 

-d file 如果 file為目錄,則為真

-f file 如果 file為常規檔案,則為真

-l file 如果 file為符號鏈結,則為真

-r file 如果 file可讀,則為真

-w file 如果 file可寫,則為真

-x file 如果 file可執行,則為真

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

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

-s file 如果 file大小非0時為真

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

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

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

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

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

str1         當串str1為非空時為真

\ > \ < 比較大小的時候 可以使用大於號,小於號,但是需轉義。

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為真

-a         與 and

-o        或 or

!        非

&& 代表and

|| 代表or

例如:

if [ 「a"e

q1−o

"a" eq 1 -o "

a"eq1−

o"b」 eq 2 ] && [ 「$c」 eq 3 ]

if [ $osname = "aix" ] ; then

lang=ja_jp

export lang

elif [ $osname = "hp-ux" ] ; then

lang=ja_jp.eucjp

export lang

fi

test和if判斷類似,

例如:判斷檔案是否存在

test -e file

if [ -e file ]

if [ $ -ne 0 ];then exit 1; fi
if [ $osname = "aix" ] ; then

lang=ja_jp

export lang

else

exit 1

fi

if [ $osname = "aix" ] ; then

lang=ja_jp

export lang

elif [ $osname = "hp-ux" ] ; then

lang=ja_jp.eucjp

export lang

else

exit 1

fi

Linux shell中的變數

1.變數 1 名稱 字母 數字 下劃線組成,數字不能開頭 2 在bash中,變數的預設型別都是字串型,別的型別要指定 2.變數分類 1 本地變數 使用者變數 區域性變數只在建立它們的shell中使用,也可以修改 2 環境變數 可以在建立它們的shell及其派生出來的任意子程式中使用和修改 有些變數是...

linux shell 中 的含義

介紹下shell中的katex parse error expected eof got at position 4 和 使用範例,本文給出了不 分別替換得到不同的值 刪掉第乙個 及其左邊的字串 dir1 dir2 dir3 my.file.txt 刪掉最後乙個 及其左邊的字串 my.file.tx...

linux shell中 , , 的含義

今天在學習shell的時候看到一行 是這樣的 echo hello test.txt這行 的意思是將 hello 這個字串輸入到test.txt檔案中,但是我對 的作用好奇了起來 經過查閱,記錄一下 linux中經常會用到將內容輸出到某檔案當中,只需要在執行命令後面加上 或者 號即可進入操作。簡單的...