We started the shell script debugging series by explaining the different debugging options and how to enable shell script debugging modes.
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.
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
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 doesn’t 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
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
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.
Another very useful option is set -x
@Amau
Yap, in the next part of the shell debugging series, we will cover shell tracing or shell execution tracing which is enabled by the -x option. So stay connected to read more about this option. And thanks for pointing that out.
If you’re running the script with bash, you don’t need to make it executable for debugging.
@danofsatx
Just tested that and yap, it works. Good info. Thanks.