- [Instructor] Debugging the script.
In the previous video, we've learned how to get and set
dates and delays.
In this video, we're going to learn
how bash will provide certain debugging options
that every sysadmin should know.
Debugging is one of the critical features
that every programming language should implement
to produce race-back information
when something unexpected happens.
Debugging information can be used to read
and understand what caused a program to crash
or to act in an unexpected fashion.
We can either use bashes in built debugging tools
or write our scripts in such a manner
that they become easy to debug.
Now, add the -x option to enable debug tracing
of a shell script as.
Running the script with the -x flag
will print each source line with the current status.
Note that you can also use sh-x script.
Debug only portions of the script
using the set -x and set +x.
For example, in the preceding script
the debug information for echo $I
will only be printed,
as debugging is restricted to that section
using the -x and +x.
The aforementioned debugging methods
are provided by bash builtins.
But they always produce debugging information
in a fixed format.
In many cases, we need debugging information
in our own format.
We can set up such a debugging style
by passing the _DEBUG environment variable.
We'll look at this example code.
We can run the above script
with the debugging set to on as.
We prefix DEBUG before every statement
where debug information is to be printed.
If _DEBUG=on is not passed to the script,
debugging information will not be printed.
In bash, the command tells the script shell to do nothing.
Let's see how it works.
The -x flag outputs every line of script
as it is executed to STD out,
however we may require only some portions
of the source lines to be observed
such that commands and arguments are to be printed
at certain portions.
In such conditions, we can use set built in
to enable and disable debug printing within the script.
This displays arguments and commands
upon their execution.
This disables debugging.
This displays input when they are red.
This disables printing input.
We can also use other convenient ways to debug scripts.
We can make use of shebang in a trickier way
to debug scripts.
Awesome!
We have successfully learned how to debug scripts.
In the next video, we'll learn about
functions and arguments.