translated

This commit is contained in:
geekpi 2016-12-16 11:36:17 +08:00
parent 73b5dadde4
commit 44e184e511
2 changed files with 170 additions and 172 deletions

View File

@ -1,172 +0,0 @@
translating---geekpi
How to Perform Syntax Checking Debugging Mode in Shell Scripts
============================================================
We started the shell script debugging series by explaining the different debugging options and [how to enable shell script debugging modes][1].
After writing your shell scripts, it is recommended that we practically check the syntax in the scripts before running them, as opposed to looking at their output to confirm that they are working correctly.
In this part of the series, we will go through how to use syntax checking debugging mode. Remember we explained the different debugging options in the first part of this series and here, we will use them to perform script debugging.
#### Enabling Verbose Debugging Mode
Before we move to the primary focus of this guide, let us briefly explore the verbose mode. It is enabled by the `-v` debugging option, which tells the shell to display all lines in a script while they are read.
To demonstrate how this works, below is a sample shell script to [batch convert PNG images to JPG format][2].
Type ( or copy and paste) it in a file.
```
#!/bin/bash
#convert
for image in *.png; do
convert "$image" "${image%.png}.jpg"
echo "image $image converted to ${image%.png}.jpg"
done
exit 0
```
Then save the file and make the script executable using the command below:
```
$ chmod +x script.sh
```
We can invoke the script and display all the lines in it as they are read by the shell like so:
```
$ bash -v script.sh
```
[
![Display All Lines in Shell Script](http://www.tecmint.com/wp-content/uploads/2016/12/Show-Shell-Script-Lines.png)
][3]
Display All Lines in Shell Script
#### Enabling Syntax Checking Debugging Mode in Shell Scripts
Coming back to our topic of emphasis, The `-n` activates syntax checking mode. It instructs the shell to basically read all the commands, however doesnt execute them, it (shell) only examines the syntax used.
In case there are errors in your shell script, the shell will output the errors on the terminal, otherwise, it displays nothing.
The syntax for activating syntax checking is as follows:
```
$ bash -n script.sh
```
Because the syntax in the script is correct, the command above will not display any output. Therefore, let us try to remove the `done` word that closes the for loop and see if it shows an error:
Below is the modified shell script to batch convert png images to jpg format that contains a bug.
```
#!/bin/bash
#script with a bug
#convert
for image in *.png; do
convert "$image" "${image%.png}.jpg"
echo "image $image converted to ${image%.png}.jpg"
exit 0
```
Save the file, then run it while performing syntax checking in it:
```
$ bash -n script.sh
```
[
![Check Syntax in Shell Script](http://www.tecmint.com/wp-content/uploads/2016/12/Check-Syntax-in-Shell-Script.png)
][4]
Check Syntax in Shell Script
From the output above, we can see that there is a syntax problem with our script, the for loop is missing a closing `done` keyword word. And the shell looked for it up to the end of the file and once it did not find it (done), the shell printed a syntax error:
```
script.sh: line 11: syntax error: unexpected end of file
```
We can as well combine the verbose mode and syntax checking mode together:
```
$ bash -vn script.sh
```
[
![Enable Verbose and Syntax Checking in Script](http://www.tecmint.com/wp-content/uploads/2016/12/Enable-Verbose-and-Syntax-Checking-in-Script.png)
][5]
Enable Verbose and Syntax Checking in Script
Alternatively, we can enable syntax checking by modifying the first line of the script above as in the next example.
```
#!/bin/bash -n
#altering the first line of a script to enable syntax checking
#convert
for image in *.png; do
convert "$image" "${image%.png}.jpg"
echo "image $image converted to ${image%.png}.jpg"
exit 0
```
As before, save the file and run it while performing syntax checking:
```
$ ./script.sh
script.sh: line 12: syntax error: unexpected end of file
```
In addition, we can employ the set shell built-in command to enable debugging mode in the script above.
In the example below, we are only checking the syntax of the for loop in our script.
```
#!/bin/bash
#using set shell built-in command to enable debugging
#convert
#enable debugging
set -n
for image in *.png; do
convert "$image" "${image%.png}.jpg"
echo "image $image converted to ${image%.png}.jpg"
#disable debugging
set +n
exit 0
```
Once again, save the file and invoke the script:
```
$ ./script.sh
```
In summary, we should always ensure that we syntactically check our shell scripts to capture any error before executing them.
To send us any questions or feedback concerning this guide, use the response form below. In the third part of this series, we shall move to explaining and using shell tracing debugging mode.
--------------------------------------------------------------------------------
作者简介:
![](http://1.gravatar.com/avatar/4e444ab611c7b8c7bcb76e58d2e82ae0?s=128&d=blank&r=g)
Aaron Kili is a Linux and F.O.S.S enthusiast, an upcoming Linux SysAdmin, web developer, and currently a content creator for TecMint who loves working with computers and strongly believes in sharing knowledge.
--------------------------------------------------------------------------------
via: http://www.tecmint.com/check-syntax-in-shell-script/
作者:[Aaron Kili ][a]
译者:[译者ID](https://github.com/译者ID)
校对:[校对者ID](https://github.com/校对者ID)
本文由 [LCTT](https://github.com/LCTT/TranslateProject) 原创编译,[Linux中国](https://linux.cn/) 荣誉推出
[a]:http://www.tecmint.com/author/aaronkili/
[1]:http://www.tecmint.com/enable-shell-debug-mode-linux/
[2]:http://www.tecmint.com/linux-image-conversion-tools/
[3]:http://www.tecmint.com/wp-content/uploads/2016/12/Show-Shell-Script-Lines.png
[4]:http://www.tecmint.com/wp-content/uploads/2016/12/Check-Syntax-in-Shell-Script.png
[5]:http://www.tecmint.com/wp-content/uploads/2016/12/Enable-Verbose-and-Syntax-Checking-in-Script.png

View File

@ -0,0 +1,170 @@
如何在shell脚本中执行语法检查调试模式
============================================================
我们会解释不同的调试选项及[如何启用调试模式][1]来开启shell脚本调试系列。
在写完你的脚本后,建议我们在运行脚本之前先检查脚本中的语法,而不是查看它们的输出以确认它们是否正常工作。
在本系列的这一部分,我们将了解如何使用语法检查调试模式。记住我们会在本系列的第一部分中解释不同的调试选项,在这里,我们将使用它们来执行脚本调试。
#### 启用verbose调试模式
在进入本指导的重点之前让我们简要地探索下verbose模式。它可以用`-v`调试选项来启用它会告诉shell在读取时显示每行。
要展示这个如何工作,下面是一个示例脚本来[批量将PNG图片转换成JPG][2]。
输入(或者复制粘贴)到一个文件中。
```
#!/bin/bash
#convert
for image in *.png; do
convert "$image" "${image%.png}.jpg"
echo "image $image converted to ${image%.png}.jpg"
done
exit 0
```
接着保存文件,并用下面的命令使脚本可执行:
```
$ chmod +x script.sh
```
我们可以执行脚本并显示它被shell读取到的每一行
```
$ bash -v script.sh
```
[
![Display All Lines in Shell Script](http://www.tecmint.com/wp-content/uploads/2016/12/Show-Shell-Script-Lines.png)
][3]
显示shell脚本中的所有行
#### 在shell脚本中启用语法检查调试模式
回到我们主题的重点,`-n`激活语法检查模式。它会让shell读取所有的命令但是不会执行它们shell只会检查语法。
一旦shell脚本中有错误shell会在终端中输出错误不然就不会显示任何东西。
激活语法检查的命令如下:
```
$ bash -n script.sh
```
因为脚本中的语法是正确的上面的命令不会显示任何东西。所以让我们尝试删除结束for循环的`done`来看下是否会显示错误:
下面是修改过的含有bug的批量将png图片转换成jpg格式的脚本。
```
#!/bin/bash
#script with a bug
#convert
for image in *.png; do
convert "$image" "${image%.png}.jpg"
echo "image $image converted to ${image%.png}.jpg"
exit 0
```
保存文件,接着执行语法检查:
```
$ bash -n script.sh
```
[
![Check Syntax in Shell Script](http://www.tecmint.com/wp-content/uploads/2016/12/Check-Syntax-in-Shell-Script.png)
][4]
检查shell脚本语法
从上面的输出中我们看到我们的脚本中有一个错误for循环缺少了一个结束的`done`关键字。shell脚本从头到尾检查文件一旦没有找到它doneshell会打印出一个语法错误
```
script.sh: line 11: syntax error: unexpected end of file
```
我们可以同时结合verbose模式和语法检查模式
```
$ bash -vn script.sh
```
[
![Enable Verbose and Syntax Checking in Script](http://www.tecmint.com/wp-content/uploads/2016/12/Enable-Verbose-and-Syntax-Checking-in-Script.png)
][5]
在脚本中启用verbose和语法检查
另外,我们会在另外一个例子中通过修改脚本的首行来启用脚本检查。
```
#!/bin/bash -n
#altering the first line of a script to enable syntax checking
#convert
for image in *.png; do
convert "$image" "${image%.png}.jpg"
echo "image $image converted to ${image%.png}.jpg"
exit 0
```
如上所示,保存文件并在运行中检查语法:
```
$ ./script.sh
script.sh: line 12: syntax error: unexpected end of file
```
此外我们可以用内置的set命令来在脚本中启用调试模式。
下面的例子中我们只检查脚本中的for循环语法。
```
#!/bin/bash
#using set shell built-in command to enable debugging
#convert
#enable debugging
set -n
for image in *.png; do
convert "$image" "${image%.png}.jpg"
echo "image $image converted to ${image%.png}.jpg"
#disable debugging
set +n
exit 0
```
再一次保存并执行脚本:
```
$ ./script.sh
```
总得来说我们应该保证在执行之前先在句法上检查我们的shell脚本。
在下面的反馈栏给我们发送关于这篇指导的任何问题或反馈。在这个系列的第三部分中我们会解释并使用shell追踪调试模式。
--------------------------------------------------------------------------------
作者简介:
![](http://1.gravatar.com/avatar/4e444ab611c7b8c7bcb76e58d2e82ae0?s=128&d=blank&r=g)
Aaron Kili是一个Linux及F.O.S.S热衷者即将是Linux系统管理员、web开发者目前是TecMint的内容创作者他喜欢用电脑工作并坚信分享知识。
--------------------------------------------------------------------------------
via: http://www.tecmint.com/check-syntax-in-shell-script/
作者:[Aaron Kili ][a]
译者:[geekpi](https://github.com/geekpi)
校对:[校对者ID](https://github.com/校对者ID)
本文由 [LCTT](https://github.com/LCTT/TranslateProject) 原创编译,[Linux中国](https://linux.cn/) 荣誉推出
[a]:http://www.tecmint.com/author/aaronkili/
[1]:http://www.tecmint.com/enable-shell-debug-mode-linux/
[2]:http://www.tecmint.com/linux-image-conversion-tools/
[3]:http://www.tecmint.com/wp-content/uploads/2016/12/Show-Shell-Script-Lines.png
[4]:http://www.tecmint.com/wp-content/uploads/2016/12/Check-Syntax-in-Shell-Script.png
[5]:http://www.tecmint.com/wp-content/uploads/2016/12/Enable-Verbose-and-Syntax-Checking-in-Script.png