Changes between Version 12 and Version 13 of YM_Course_2009/Lab4


Ignore:
Timestamp:
Jul 4, 2009, 12:03:50 PM (16 years ago)
Author:
jazz
Comment:

--

Legend:

Unmodified
Added
Removed
Modified
  • YM_Course_2009/Lab4

    v12 v13  
    160160= 3. test 與 Shell Script =
    161161
    162  * 在學習寫 Shell Script 之前,可以先學著用 test 指令來確認自己寫的判斷式正不正確。
     162 * 在學習寫 Shell Script 之前,可以先學著用 test 指令來確認自己寫的判斷式正不正確。在 Bash 環境中,變數 $? 代表上一次執行命令的結果,若為 0 代表成功/條件成立/真;若為 1 代表失敗/條件不成立/假。
    163163{{{
    164164ubuntu@ubuntu:~$ test -e file
     
    1661660
    167167ubuntu@ubuntu:~$ test -e file0
     168ubuntu@ubuntu:~$ echo $?
     1691
     170}}}
     171 * 同樣的動作也可以用 [] 或 [[]] 來完成
     172{{{
     173ubuntu@ubuntu:~$ [ -e file ]
     174ubuntu@ubuntu:~$ echo $?
     1750
     176ubuntu@ubuntu:~$ [ -e file0 ]
    168177ubuntu@ubuntu:~$ echo $?
    1691781
     
    174183if [ 條件一 ]; then
    175184  若條件一為成立(真,0)時,要做的動作
    176 elif [ 條件二 ]
     185elif [ 條件二 ]; then
    177186  若條件二為成立(真,0)時,要做的動作
    178187else
     
    180189fi
    181190}}}
     191 * 範例:
     192{{{
     193ubuntu@ubuntu:~$ if [ -e file ]; then echo "file 存在"; fi
     194file 存在
     195ubuntu@ubuntu:~$ if [ -e file0 ]; then echo "file0 存在"; else echo "file0 不存 在"; fi
     196file0 不存在
     197ubuntu@ubuntu:~$ if [ -e file ]; then echo "file 存在"; elif [ -e file0 ]; then echo "file0 存在"; else echo "file0 不存在"; fi
     198file 存在
     199ubuntu@ubuntu:~$ if [ -e file0 ]; then echo "file0 存在"; elif [ -e file ]; then echo "file 存在"; else echo "file, file0 均不存在"; fi
     200file 存在
     201ubuntu@ubuntu:~$ if [ -e file0 ]; then echo "file0 存在"; elif [ -e fileA ]; then echo "fileA 存在"; else echo "file0, fileA 均不存在"; fi
     202file0, fileA 均不存在
     203}}}