Shell (一) 脚本错误检测
来源:互联网 发布:macbook如何下载淘宝 编辑:程序博客网 时间:2024/05/02 01:57
1、使用set命令来观察脚本运行情况
源码:
#!/bin/shset -xecho -n "Can you write device drivers? "read answeranswer=`echo $answer | tr [a-z] [A-Z]`#echo $answerif [ "$answer" = Y ]then echo "Wow, you must be very skilled"else echo "Neither can I, I'm just an example shell script"fi
运行:
[apache@indigo shell]$ sh example.sh+ echo -n 'Can you write device drivers? 'Can you write device drivers? + read answerY++ echo Y++ tr '[a-z]' '[A-Z]'+ answer=Y+ '[' Y = Y ']'+ echo 'Wow, you must be very skilled'Wow, you must be very skilled[apache@indigo shell]$ sh example.sh+ echo -n 'Can you write device drivers? 'Can you write device drivers? + read answerMM++ echo MM++ tr '[a-z]' '[A-Z]'+ answer=MM+ '[' MM = Y ']'+ echo 'Neither can I, I'\''m just an example shell script'Neither can I, I'm just an example shell script
2、加入可打开和关闭的调试层次
源码:
#!/bin/sh#set -xdebug=$1test $debug -gt 0 && echo "Debug is on"echo -n "Can you write device drivers? "read answertest $debug -gt 0 && echo "The answer is $answer"answer=`echo $answer | tr [a-z] [A-Z]`#echo $answerif [ "$answer" = Y ]then echo "Wow, you must be very skilled" test $debug -gt 0 && echo "then:The answer is $answer"else echo "Neither can I, I'm just an example shell script" test $debug -gt 0 && echo "else:The answer is $answer"fi运行:
[apache@indigo shell]$ sh p6.sh 2Debug is onCan you write device drivers? nThe answer is nNeither can I, I'm just an example shell scriptelse:The answer is N
3、检查命令执行结果
shell自动设置内部变量 $? 值为前一个命令执行结果的返回值,命令的返回值是一个已定义的、记录最近执行命令的退出状态的数字。 0为成功,先来看一下它的作用:[apache@indigo shell]$ command not foundbash: not: command not found[apache@indigo shell]$ echo $?127[apache@indigo shell]$ touch /root/datatouch: cannot touch ‘/root/data’: Permission denied[apache@indigo shell]$ echo $?1[apache@indigo shell]$ cd ..[apache@indigo diary]$ echo $?0[apache@indigo diary]$把这个功能写在函数里,alert.sh:
alert(){ #usage: alert <$?> <object> if [ "$1" -ne 0 ] then echo "WARNING: $2 did not complete successfully." >&2 exit $1 else echo "INFO: $2 completed successfully" >&2 fi}alert $1 $2
试一下:
[apache@indigo shell]$ commandNotExistbash: commandNotExist: command not found[apache@indigo shell]$ ./alert.sh $? "commandNotExist"WARNING: commandNotExist did not complete successfully.[apache@indigo shell]$ touch /root/datatouch: cannot touch ‘/root/data’: Permission denied[apache@indigo shell]$ ./alert.sh $? "touch /root/data"WARNING: touch did not complete successfully.[apache@indigo shell]$ mkdir data[apache@indigo shell]$ ./alert.sh $? "mkdir data"INFO: mkdir completed successfully
[以上脚本来自<Shell脚本专家指南>]
- Shell (一) 脚本错误检测
- shell脚本---回文检测
- shell 脚本 检测文件夹
- 记录一开机网络配置检测脚本(shell)
- Shell脚本检测URL可用性
- shell脚本错误总结
- shell 脚本执行错误
- 执行shell脚本错误
- shell脚本---mysql检测监控脚本系列
- Shell脚本学习一
- shell脚本学习《一》
- shell脚本学习 一
- Shell脚本一枚
- 学习Shell脚本[一]
- shell脚本(一)
- 学习shell脚本(一)
- shell脚本编程(一)
- shell脚本学习一
- spring JDBC事务使用经验
- strncasecmp函数--比较字符串,忽略大小写差异+sscanf格式选择存储字符串+atoi函数
- MongoDB学习之五----空间操作
- bat启动软件例子
- dnsmasq配置
- Shell (一) 脚本错误检测
- Nginx---父子进程通信
- c++primer 课后习题个人解答 part1 4.3
- Struts2保存更新程序走完数据库不更新
- MFC中多线程编程方法
- Android Activity和Intent机制学习笔记
- 【hadoop】Hadoop学习笔记(一)之示例程序:计算每年的最高温度MaxTemperature
- RedHat和CentOS使用本地yum源配置
- CentOS下SVN服务器的搭建使用