shell脚本专家指南笔记----shell错误调试

来源:互联网 发布:维斯布鲁克体测数据 编辑:程序博客网 时间:2024/05/16 10:39

shell脚本专家指南

自己看书的笔记,要了解详情,可以看原著《shell脚本专家指南》

SHELL 跟踪选项

一: set
 1.set -x(xtrace):
  显示脚本运行时的冗余输出,每行代码的拓展命令和变量。 
  每一行都会加上加号(+),提示它是输出跟踪标志
  子shell中会加两个加号"++"
 2.set -v:
  将脚本代码输出
 实例代码:
  #!/bin/sh
  set -x
  echo -n  "Can you write device drivers?"
  read answer
  answer=`echo $answer |tr [a-z] [A-Z]`
  if [ $answer = Y ]
  then
   echo "Wow,you must be very skilled"
  else
   echo "Neither can I,I'm just a example shell script"
  fi

 3.逐步调试:对程序进行分段跟踪
  使用如 -x 减号来打开一个选项
  使用   +x 加号来关闭某个选项
  set -x
  if [ $answer = Y ]
  set +x
二:echo
 类似程序打印日志。缺点:不想输出的时候不得不注释或者删除这些指令。
 改进:
 加入可以打开或者关闭的调试层次来实现。
 下面如果debug>0就输出后续
  #!/bin/sh
  debug=1
  test $debug -gt 0 && echo "Debug is on"
  echo -n  "Can you write device drivers?"

原创粉丝点击