GB | BIG5
|
| 首頁 > 編程技術 > Shell > 正文 |
 |
| Linux程式設計-11.Shell Script(bash)--(8)返回狀態Exit |
| http://www.openchess.org/noitatsko/programming/ (2001-05-25 15:00:00) |
在繼續下去之前,我們必須要切入另一個話題,即返回狀態值 - Exit Status。因為if/while/until都遷涉到了使用Exit Status來控制程式流程的問題。
--------------------------------------------------------------------------------
許多人都知道,在許多語言中(C/C++/Perl....),都有一個exit的函數,甚至連Bash自己都有個exit的內建命令。而exit後面所帶的數字,便是返回狀態值 - Exit Status。
返回狀態值可以使得程式與程式之間,利用Shell script來結合的可能性大增,利用小程式,透過Shell script,來完成很雜的工作。
在shell中,返回值為零表示成功(True),非零值為失敗(False)。
--------------------------------------------------------------------------------
舉例來說,以下這個兩個小程式yes/no分別會返回0/1(成功/失敗):
/* yes.c */
void main(void) { exit(0); }
/* no.c */
void main(void) { exit(1); }
那麼以下這個"YES"的shell script便會顯示"YES"。
#!/bin/sh
# YES
if yes ; then
echo "YES"
fi
而"NO"不會顯示任何東西。
#!/bin/sh
# NO
if no ; then
echo "YES"
fi
--------------------------------------------------------------------------------
test express
[ express ]
在Shell script中,test express/[ express ]這個語法被大量地使用,它是個非常實用的指令。由於它的返回值即Exit Status,經常被運用在if/while/until的場合中。而在後面,我們也會大量運用到,在進入介紹if/while/until之前,有必要先解一下。
其返回值為0(True)或1(False),要看表述(express)的結果為何。
express格式
-b file : 當檔案存在並且屬性是Block special(通常是/dev/xxx)時,返回True。
-c file : 當檔案存在並且屬性是character special(通常是/dev/xxx)時,返回True。
-d file : 當檔案存在並且屬性是目錄時,返回True。
-e file : 當檔案存在時,返回True。
-f file : 當檔案存在並且是正常檔案時,返回True。
-g file : 當檔案存在並且是set-group-id時,返回True。
-k file : 當檔案存在並且有"sticky" bit被設定時,返回True。
-L file : 當檔案存在並且是symbolic link時,返回True。
-p file : 當檔案存在並且是name pipe時,返回True。
-r file : 當檔案存在並且可讀取時,返回True。
-s file : 當檔案存在並且檔案大小大於零時,返回True。
-S file : 當檔案存在並且是socket時,返回True。
-t fd : 當fd被開啟為terminal時,返回True。
-u file : 當檔案存在並且set-user-id bit被設定時,返回True。
-w file : 當檔案存在並且可寫入時,返回True。
-x file : 當檔案存在並且可執行時,返回True。
-O file : 當檔案存在並且是被執行的user id所擁有時,返回True。
-G file : 當檔案存在並且是被執行的group id所擁有時,返回True。
file1 -nt file2 : 當file1比file2新時(根據修改時間),返回True。
file1 -ot file2 : 當file1比file2舊時(根據修改時間),返回True。
file1 -ef file2 : 當file1與file2有相同的device及inode number時,返回True。
-z string : 當string的長度為零時,返回True。
-n string : 當string的長度不為零時,返回True。
string1 = string2 : string1與string2相等時,返回True。
string1 != string2 : string1與string2不相等時,返回True。
! express : express為False時,返回True。
expr1 -a expr2 : expr1及expr2為True。
expr1 -o expr2 : expr1或expr2其中之一為True。
arg1 OP arg2 : OP是-eq[equal]、-ne[not-equal]、-lt[less-than]、-le[less-than-or-equal]、-gt[greater-than]、-ge[greater-than-or-equal]的其中之一。
--------------------------------------------------------------------------------
在Bash中,當錯誤發生在致命信號時,bash會返回128+signal number做為返回值。如果找不到命令,將會返回127。如果命令找到了,但該命令是不可執行的,將返回126。除此以外,Bash本身會返回最後一個指令的返回值。若是執行中發生錯誤,將會返回一個非零的值。
Fatal Signal : 128 + signo
Can't not find command : 127
Can't not execute : 126
Shell script successfully executed : return the last command exit status
Fatal during execution : return non-zero
(http://www.fanqiang.com)
進入【UNIX論壇】
|
|
| 相關文章 |
Linux程式設計-31.工作群資訊管理(grp) (2001-05-27 22:08:00) Linux程式設計-30.使用者資訊管理(pwd) (2001-05-27 21:04:00) Linux程式設計-29.時間處理 (2001-05-27 20:10:01) Linux程式設計-28.GNU Make (2001-05-27 19:00:00) Linux程式設計-27.GNU Debugger (2001-05-27 18:08:01) Linux程式設計-26.PIPE (2001-05-27 17:04:00) Linux程式設計-25.Message Queues (2001-05-27 16:10:00) Linux程式設計-24.Semaphores (2001-05-27 15:00:00) Linux程式設計-23.共享記憶體(Shared Memory) (2001-05-27 14:08:00) Linux程式設計-20.getopt (2001-05-27 13:04:00)
|
===更多相關=== |
|
|
 |
★ 樊強制作 歡迎分享 ★ |