Unix Shell Scripting For DBA’s (PART – 03)

Unix Shell Scripting For DBA’s (PART – 03)

reference of this article from : www.orskl.com

Debugging on the entire script

When things don’t go according to plan, you need to determine what exactly causes the script to fail. Bash provides extensive debugging features. The most common is to start up the subshell with the -x option, which will run the entire script in debug mode. Traces of each command plus its arguments are printed to standard output after the commands have been expanded but before they are executed.

This is the commented -script1.sh script ran in debug mode. Note again that the added comments are not visible in the output of the script.

We can debug the script in  two ways. They are :

1.Full script debug

2.Partial script debug.

1.Full script debug.

We need to create an issue in the script for that i have added dummy command inside script.

Run the script by using special debugging command called “bash -x”

Now its saying the first command date was executes but the second the command dummy was not found.

Then the third command cat /etc/hosts executes properly.

We can debug the script along with she bang (#!) :

Inside the script add  “-x ” along with #!

Execute using ./ file_name :

set -x  debugs every command where it finds.

2.Partial script debug.

To debug the particular command we need to use set -x and set +x

Here I want to debug only for dummy command then write dummy command in between set -x and set +x.

If we execute the script it will never show the command which execute successfully(date and cat /etc/hosts).

Here it debugs only dummy command.

Overview of set debugging option

The dash is used to activate a shell option and a plus to deactivate it. Don’t let this confuse you!

In the example below, we demonstrate these options on the command line:

Alternatively, these modes can be specified in the script itself, by adding the desired options to the first line shell declaration. Options can be combined, as is usually the case with UNIX commands:

#!/bin/bash -xv

Once you found the buggy part of your script, you can add echo statements before each command of which you are unsure, so that you will see exactly where and why things don’t work. In the example commented-script1.sh script, it could be done like this, still assuming that the displaying of users gives us problems:

In more advanced scripts, the echo can be inserted to display the content of variables at different stages in the script, so that flaws can be detected:

Note: Please test scripts in Non Prod before trying in Production.
1 Star2 Stars3 Stars4 Stars5 Stars (3 votes, average: 5.00 out of 5)
Loading...

Add Comment