1 / 8

test command - 1

test command - 1. Shell Script 程式撰寫常用到測試參數的條件是否符合 ,test 指令即用來做測試用 . 請特別注意 test 這個指令亦可以用中括號 [] 取代之 . << 下面的 test 指令是針對檔案而測試 >> test – d file 測試 file 是否為目錄 test -f file 測試 file 是否為一般檔案 test -L file 測試 file 是否為連結檔 test – r file 測試 file 是否可讀 test -w file 測試 file 是否可寫

kamana
Download Presentation

test command - 1

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. test command - 1 • Shell Script程式撰寫常用到測試參數的條件是否符合,test指令即用來做測試用. 請特別注意 test這個指令亦可以用中括號[] 取代之. • << 下面的test指令是針對檔案而測試 >> • test –d file 測試file是否為目錄 • test -f file 測試file是否為一般檔案 • test -L file 測試file是否為連結檔 • test –r file 測試file是否可讀 • test -w file 測試file是否可寫 • test -x file 測試file是否可執行 • test –e file 測試file是否存在 • test -s file 測試file是否檔案大小>0 • test -S file 測試file是否是一個Socket

  2. test command-2 • 下面的test指令最常用來做if及迴圈條件的判斷式. • << 下面的test指令是針對數值而測試 >> • test n1 –eq n2 即測試 n1=n2 ? • test n1 –ne n2 即測試 n1n2 ? • test n1 –ge n2 即測試 n1>=n2 ? • test n1 –gt n2 即測試 n1>n2 ? • test n1 –le n2 即測試 n1<=n2 ? • test n1 –lt n2 即測試 n1<n2 ?

  3. test command-3 • 下面的test指令是對 expr的式子再做是否真(true)或假(false)的判斷 • test !expr 測試expr的結果是否為假 • test expr1 –a expr2 測試二個式子取AND的結果 • test expr1 –o expr2 測試二個式子取OR的結果 下面的test指令是對字串(s)做比較 test str1 = str2 測試是否二個字串相等? test str1 == str2 測試是否二個字串相等? test str1 != str2 測試是否二個字串不等? test –n str 測試是否字串長度不為0? test –z str 測試是否為字串長度為0?

  4. Shell Script 練習 檔名: makex • 熟悉Unix的你,如果常常要去建立執行檔,那麼可不可以直接寫個script來完成? • 要執行時請打: • sh makex makex • makex 檔名 # make executable file #!/bin/sh chmod u+x $1 echo $1 is now executable ls –l $1 檔名: morewc • 如果我們需要利用wc指令來看檔案內容的行數及字數,那可不可以寫個script讓列出的內容更明確?? • 要執行時請打: • makex morewc • morewc 檔名 # make wc more readable #!/bin/sh set `wc $1` echo “File: $4” echo “lines: $1” echo “words: $2” echo “characters: $3”

  5. Shell Script 練習 檔名: remember 想寫一個script檔案來提醒您週一到週日每天應做的事. 要執行時請打: makex remember remember #A daily reminder service #!/bin/sh echo “===remind of the week==” Set `date` case $1 in 週一) echo “Plan the week”;; 週二) echo “meet teacher” ;; 週三) echo “Join the club” ;; 週四) echo “Write homework” ;; 週五) echo “Wash my cloth” ;; 週六) echo “go home” ;; 週日) echo “enjoy the NBA” ;; *) echo “哈..這是什麼日子啊?” esac 我們要將此檔案的檔名放入登入檔: .bash_profile 中 接著可以測試 source .login 或用 ./.profile

  6. Shell Script 練習 檔名:ezdel #delete file interactively #!/bin/sh fname = $1 if test ! –f $fname then echo “There is no file \”$fname\”.” else echo “Do you want to delete \”$fname”\?” read choice if test $choice = y then rm $fname echo \”$fname\” deleted.” else echo \”$fname\” not deleted.” fi fi 想把某一個檔案做刪除時能有更安全的刪除機制,所以請寫一個script檔案來達成這一個目的. 要執行時請打: makex ezdel ezdel 檔名

  7. Shell Script 練習 檔名:myspell 想把spell這個功能加強,也就是說,不僅列出錯誤的字而已, 還要列出在檔案中的哪裡. 要執行時請打: makex myspell myspell 檔名 #improve my spell checker #!/bin/sh echo enter a word for spell check: read fword for word in `spell $fword` do line=`grep –n $word $word` echo “ “ echo “Misspelled word: $word” echo “$line” done

  8. Shell Script 練習 # test integer a and b who is bigger #!/bin/sh A = $1 B = $2 if test $A –gt $B; then echo “$A > $B” else if test $A –lt $B; then echo “$A < $B” else; echo “$A = $B” fi fi 檔名:testab 想知道輸入的二個數字誰大誰小 要執行時請打: makex testab testab 6 8 (後面2個數字可任意 變換) # add number from 1 to 9 #!/bin/sh sum=0 for i in 1 2 3 4 5 6 7 8 9 do sum=`expr $sum + $i` done echo “The sum of 1-9 is: $sum” 檔名:sum-t 要計算1-9的加總. 要執行時請打: makex sum-t sum-t

More Related