linux poison RSS
linux poison Email

How to debug Shell Scripts

When things in your script 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.

-x option to debug a shell script:
Below is a sample shell script ..
#!/bin/bash
echo 'Hi'
echo 'I will now fetch you a list of connected users:'
w
echo "Display a calendar..."
cal
echo "Bye!"

Now, running a shell script with -x option enables the display of commands and its arguments when they are executed.
nikesh@poison:~$ bash -x script.sh
+ echo Hi
Hi
+ echo 'I will now fetch you a list of connected users:'
I will now fetch you a list of connected users:
+ w
 17:10:09 up  1:01,  3 users,  load average: 0.79, 0.80, 0.72
USER     TTY      FROM              LOGIN@   IDLE   JCPU   PCPU WHAT
nikesh   tty7     :0               16:09    1:00m  4:21   0.27s gnome-session
nikesh   pts/0    :0.0             16:20   18:43   1.38s  4.03s gnome-terminal
nikesh   pts/1    :0.0             16:51    0.00s  0.39s  0.00s bash -x script.sh
+ echo 'Display a calendar...'
Display a calendar...
+ cal
   November 2010
Su Mo Tu We Th Fr Sa
    1  2  3  4  5  6
 7  8  9 10 11 12 13
14 15 16 17 18 19 20
21 22 23 24 25 26 27
28 29 30

+ echo 'Bye!'
Bye!

Using "set" command debug options
Bash shell offers debugging options which can be turn on or off using set command. Below are the two options, which can be used to debug any script in a bash shell.

- set -x : Print commands and their arguments as they are executed.
- set -v : Print shell input lines as they are read.

Using the set Bash built-in you can run in normal mode those portions of the script of which you are sure they are without fault, and display debugging information only for troublesome zones.

You can use above two command in shell script itself like below:
#!/bin/bash
#Turn on Debug mode
set -x
set -v
echo 'Hi'
echo 'I will now fetch you a list of connected users:'
w
#Turn off Debug mode
set +x
set +v
echo "Display a calendar..."
cal
echo "Bye!"

The output of running this script with bash will be
nikesh@poison:~$ bash script.sh
+ set -v
echo 'Hi'
+ echo Hi
Hi
echo 'I will now fetch you a list of connected users:'
+ echo 'I will now fetch you a list of connected users:'
I will now fetch you a list of connected users:
w
+ w
 17:21:18 up  1:12,  3 users,  load average: 0.96, 0.76, 0.70
USER     TTY      FROM              LOGIN@   IDLE   JCPU   PCPU WHAT
nikesh   tty7     :0               16:09    1:11m  4:57   0.28s gnome-session
nikesh   pts/0    :0.0             16:20   29:52   1.38s  5.21s gnome-terminal
nikesh   pts/1    :0.0             16:51    0.00s  0.42s  0.00s bash script.sh
#Turn off Debug mode
set +x
+ set +x
set +v
Display a calendar...
   November 2010
Su Mo Tu We Th Fr Sa
    1  2  3  4  5  6
 7  8  9 10 11 12 13
14 15 16 17 18 19 20
21 22 23 24 25 26 27
28 29 30

Bye!

You can switch debugging mode on and off as many times as you want within the same script.


0 comments:

Post a Comment

Related Posts with Thumbnails