Linux命令之exit - 退出當前shell【傳回值狀態】

來源:互聯網
上載者:User

標籤:des   style   blog   http   io   ar   color   os   使用   

原文連結:http://codingstandards.iteye.com/blog/836625   (轉載請註明出處)

 

用途說明

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

C代碼
  1. #define EXIT_FAILURE    1       /* Failing exit status.  */ 
  2. #define EXIT_SUCCESS    0       /* Successful exit status.  */ 
#define EXIT_FAILURE    1       /* Failing exit status.  */#define EXIT_SUCCESS    0       /* Successful exit status.  */

 

BSD試圖對退出碼標準化。

man 3 exit 寫道BSD has attempted to standardize exit codes; see the file <sysexits.h>.

 

以下摘自/usr/include/sysexits.h

C代碼
  1. #define EX_OK           0       /* successful termination */ 
  2.  
  3. #define EX__BASE        64      /* base value for error messages */ 
  4.  
  5. #define EX_USAGE        64      /* command line usage error */ 
  6. #define EX_DATAERR      65      /* data format error */ 
  7. #define EX_NOINPUT      66      /* cannot open input */ 
  8. #define EX_NOUSER       67      /* addressee unknown */ 
  9. #define EX_NOHOST       68      /* host name unknown */ 
  10. #define EX_UNAVAILABLE  69      /* service unavailable */ 
  11. #define EX_SOFTWARE     70      /* internal software error */ 
  12. #define EX_OSERR        71      /* system error (e.g., can‘t fork) */ 
  13. #define EX_OSFILE       72      /* critical OS file missing */ 
  14. #define EX_CANTCREAT    73      /* can‘t create (user) output file */ 
  15. #define EX_IOERR        74      /* input/output error */ 
  16. #define EX_TEMPFAIL     75      /* temp failure; user is invited to retry */ 
  17. #define EX_PROTOCOL     76      /* remote error in protocol */ 
  18. #define EX_NOPERM       77      /* permission denied */ 
  19. #define EX_CONFIG       78      /* configuration error */ 
  20.  
  21. #define EX__MAX 78      /* maximum listed value */ 
#define EX_OK           0       /* successful termination */#define EX__BASE        64      /* base value for error messages */#define EX_USAGE        64      /* command line usage error */#define EX_DATAERR      65      /* data format error */#define EX_NOINPUT      66      /* cannot open input */#define EX_NOUSER       67      /* addressee unknown */#define EX_NOHOST       68      /* host name unknown */#define EX_UNAVAILABLE  69      /* service unavailable */#define EX_SOFTWARE     70      /* internal software error */#define EX_OSERR        71      /* system error (e.g., can‘t fork) */#define EX_OSFILE       72      /* critical OS file missing */#define EX_CANTCREAT    73      /* can‘t create (user) output file */#define EX_IOERR        74      /* input/output error */#define EX_TEMPFAIL     75      /* temp failure; user is invited to retry */#define EX_PROTOCOL     76      /* remote error in protocol */#define EX_NOPERM       77      /* permission denied */#define EX_CONFIG       78      /* configuration error */#define EX__MAX 78      /* maximum listed value */
使用樣本樣本一 退出當前shell

[[email protected] ~]# [[email protected] ~]# exit logout

 

樣本二 在指令碼中,進入指令碼所在目錄,否則退出Bash代碼
  1. cd $(dirname $0) || exit 1 
cd $(dirname $0) || exit 1

 

樣本三 在指令碼中,判斷參數數量,不匹配就列印使用方式,退出Bash代碼
  1. if [ "$#" -ne "2" ]; then 
  2.     echo "usage: $0 <area> <hours>" 
  3.     exit 2 
  4. fi 
if [ "$#" -ne "2" ]; then    echo "usage: $0 <area> <hours>"    exit 2fi

 

樣本四 在指令碼中,退出時刪除臨時檔案Bash代碼
  1. trap "rm -f tmpfile; echo Bye." EXIT 
trap "rm -f tmpfile; echo Bye." EXIT

 

樣本五 檢查上一命令的退出碼Bash代碼
  1. ./mycommand.sh 
  2. EXCODE=$? 
  3. if [ "$EXCODE" == "0" ]; then 
  4.     echo "O.K" 
  5. fi 
./mycommand.shEXCODE=$?if [ "$EXCODE" == "0" ]; then    echo "O.K"fi
問題思考相關資料

【1】91linux    Linux exit 命令    【2】曲徑通幽   [概念]exit n    【3】Linux大學  Bash Shell Exit Status Tutorial with Practical Examples  

Linux命令之exit - 退出當前shell【傳回值狀態】

相關文章

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.