Shell學習筆記,shell指令碼

來源:互聯網
上載者:User

Shell學習筆記,shell指令碼
1. 判斷格式

1) test 參數 檔案

例: test -e /root/install.log

2) [ 參數 檔案 ]  -- 推薦使用

例: [ -e /root/install.log ]

注意:中括弧後面和前面需要有空格

2. 判斷檔案類型參數

1)-d 檔案:判斷該檔案是否存在,並且是否為目錄檔案

2)-e 檔案:判斷檔案是否存在

3)-f 檔案:判斷檔案是否存在,並且是否為普通檔案

4)-s 檔案:判斷檔案是否存在,並且是否為非空

5)其他檔案類型判斷:

-b 塊裝置檔案;-c 字元裝置檔案;-L 符號連結檔案; -p 管道檔案;-S 通訊端檔案

樣本:

[root@localhost ~]# [ -d /root ] && echo yes || echo noyes[root@localhost ~]# [ -e /root/install.log ] && echo yes || echo noyes[root@localhost ~]# [ -f /root/install.log ] && echo yes || echo noyes[root@localhost ~]# [ -s /root/install.log ] && echo yes || echo noyes

 

3. 判斷檔案許可權參數

1)-r 檔案:判斷該檔案是否存在,並且是否有讀許可權

2)-w檔案:判斷該檔案是否存在,並且是否有寫入權限

3)-x檔案:判斷該檔案是否存在,並且是否有執行許可權

4)其他檔案許可權判斷:

-u SUID許可權;-g SGID許可權; -k SBit許可權

樣本:

[root@localhost ~]# [ -r /root/install.log ] && echo yes || echo noyes[root@localhost ~]# [ -w /root/install.log ] && echo yes || echo noyes[root@localhost ~]# [ -x /root/install.log ] && echo yes || echo nono

 

4. 兩個檔案比較

1)檔案1 -nt 檔案2:判斷檔案1的修改時間是否比檔案2的新

2)檔案1 -ot 檔案2:判斷檔案1的修改是否是否比檔案2的舊

3)檔案1 -ef 檔案2:判斷檔案1是否和檔案2的lnode號一致,可以理解為兩個檔案是否為同一個檔案。這個判斷用於判斷永久連結是很好的方法。

樣本:

[root@localhost ~]# [ /root/install.log -nt /root/install.log.syslog ] && echo yes || echo noyes[root@localhost ~]# [ /root/install.log -ot /root/install.log.syslog ] && echo yes || echo nono[root@localhost ~]# [ /root/install.log -ef /root/install.log.syslog ] && echo yes || echo nono

 

5. 兩個整數比較

1)整數1 -eq 整數2:判斷是否相等

2)整數1 -ne 整數2:判斷是否不相等

3)整數1 -gt 整數2:判斷是否大於

4)整數1 -lt 整數2:判斷是否小於

5)整數1 -ge 整數2:判斷是否大於等於

6)整數1 -le 整數2:判斷是否小於等於

註:在shell裡,所有的變數都是字元類型,但是加了整數比較參數,兩邊的變數就會被認為是整數類型。

樣本:

[root@localhost ~]# [ 1 -eq 1 ] && echo yes || echo noyes[root@localhost ~]# [ 1 -ne 1 ] && echo yes || echo nono[root@localhost ~]# [ 2 -gt 1 ] && echo yes || echo noyes[root@localhost ~]# [ 2 -lt 1 ] && echo yes || echo nono[root@localhost ~]# [ 1 -ge 1 ] && echo yes || echo noyes[root@localhost ~]# [ 1 -le 1 ] && echo yes || echo noyes

 

6. 字串判斷

1)-z 字串:判斷字串是否為空白

2)-n 字串:判斷字串是否非空

3)字串1 == 字串2:判斷兩個字串是否相等

4)字串1 != 字串2:判斷兩個字串是否不相等

樣本:

[root@localhost ~]# str="abc"[root@localhost ~]# [ -z $str ] && echo yes || echo nono[root@localhost ~]# [ -n $str ] && echo yes || echo noyes[root@localhost ~]# str2="efg"[root@localhost ~]# [ "$str" == "$str2" ] && echo yes || echo nono[root@localhost ~]# [ "$str" != "$str2" ] && echo yes || echo noyes

 

7. 多重條件判斷

1)判斷1 -a 判斷2:表示邏輯與,相當於and

2)判斷1 -o 判斷2:表示邏輯或,相當於or

3)!判斷:表示邏輯非,相當於not

樣本:

[root@localhost ~]# [ 1 -eq 1 -a 1 -eq 2  ] && echo yes || echo nono[root@localhost ~]# [ 1 -eq 1 -o 1 -eq 2  ] && echo yes || echo noyes[root@localhost ~]# [ ! 1 -eq 2  ] && echo yes || echo noyes

 

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.