[linux device driver]Chapter 03:scull_load中涉及到的bash命令 exit
来源:互联网 发布:泰国评论上瘾网络剧 编辑:程序博客网 时间:2024/06/04 07:24
用途说明
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 信号产生
successful or unsuccessful termination, respectively.
以下摘自/usr/include/stdlib.h
- #define EXIT_FAILURE 1 /* Failing exit status. */
- #define EXIT_SUCCESS 0 /* Successful exit status. */
BSD试图对退出码标准化。
以下摘自/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 */
使用示例
示例一 退出当前shell
[root@new55 ~]#
[root@new55 ~]# exit
logout
示例二 在脚本中,进入脚本所在目录,否则退出
- cd $(dirname $0) || exit 1
示例三 在脚本中,判断参数数量,不匹配就打印使用方式,退出
- if [ "$#" -ne "2" ]; then
- echo "usage: $0 <area> <hours>"
- exit 2
- fi
示例四 在脚本中,退出时删除临时文件
- trap "rm -f tmpfile; echo Bye." EXIT
示例五 检查上一命令的退出码
- ./mycommand.sh
- EXCODE=$?
- if [ "$EXCODE" == "0" ]; then
- echo "O.K"
- fi
The exit command terminates a script, just as in a C program. It can also return a value, which is available to the script's parent process.
Every command returns an exit status (sometimes referred to as a return status or exit code). A successful command returns a 0, while an unsuccessful one returns a non-zerovalue that usually can be interpreted as an error code. Well-behaved UNIX commands, programs, and utilities return a 0 exit code upon successful completion, though there are some exceptions.
Likewise, functions within a script and the script itself return an exit status. The last command executed in the function or script determines the exit status. Within a script, anexit nnn command may be used to deliver an nnn exit status to the shell (nnn must be an integer in the 0 - 255 range).
When a script ends with an exit that has no parameter, the exit status of the script is the exit status of the last command executed in the script (previous to theexit).
#!/bin/bashCOMMAND_1. . .COMMAND_LAST# Will exit with status of last command.exit
The equivalent of a bare exit is exit $? or even just omitting the exit.
#!/bin/bashCOMMAND_1. . .COMMAND_LAST# Will exit with status of last command.exit $?
#!/bin/bashCOMMAND1. . . COMMAND_LAST# Will exit with status of last command.
$? reads the exit status of the last command executed. After a function returns, $? gives the exit status of the last command executed in the function. This is Bash's way of giving functions a "return value." [1]
Following the execution of a pipe, a $? gives the exit status of the last command executed.
After a script terminates, a $? from the command-line gives the exit status of the script, that is, the last command executed in the script, which is, by convention, 0 on success or an integer in the range 1 - 255 on error.
Example 6-1. exit / exit status
#!/bin/bashecho helloecho $? # Exit status 0 returned because command executed successfully.lskdf # Unrecognized command.echo $? # Non-zero exit status returned -- command failed to execute.echoexit 113 # Will return 113 to shell. # To verify this, type "echo $?" after script terminates.# By convention, an 'exit 0' indicates success,#+ while a non-zero exit value means an error or anomalous condition.# See the "Exit Codes With Special Meanings" appendix.
$? is especially useful for testing the result of a command in a script (see Example 16-35and Example 16-20).
The !, the logical not qualifier, reverses the outcome of a test or command, and this affects its exit status.
Example 6-2. Negating a condition using !
true # The "true" builtin.echo "exit status of \"true\" = $?" # 0! trueecho "exit status of \"! true\" = $?" # 1# Note that the "!" needs a space between it and the command.# !true leads to a "command not found" error## The '!' operator prefixing a command invokes the Bash history mechanism.true!true# No error this time, but no negation either.# It just repeats the previous command (true).# =========================================================== ## Preceding a _pipe_ with ! inverts the exit status returned.ls | bogus_command # bash: bogus_command: command not foundecho $? # 127! ls | bogus_command # bash: bogus_command: command not foundecho $? # 0# Note that the ! does not change the execution of the pipe.# Only the exit status changes.# =========================================================== ## Thanks, Stéphane Chazelas and Kristopher Newsome.
Certain exit status codes have reserved meanings and should not be user-specified in a script.
- [linux device driver]Chapter 03:scull_load中涉及到的bash命令 exit
- [linux device driver]Chapter 03:Scull_load中涉及到的群组权限之staff\wheel
- [linux device driver] Chapter 03:以scull_load为例,讲解bash中反单引号`、双引号等的作用
- [linux device driver] Chapter 03:以scull_load为引,详解Bash的位置参数和内部参数
- [linux device driver] Chapter 03:scull_load脚本理解
- [linux device driver] Chapter 03:从scull_load了解awk
- [linux device driver] Chapter 03:以scull_load为例,详解mknod命令
- [linux device driver] Chapter 03:从scull_load学习小括号,中括号,和大括号的区别
- [linux device driver] Chapter 03:以scull_load为引,详解shell逻辑运算、基本运算、位运算
- [linux device driver] Chapter 03:IS_ERR的理解
- [linux device driver] Chapter 03:scullc_open理解
- [linux device driver] Chapter 14:kobject_init的理解
- [linux device driver] Chapter 03:字符设备注册学习
- [linux device driver] Chapter 03:make输出重定向
- [linux device driver] Chapter 04:Magic SysRq Key
- [linux device driver] Chapter 04:使用/proc文件系统查询调试
- Linux device driver 的概念
- Linux device driver 的概念
- 如何通过编程发现Java死锁
- Java多线程-工具篇-BlockingQueue
- 循序渐进做项目系列:最简单的C/S程序——让服务器来做加法
- Android异步加载全解析之使用多线程
- 欧几里德与扩展欧几里德算法
- [linux device driver]Chapter 03:scull_load中涉及到的bash命令 exit
- 关于字典转模型中遇到的问题
- 在大公司和小公司做产品经理有哪些异同点
- 痛苦的挣扎--msp430g2553我恨你!
- oracle 11g 安装详解
- C++ 智能指针详解
- QMUIUtil
- Hadoop学习笔记2--MapReduce编程模型
- 并查集基础