Tuesday, July 28, 2015

How to debug a Shell script in Linux !!!

source

Debugging a shell script is very essential to check your script for any errors before moving it into production. In this post we will see different debugging options useful for running your scripts without a flaw. When executing a shell script use sh command with below options to debug a shell script in effective way.  

Option -x: If you set this option, it will show you each line before it executes it. Make a note that comments will not be reported.
sh -x scriptname
Option -v: Echo’s each line as it is read. It’s a kind of verbose and even echo back commented lines as well.
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.
sh -xv scriptname
Option -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.
sh -u scriptname
Option -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…
sh -e scriptname
Option -n: Check for syntax errors. This option is verymuch usefull for new commers to check for if, for, while, case etc statements
sh -n scriptname
Note: This options will not give any error if the command you are running is not present in your machine.
Controlled debugging:
We can use set -x at start of your code in your shell script to enable debugging and use set +x to disable debugging.
 
Tips: 1) Always use vim editor to get the syntax error at the time of editing the script itself.
      2) Place brackets in a meaning full way using tabs.
      3) Try to read and understand the script before running a script. 4) Try to give as many comments as possible for better understanding of the script.
Debugging a shell script is very essential to check your script for any errors before moving it into production. In this post we will see different debugging options useful for running your scripts without a flaw. When executing a shell script use sh command with below options to debug a shell script in effective way.    
Option -x: If you set this option, it will show you each line before it executes it. Make a note that comments will not be reported.
sh -x scriptname
Option -v: Echo’s each line as it is read. It’s a kind of verbose and even echo back commented lines as well.
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.
sh -xv scriptname
Option -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.
sh -u scriptname
Option -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…
sh -e scriptname
Option -n: Check for syntax errors. This option is verymuch usefull for new commers to check for if, for, while, case etc statements
sh -n scriptname
Note: This options will not give any error if the command you are running is not present in your machine.
Controlled debugging:
We can use set -x at start of your code in your shell script to enable debugging and use set +x to disable debugging.
 
Tips: 1) Always use vim editor to get the syntax error at the time of editing the script itself.
      2) Place brackets in a meaning full way using tabs.
      3) Try to read and understand the script before running a script. 4) Try to give as many comments as possible for better understanding of the script.

No comments:

Post a Comment