日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問(wèn) 生活随笔!

生活随笔

當(dāng)前位置: 首頁(yè) > 编程资源 > 综合教程 >内容正文

综合教程

Linux命令之exit

發(fā)布時(shí)間:2024/2/2 综合教程 28 生活家
生活随笔 收集整理的這篇文章主要介紹了 Linux命令之exit 小編覺(jué)得挺不錯(cuò)的,現(xiàn)在分享給大家,幫大家做個(gè)參考.

用途說(shuō)明

exit命令用于退出當(dāng)前shell,在shell腳本中可以終止當(dāng)前腳本執(zhí)行。

常用參數(shù)

格式:exit n

退出。設(shè)置退出碼為n。(Cause the shell to exit with a status of n.)

格式:exit

退出。退出碼不變,即為最后一個(gè)命令的退出碼。(If n is omitted, the exit status is that of the last command executed. )

格式:$?

上一個(gè)命令的退出碼。

格式:trap "commands" EXIT

退出時(shí)執(zhí)行commands指定的命令。( A trap on EXIT is executed before the shell terminates.)

退出碼(exit status,或exit code)的約定:

0表示成功(Zero - Success)

非0表示失敗(Non-Zero - Failure)

2表示用法不當(dāng)(Incorrect Usage)

127表示命令沒(méi)有找到(Command Not Found)

126表示不是可執(zhí)行的(Not an executable)

>=128 信號(hào)產(chǎn)生

man 3 exit 寫(xiě)道
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

#define EXIT_FAILURE    1       /* Failing exit status.  */
#define EXIT_SUCCESS    0       /* Successful exit status.  */

BSD試圖對(duì)退出碼標(biāo)準(zhǔn)化。

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

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

#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 */

  

使用示例

示例一 退出當(dāng)前shell

[root@new55 ~]# [root@new55 ~]# exit logout

示例二 在腳本中,進(jìn)入腳本所在目錄,否則退出

Bash代碼

cd $(dirname $0) || exit 1

示例三 在腳本中,判斷參數(shù)數(shù)量,不匹配就打印使用方式,退出

if [ "$#" -ne "2" ]; then
    echo "usage: $0 <area> <hours>"
    exit 2
fi

示例四 在腳本中,退出時(shí)刪除臨時(shí)文件

trap "rm -f tmpfile; echo Bye." EXIT

示例五 檢查上一命令的退出碼

./mycommand.sh
EXCODE=$?
if [ "$EXCODE" == "0" ]; then
    echo "O.K"
fi

問(wèn)題思考

相關(guān)資料

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

總結(jié)

以上是生活随笔為你收集整理的Linux命令之exit的全部?jī)?nèi)容,希望文章能夠幫你解決所遇到的問(wèn)題。

如果覺(jué)得生活随笔網(wǎng)站內(nèi)容還不錯(cuò),歡迎將生活随笔推薦給好友。