如何在Shell脚本中执行语法检查调试模式

系统 Linux 系统运维
我们开启了 Shell 脚本调试系列文章,先是解释了不同的调试选项,下面介绍如何启用 Shell 调试模式。写完脚本后,建议在运行脚本之前先检查脚本中的语法,而不是查看它们的输出以确认它们是否正常工作。在本系列的这一部分,我们将了解如何使用语法检查调试模式。

我们开启了 Shell 脚本调试系列文章,先是解释了不同的调试选项,下面介绍如何启用 Shell 调试模式。

[[179288]]

写完脚本后,建议在运行脚本之前先检查脚本中的语法,而不是查看它们的输出以确认它们是否正常工作。

在本系列的这一部分,我们将了解如何使用语法检查调试模式。记住我们之前在本系列的***部分中解释了不同的调试选项,在这里,我们将使用它们来执行脚本调试。

启用 verbose 调试模式

在进入本指导的重点之前,让我们简要地探索下 verbose 模式。它可以用 -v 调试选项来启用,它会告诉 shell 在读取时显示每行。

要展示这个如何工作,下面是一个示例脚本来批量将 PNG 图片转换成 JPG 格式。

将下面内容输入(或者复制粘贴)到一个文件中。

  1. #!/bin/bash 
  2. #convert 
  3. for image in *.png; do 
  4. convert  "$image"  "${image%.png}.jpg" 
  5. echo "image $image converted to ${image%.png}.jpg" 
  6. done 
  7. exit 0 

接着保存文件,并用下面的命令使脚本可执行:

  1. $ chmod +x script.sh 

我们可以执行脚本并显示它被 Shell 读取到的每一行:

  1. $ bash -v script.sh  

 

 

 

显示shell脚本中的所有行

在 Shell 脚本中启用语法检查调试模式

回到我们主题的重点,-n 激活语法检查模式。它会让 shell 读取所有的命令,但是不会执行它们,它(shell)只会检查语法。

一旦 shell 脚本中发现有错误,shell 会在终端中输出错误,不然就不会显示任何东西。

激活语法检查的命令如下:

  1. $ bash -n script.sh 

因为脚本中的语法是正确的,上面的命令不会显示任何东西。所以,让我们尝试删除结束 for 循环的 done 来看下是否会显示错误:

下面是修改过的含有 bug 的批量将 png 图片转换成 jpg 格式的脚本。

  1. #!/bin/bash 
  2. #script with a bug 
  3. #convert 
  4. for image in *.png; do 
  5. convert  "$image"  "${image%.png}.jpg" 
  6. echo "image $image converted to ${image%.png}.jpg" 
  7. exit 0 

保存文件,接着运行该脚本并执行语法检查:

  1. $ bash -n script.sh  

 

 

 

检查 shell 脚本语法

从上面的输出中,我们看到我们的脚本中有一个错误,for 循环缺少了一个结束的 done 关键字。shell 脚本从头到尾检查文件,一旦没有找到它(done),shell 会打印出一个语法错误:

  1. script.sh: line 11: syntax error: unexpected end of file 

我们可以同时结合 verbose 模式和语法检查模式:

  1. $ bash -vn script.sh  

 

 

 

在脚本中同时启用 verbose 检查和语法检查

另外,我们可以通过修改脚本的首行来启用脚本检查,如下面的例子:

  1. #!/bin/bash -n 
  2. #altering the first line of a script to enable syntax checking 
  3. #convert 
  4. for image in *.png; do 
  5. convert  "$image"  "${image%.png}.jpg" 
  6. echo "image $image converted to ${image%.png}.jpg" 
  7. exit 0 

如上所示,保存文件并在运行中检查语法:

  1. $ ./script.sh 
  2. script.sh: line 12: syntax error: unexpected end of file 

此外,我们可以用内置的 set 命令来在脚本中启用调试模式。

下面的例子中,我们只检查脚本中的 for 循环语法。

  1. #!/bin/bash 
  2. #using set shell built-in command to enable debugging 
  3. #convert 
  4. #enable debugging 
  5. set -n 
  6. for image in *.png; do 
  7. convert  "$image"  "${image%.png}.jpg" 
  8. echo "image $image converted to ${image%.png}.jpg" 
  9. #disable debugging 
  10. set +n 
  11. exit 0 

再一次保存并执行脚本:

  1. $ ./script.sh  

总的来说,我们应该保证在执行 Shell 脚本之前先检查脚本语法以捕捉错误。

请在下面的反馈栏中,给我们发送关于这篇指导的任何问题或反馈。在这个系列的第三部分中,我们会解释并使用 shell 追踪调试模式。

作者简介: 

 

 

[[179289]]

Aaron Kili 是一个 Linux 及 F.O.S.S 热衷者,即将是 Linux 系统管理员、web 开发者,目前是 TecMint 的内容创作者,他喜欢用电脑工作,并热心分享知识。

责任编辑:庞桂玉 来源: Linux中国
相关推荐

2016-12-16 09:23:29

LinuxShell脚本

2017-01-18 20:38:36

LinuxShell脚本命令

2018-02-01 17:32:30

LinuxUNIXBash Shell

2023-04-04 07:52:26

RedisLua脚本

2021-01-18 17:23:30

代码调试VS Code

2021-04-21 08:03:34

脚本Shell读取

2021-08-20 10:46:25

Shell脚本文件Linux

2021-08-30 07:50:42

脚本语言命令行

2021-02-15 17:29:46

LinuxShell脚本

2023-07-10 12:11:50

TypeScripChrome识别

2022-10-28 16:42:04

Linuxcrontab日志

2009-12-01 09:13:51

shell脚本linux

2022-11-03 08:13:52

echo 命令Linux

2022-11-01 15:38:22

LinuxShell

2009-06-02 10:02:50

eclipse jboeclipse jbojboss for e

2022-01-17 13:34:45

MySQLLinux数据库

2022-10-19 21:24:24

临时表空间Oracle

2020-12-14 06:57:37

shell

2021-03-14 09:28:24

Linux Shell脚本

2022-10-09 10:18:44

LinuxShell脚本
点赞
收藏

51CTO技术栈公众号