Debugging a shell script is very essential to check your script for any errors before moving in to production. There are different debugging tips and tricks useful for running your scripts
without any flaw.
For example: sh –x scriptname
For
Example: sh –v scriptname
Note: A small difference between –x and –v is
that –v echo’s the line as it is read (So it will even
display comments too.), whereas –x flag causes each command to be
echoed as it is executed.
For Example: sh –xv scriptname
-
-u –At times you use a variable without setting
some value to it. If you use this flag it will give you the error
saying so and so variable is not set before executing the script.
-
-e –Exit the shell script if any error occurs.
This option will stop the script to run further once the script
encounters an error. Use full for debugging the first error
itself when running big scripts…
For
Example: sh –e scriptname
-
Always use vim editor to get the syntax error at the time of editing
the script itself.
-
Place
brackets in a meaning full way using tabs.
-
Try to read and understand the script before running a script.
-
Try
to give as many comments as possible for better understanding of the
script.