We started the Shell Script debugging series, first explaining the different debugging options, and here's how to enable Shell debug mode.
After you finish writing the script, it is recommended that you check the syntax in the script before running the script, rather than looking at their output to verify that they are working correctly.
In this part of the series, we will learn how to use the syntax to check debug mode. Remember we previously explained different debugging options in the first part of this series, where we will use them to perform script debugging.
Enable verbose debug mode
Before entering the focus of this guide, let's briefly explore the next verbose model. It can be -v
enabled with the Debug option, which tells the shell to display each line as it reads.
To show how this works, here is a sample script to convert PNG images into JPG format in bulk.
Enter (or copy and paste) the following into 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 use the following command to make the script executable:
$ chmod +x script.sh
We can execute the script and show each line it was read by the Shell:
$ bash -v script.sh
Show all rows in a shell script
Enable syntax check debug mode in a Shell script
Back to the focus of our topic, -n
Activate grammar check mode. It lets the shell read all the commands, but does not execute them, and it (the shell) checks the syntax only.
Once an error is found in the shell script, the shell will output an error in the terminal, otherwise it will not show anything.
The command to activate the grammar check is as follows:
$ bash -n script.sh
Because the syntax in the script is correct, the command above does not show anything. So let's try to delete the end for loop to done
see if the error is displayed:
The following is a modified batch of bugs containing PNG images converted to JPG format.
#!/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 the script and perform a grammar check:
$ bash -n script.sh
Check shell script syntax
From the above output, we see an error in our script that the For loop is missing an end done
keyword. The shell script checks the file from beginning to end, and once it is not found ( done
), the shell prints a syntax error:
script.sh: line 11: syntax error: unexpected end of file
We can combine verbose mode and grammar check mode at the same time:
$ bash -vn script.sh
Enable both verbose and grammar checking in scripts
In addition, we can enable script checking by modifying the first line of the script, as in the following 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
Save the file and check the syntax in the run as shown above:
$ ./script.sh
script.sh: line 12: syntax error: unexpected end of file
In addition, we can use the built-in set command to enable debug mode in the script.
In the following example, we only examine the FOR loop syntax in the 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
Save and execute the script again:
$ ./script.sh
In general, we should ensure that the script syntax is checked before executing the Shell script to catch errors.
Please send us any questions or feedback about this guide in the feedback section below. In the third part of this series, we will explain and use the shell to track debug mode.
Author Profile:
Aaron Kili is a Linux and f.o.s.s enthusiast who is about to be a Linux system administrator, Web Developer, and currently a tecmint content creator who likes to work with computers and share knowledge.
Aaron Kili[1] Translator: GEEKPI proofreading: Jasminepeng
This article by Lctt[2] original compilation, Linux China honor launched
Shell Script Debugging methods