linux中的shell脚本语法检查 – 知识蚂蚁 您所在的位置:网站首页 linux测试脚本命令 linux中的shell脚本语法检查 – 知识蚂蚁

linux中的shell脚本语法检查 – 知识蚂蚁

#linux中的shell脚本语法检查 – 知识蚂蚁| 来源: 网络整理| 查看: 265

编写好shell后,在执行之前最好先进行下语法检查(可不是通过肉眼一行行去看哦),当然是通过shell命令来检查了。

bash -n script_name.sh -n选项只做语法检查,而不执行脚本。

下面新建一个t.sh文件来做个演示

1 #!/bin/bash 2 3 set -x 4 5 a=1 6 while [[ $a -le 5]]; 7 do 8 echo $a 9 let "a+=1" 10 done

上面的shell脚本第6行有一个错误,通过shell脚本的语法检查命令可以得到下面输出

# bash -n t.sh t.sh: line 6: syntax error in conditional expression: unexpected token `;' t.sh: line 6: syntax error near `;' t.sh: line 6: `while [[ $a -le 5]]; '

发现时少了一个空格导致的,修改后再执行bash -n t.sh没有任何输出,表示没有语法错误了。

1 #!/bin/bash 2 3 set -x 4 5 a=1 6 while [[ $a -le 5 ]]; 7 do 8 echo $a 9 let "a+=1" 10 done

转载请注明:知识蚂蚁 » linux中的shell脚本语法检查



【本文地址】

公司简介

联系我们

今日新闻

    推荐新闻

    专题文章
      CopyRight 2018-2019 实验室设备网 版权所有