shell--脚本调试

参数:

-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  ]
复制代码

 

posted @   头痛不头痛  阅读(389)  评论(0编辑  收藏  举报
编辑推荐:
· 基于Microsoft.Extensions.AI核心库实现RAG应用
· Linux系列:如何用heaptrack跟踪.NET程序的非托管内存泄露
· 开发者必知的日志记录最佳实践
· SQL Server 2025 AI相关能力初探
· Linux系列:如何用 C#调用 C方法造成内存泄露
阅读排行:
· Manus爆火,是硬核还是营销?
· 终于写完轮子一部分:tcp代理 了,记录一下
· 别再用vector<bool>了!Google高级工程师:这可能是STL最大的设计失误
· 单元测试从入门到精通
· 震惊!C++程序真的从main开始吗?99%的程序员都答错了
点击右上角即可分享
微信分享提示
主题色彩