shell指令碼:test命令 if-then for while 學習筆記

來源:互聯網
上載者:User

test 檔案運算子:

-b file  如果檔案為一個塊特殊檔案,則為真

-c file  如果檔案為一個字元特殊檔案,則為真

-d file  如果檔案為一個目錄,則為真

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

-f file  如果檔案為一個普通檔案,則為真

-g file  如果設定了檔案的 SGID 位,則為真

-G file  如果檔案存在且歸該組所有,則為真

-k file  如果設定了檔案的粘著位,則為真

-O file  如果檔案存在並且歸該使用者所有,則為真

-p file  如果檔案為一個具名管道,則為真

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

-s file  如果檔案的長度不為零,則為真

-S file  如果檔案為一個通訊端特殊檔案,則為真

-t fd   如果 fd 是一個與終端相連的開啟的檔案描述符(fd 預設為 1),則為真

-u file  如果設定了檔案的 SUID 位,則為真

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

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


if-then結構化命令 執行個體:

file=test1touch fileif [ -s file ]then        echo " The file file exits and has data in it"else        echo " The file file exits ,but  has not  data in it"fidate > fileif [ -s file ]then        echo "the file file has data in it"else        echo "the file file is also empty ,no data hummm"fiif [ 1.c -nt one2.c ]then        echo "1.c is newer than one2.c"else        echo "1.c is not newer than one2.c"fi


for命令:

for var in list
do
   commands
done 

執行個體:

list="one one2 one4 one5"list=$list" one6"//在已經存在的變數中添加新的資料for state in $listdo        echo "state is $state"done結果:./forteststate is onestate is one2state is one4state is one5state is one6

讀取命令中的值:file="states"for state in 'cat $file'do        echo "visit word $state"done~      

while命令

格式:

while the command
do
    other commands
done


執行個體:

var1=10 #賦值是不能有空格的 有空格就會出現問題 例如: /last: 第 3 行: [: -gt: 期待一元運算式等echo "var1 is $var1"while [ $var1 -gt 0 ]do        echo "the var1 is $var1"        var1=$[$var1 -1 ]done

結果:

the var1 is 10the var1 is 9the var1 is 8the var1 is 7the var1 is 6the var1 is 5the var1 is 4the var1 is 3the var1 is 2the var1 is 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.