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

本文介绍了Shell脚本调试的基本方法,包括使用-n参数检查语法错误、使用-v参数显示脚本内容并执行、使用-x参数逐行执行并显示内容。此外,还讲解了如何利用PS4变量输出行号以及如何通过set命令控制调试范围。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

参数:

-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

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值