shell脚本学习总结11--脚本调试

参数:

-n    不执行脚本,仅检查语法是否错误

-v    将脚本内容输出到屏幕上,然后执行脚本

-x   执行脚本,并将内容输出到屏幕

-n

[root@new sbin]# sh -n debug.sh 
[root@new sbin]#

-v

[root@new sbin]# sh -v debug.sh 10
module () {  eval `/usr/bin/modulecmd bash $*`
}
#/bin/bash
if [ $1 -gt 5 ];then
   echo "yes"
else 
   echo "no"
fi
yes

-x

[root@new sbin]# sh -x debug.sh 
+ '[' -gt 5 ']'
debug.sh: line 2: [: -gt: unary operator expected
+ echo no
no

拓展:

利用PS4,使调试时输出行号

[root@new sbin]# echo $PS4
+
[root@new sbin]# export PS4='+{$LINENO}'[root@new sbin]# sh -x debug.sh 4
+{2}'[' 4 -gt 5 ']'
+{5}echo no
no

利用set ,缩小调试作用域

set -x 开启调试功能

set +x  关闭调试功能

[root@new sbin]# cat demo.sh 
#/bin/bash
. /etc/init.d/functions
set -x
read -p "Pleas input your anwser[yes/no]: " an
set +x
if [[ $an = yes ]]
then 
   action "The answer is true" /bin/true
else 
   action "Then answer is false" /bin/false
fi
[root@new sbin]# sh demo.sh 
+{4}read -p 'Pleas input your anwser[yes/no]: ' an
Pleas input your anwser[yes/no]: yes
+{5}set +x
The answer is true                                         [  OK  ]
原文地址:https://www.cnblogs.com/zydev/p/5791755.html