基礎指令學習目錄首頁
原文連結:https://www.cnblogs.com/itcomputer/p/4157859.html
用途說明
exit指令用于退出目前shell,在shell腳本中可以終止目前腳本執行。
常用參數
格式:exit n
退出。設定退出碼為n。(Cause the shell to exit with a status of n.)
格式:exit
退出。退出碼不變,即為最後一個指令的退出碼。(If n is omitted, the exit status is that of the last command executed. )
格式:$?
上一個指令的退出碼。
格式:trap "commands" EXIT
退出時執行commands指定的指令。( A trap on EXIT is executed before the shell terminates.)
退出碼(exit status,或exit code)的約定:
0表示成功(Zero - Success)
非0表示失敗(Non-Zero - Failure)
2表示用法不當(Incorrect Usage)
127表示指令沒有找到(Command Not Found)
126表示不是可執行的(Not an executable)
>=128 信号産生
man 3 exit 寫道
The C standard specifies two constants, EXIT_SUCCESS and EXIT_FAILURE, that may be passed to exit() to indicate successful or unsuccessful termination, respectively.
以下摘自/usr/include/stdlib.h
1 2 | |
BSD試圖對退出碼标準化。
BSD has attempted to standardize exit codes; see the file <sysexits.h>.
以下摘自/usr/include/sysexits.h
3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 | |
使用示例
示例一 退出目前shell
[root@new55 ~]# [root@new55 ~]# exit logout
示例二 在腳本中,進入腳本所在目錄,否則退出
Bash代碼
|
示例三 在腳本中,判斷參數數量,不比對就列印使用方式,退出
|
示例四 在腳本中,退出時删除臨時檔案
|
示例五 檢查上一指令的退出碼
|
功能說明:退出目前的shell。
語 法:exit [狀态值]
補充說明:執行exit可使shell以指定的狀态值退出。若不設定狀态值參數,則shell以預設值退出。狀态值0代表執行成功,其他值代表執行失敗。exit也可用在script,離開正在執行的script,回到shell。