Linux的shell scripts 还有debug功能,你知道吗

这一节主要介绍shell scripts脚本如何debug

root@ubuntu:/tmp# bash [-nvx] sum.sh

选项与参数:

-n :不要执行 script,仅查询语法的问题;

-v :再执行 sccript 前,先将 scripts 的内容输出到屏幕上;

-x :将使用到的 script 内容显示到屏幕上,这是很有用的参数!

示例一: bash -n scripts.sh

模板练习文件:

#!/bin/bash

s=0

i=0

while [ "${i}" != "100" ]

do

i=$(($i+1))

s=$(($s+$i))

done

echo "The result of '1+2+3+...+100' is: $s"

Linux的shell scripts 还有debug功能,你知道吗

bash -n参数使用示例

上图中我把while变为whi,然后执行bash -n sum.sh,会检查出语法错误,如果语法没有任何错误,则不会有任何输出.

示例二: bash -v scripts.sh

Linux的shell scripts 还有debug功能,你知道吗

bash -v使用示例

bash -v功能是在执行脚本之前把脚本内容全部打印出来,然后再输出脚本内容

示例三: bash -x scripts.sh

Linux的shell scripts 还有debug功能,你知道吗

bash -x使用示例

bash -x scripst.sh 是将使用的内容显示出来(记住,时使用到的script语句),这一参数很重要,也很常用

除了参数调试scripts之外,set -x [scripts] set +x也可以起到debug scripts的作用

Linux的shell scripts 还有debug功能,你知道吗

set -x set +x使用示例

执行结果和bash -x scripts.sh是一样的


分享到:


相關文章: