- sh -x 脚本名.sh
对整个脚本进行跟踪
[root@master shellexer]# cat bash.sh#!/bin/bashvar=$1echo $var[root@master shellexer]# sh -x bash.sh hello+ var=hello+ echo hello- hello #脚本输出结果
- #带+的表示被跟踪的代码
- set -x
对脚本内部部分代码进行跟踪,被跟踪的代码以set -x开始,与set +x结束
[root@master shellexer]# cat bash.sh#!/bin/bashvar=$1set -xceho $varset +xecho $var[root@master shellexer]# sh bash.sh hello+ ceho hello #带+的表示被跟踪的代码bash.sh: line 4: ceho: command not found+ set +xhello#脚本输出结果
本文介绍了使用Bash脚本时两种不同的调试方法:一种是对整个脚本进行跟踪,另一种则是仅跟踪脚本内的特定部分。通过示例展示了如何利用set -x 和 set +x 命令来开启和关闭代码跟踪,帮助开发者更好地定位和解决问题。
2万+

被折叠的 条评论
为什么被折叠?



