Sunday, 17 April 2016

How to debug shell scripts ?


How to debug a Shell script ?

Debugging a shell script is needed to check it for any errors.
Use sh command with below options to debug a shell script :

-x
Shows each line before it executes it. (Ignores the comments)
sh -x scriptname

-v
Echo each line as it is read. (Also echo commented lines too)
sh -v scriptname

Note: -v echo 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

-u
Gives you the error if there are some variables without values assigned to them 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.
Useful for debugging the first error itself when running big scripts.
sh -e scriptname

No comments:

Post a Comment

Note: only a member of this blog may post a comment.