Shell之test,shelltest

來源:互聯網
上載者:User

Shell之test,shelltest

test命令用法。功能:檢查檔案和比較值

1)判斷運算式

if test (運算式為真)

if test !運算式為假

test 運算式1 –a 運算式2                  兩個運算式都為真

test 運算式1 –o 運算式2                 兩個運算式有一個為真

2)判斷字串

test –n 字串                                   字串的長度非零

test –z 字串                                    字串的長度為零

test 字串1=字串2                    字串相等

test 字串1!=字串2               字串不等

3)判斷整數

test 整數1 –eq 整數2                        整數相等

test 整數1 –ge 整數2                        整數1大於等於整數2

test 整數1 –gt 整數2                         整數1大於整數2

test 整數1 –le 整數2                         整數1小於等於整數2

test 整數1 –lt 整數2                          整數1小於整數2

test 整數1 –ne 整數2                        整數1不等於整數2

4)判斷檔案

test File1 –ef File2                           兩個檔案具有同樣的裝置號和i結點號

test File1 –nt File2                            檔案1比檔案2 新

test File1 –ot File2                            檔案1比檔案2 舊

test –b File                                           檔案存在並且是塊裝置檔案

test –c File                                           檔案存在並且是字元裝置檔案

test –d File                                           檔案存在並且是目錄

test –e File                                           檔案存在

test –f File                                            檔案存在並且是正規檔案

test –g File                                           檔案存在並且是設定了組ID

test –G File                                           檔案存在並且屬於有效組ID

test –h File                                           檔案存在並且是一個符號連結(同-L)

test –k File                                           檔案存在並且設定了sticky位

test –b File                                           檔案存在並且是塊裝置檔案

test –L File                                           檔案存在並且是一個符號連結(同-h)

test –o File                                           檔案存在並且屬於有效使用者ID

test –p File                                           檔案存在並且是一個具名管道

test –r File                                            檔案存在並且可讀

test –s File                                           檔案存在並且是一個通訊端

test –t FD                                             檔案描述符是在一個終端開啟的

test –u File                                           檔案存在並且設定了它的set-user-id位

test –w File                                          檔案存在並且可寫

test –x File                                           檔案存在並且可執行


有一句shell命令誰幫忙解釋一下, if test ! -h //Home

這是一個判斷運算式。
用 man test 查看 test 命令的協助:
-h FILE
FILE exists and is a symbolic link (same as -L)
檔案存在並且是一個字元連結(與-L選項相同)。

! EXPRESSION
EXPRESSION is false
運算式為false,即“非”操作(“取反”操作)。

所以,這句判斷語句的意思就是:
如果 ../../Home 這個檔案不存在,或者存在但不是一個字元連結。

如果是一個字元連結,那麼用ls -l ../../Home 應該能看到後面顯示一個連結符號 -> 連結到別的檔案。比如:
$ ls -l /bin/sh
lrwxrwxrwx 1 root root 4 Mar 16 2009 /bin/sh -> bash
 
shell編程if,test命令,問我的理解錯在什地方了?

if test -z $1
then echo 'please input a file name' ; exit #加個退出
else
。。。
不然, if test -x $1 會錯報
 

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在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.