next up previous contents
Next: 判斷符號 [ ] Up: 條件判斷 Previous: test 命令   Contents

test 命令使用

  1. test -f 測試檔案 sh03.sh 及 sh0y.sh 是否存在?
    [dywang@dywmac zzz]$ test -e sh03.sh && echo "exist" || echo "not exist"
    exist
    [dywang@dywmac zzz]$ test -e sh0y.sh && echo "exist" || echo "not exist"
    not exist
    
  2. sh05.sh 從鍵盤輸人一個數字,test 測試這個數字是否大於 10。
    [dywang@dywmac zzz]$ vim sh05.sh 
    [dywang@dywmac zzz]$ cat sh05.sh
    #!/bin/bash
    read -p "請輸入一數字: " -t 5 num
    
    test "$num" -gt "10" && echo "$num>10" || echo "$num<=10"
    exit 0
    
  3. 執行 sh05.sh。
    [dywang@dywmac zzz]$ sh sh05.sh
    請輸入一數字: 10
    10<=10
    [dywang@dywmac zzz]$ sh sh05.sh
    請輸入一數字: 45
    45>10
    [dywang@dywmac zzz]$ sh sh05.sh
    請輸入一數字: -3
    -3<=10
    



De-Yu Wang 2019-08-12