Bash脚本编程 (二):Bash Shell的退出状态
2010-05-28 20:51
453 查看
〖正天时代〗HTC Touch2/T3333◆原厂原配 | |
1690.0元 |
Bash Shell的退出状态 |
命令就是一组单词序列的组合。第一个单词是将被执行的命令,剩下的单词将作为参数传递给这个命令。在这里参数 (arguments) 可以是选项 (options) ,也可以是参数值 (parameters) 。
在命令行中执行的命令有些是 shell 命令,比如 ls,lpr 以及 grep 命令。
$ ls -alF $ lpr filename $ grep "string" filename |
命令的返回值就是它的退出状态,如果该命令被信号 n 终止,那么它的退出状态就是 128+n 。退出状态用来检查命令是否被成功执行。如果状态为 0 ,那么该命令执行成功。如果该命令执行失败,那么退出状态为非零。
Exit Value | Exit Status |
0(Zero) | Success |
Non-zero | Failure |
2 | Incorrect usage |
127 | Command Not Found |
126 | Not an executable |
Shell变量$?是一个特殊的内建变量,它存储的是最后一个被执行的命令的退出状态。
1. 当shell函数执行后,$?返回函数体内最后一个命令的退出状态。
2. 当shell脚本执行后,$?返回脚本内最后一个命令的退出状态。
Shell命令退出状态实例
下面的exitstatus.sh脚本显示不同shell命令的退出状态。
$ cat exitstatus.sh #! /bin/bash echo -e "Successful execution" echo -e "=====================" echo "hello world" # Exit status returns 0, because the above command is a success. echo "Exit status" $? echo -e "Incorrect usage" echo -e "=====================" ls --option # Incorrect usage, so exit status will be 2. echo "Exit status" $? echo -e "Command Not found" echo -e "=====================" bashscript # Exit status returns 127, because bashscript command not found echo "Exit status" $? echo -e "Command is not an executable" echo -e "=============================" ls -l execution.sh ./execution.sh # Exit status returns 126, because its not an executable. echo "Exit status" $? |
$ bash exitstatus.sh Successful execution ===================== hello world Exit status 0 Incorrect usage ===================== ls: unrecognized option `--option' Try `ls --help' for more information. Exit status 2 Command Not found ===================== exitstaus.sh: line 15: bashscript: command not found Exit status 127 Command is not an executable ============================= -rw-r--r-- 1 root root 659 Mar 9 13:36 execution.sh exitstatus.sh: line 21: ./execution.sh: Permission denied Exit status 126 |
〖正天时代〗HTC Touch2/T3333◆原厂原配 | |
1690.0元 |
相关文章推荐
- shell退出和退出状态学习笔记
- 一个通用的调用shell命令及打印退出状态的C语言实现
- shell学习二十七天----退出状态和if语句
- shell脚本中函数调用的返回值,以及退出状态问题
- shell退出状态
- SHELL学习——退出状态、测试(整数\字符串\ 文件\逻辑运算符)
- bash 退出状态值
- Linux Shell Bash 带有特殊含义的退出码
- shell退出状态
- shell-6-退出状态与流程控制语句(if;test;case命令)
- shell脚本中函数调用的返回值,以及退出状态问题
- Shell -退出状态
- newlisp获取bash命令的退出状态
- Linux Shell Bash 带有特殊含义的退出码
- shell同时启多个进程,并等待所有子进程结束,再做接下来的操作,最好能获取每个子进程的退出状态
- Linux Shell学习笔记:exit退出状态代码
- Linux命令之exit - 退出当前shell【返回值状态】
- shell退出码(退出状态)
- Linux命令之exit - 退出当前shell【返回值状态】
- 【Unix/Linux.Shell Script】退出状态,exit命令