close
Warning:
Can't synchronize with repository "(default)" (Unsupported version control system "svn": /usr/lib/python2.7/dist-packages/libsvn/_repos.so: failed to map segment from shared object: Cannot allocate memory). Look in the Trac log for more information.
- Timestamp:
-
Jul 4, 2009, 12:03:50 PM (16 years ago)
- Author:
-
jazz
- Comment:
-
--
Legend:
- Unmodified
- Added
- Removed
- Modified
-
v12
|
v13
|
|
160 | 160 | = 3. test 與 Shell Script = |
161 | 161 | |
162 | | * 在學習寫 Shell Script 之前,可以先學著用 test 指令來確認自己寫的判斷式正不正確。 |
| 162 | * 在學習寫 Shell Script 之前,可以先學著用 test 指令來確認自己寫的判斷式正不正確。在 Bash 環境中,變數 $? 代表上一次執行命令的結果,若為 0 代表成功/條件成立/真;若為 1 代表失敗/條件不成立/假。 |
163 | 163 | {{{ |
164 | 164 | ubuntu@ubuntu:~$ test -e file |
… |
… |
|
166 | 166 | 0 |
167 | 167 | ubuntu@ubuntu:~$ test -e file0 |
| 168 | ubuntu@ubuntu:~$ echo $? |
| 169 | 1 |
| 170 | }}} |
| 171 | * 同樣的動作也可以用 [] 或 [[]] 來完成 |
| 172 | {{{ |
| 173 | ubuntu@ubuntu:~$ [ -e file ] |
| 174 | ubuntu@ubuntu:~$ echo $? |
| 175 | 0 |
| 176 | ubuntu@ubuntu:~$ [ -e file0 ] |
168 | 177 | ubuntu@ubuntu:~$ echo $? |
169 | 178 | 1 |
… |
… |
|
174 | 183 | if [ 條件一 ]; then |
175 | 184 | 若條件一為成立(真,0)時,要做的動作 |
176 | | elif [ 條件二 ] |
| 185 | elif [ 條件二 ]; then |
177 | 186 | 若條件二為成立(真,0)時,要做的動作 |
178 | 187 | else |
… |
… |
|
180 | 189 | fi |
181 | 190 | }}} |
| 191 | * 範例: |
| 192 | {{{ |
| 193 | ubuntu@ubuntu:~$ if [ -e file ]; then echo "file 存在"; fi |
| 194 | file 存在 |
| 195 | ubuntu@ubuntu:~$ if [ -e file0 ]; then echo "file0 存在"; else echo "file0 不存 在"; fi |
| 196 | file0 不存在 |
| 197 | ubuntu@ubuntu:~$ if [ -e file ]; then echo "file 存在"; elif [ -e file0 ]; then echo "file0 存在"; else echo "file0 不存在"; fi |
| 198 | file 存在 |
| 199 | ubuntu@ubuntu:~$ if [ -e file0 ]; then echo "file0 存在"; elif [ -e file ]; then echo "file 存在"; else echo "file, file0 均不存在"; fi |
| 200 | file 存在 |
| 201 | ubuntu@ubuntu:~$ if [ -e file0 ]; then echo "file0 存在"; elif [ -e fileA ]; then echo "fileA 存在"; else echo "file0, fileA 均不存在"; fi |
| 202 | file0, fileA 均不存在 |
| 203 | }}} |