Search Web......

Debugging Shell Scripts on Unix/Linux environment

I have seen many unix platform beginners struggling with shell scripts, and many of them have asked this question to me

"How can I Debug a shell script?"

As you may already know that shell scripts are interpreted, and every statement in your script is interpreted just before running it. So locating the problem can be easier then other languages where code is converted to binary and matching line of code with a binary code becomes difficult. Still, many people don't know that there can be different ways to debug a shell script on unix platform. I am trying to list them all as it may depend on your usage.

-x option to debug a shell script



Running a shell script with -x option enables the display of commands and its arguments when they are executed.

  
$ bash -x [script-name]


Look at the test.sh example below. test.sh is a a simple test shell script, which sets the path with a bin directory in my home



#!/bin/bash
echo 'Hi'
export PATH=$PATH:/export/home/swiki/bin
echo $PATH



Here is the output of running this script in a debug mode using -x option for bash command.



$ /bin/bash -x test.sh

+ echo Hi
Hi
+ export PATH=:/export/home/swiki/bin
+ PATH=:/export/home/swiki/bin
+ echo :/export/home/swiki/bin
:/export/home/swiki/bin



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.

You can use above two command in shell script itself like below:



#Turn on Debug mode
set -x
set -v
echo 'Hi'
export PATH=$PATH:/export/home/swiki/bin
echo $PATH
#Turn off Debug mode
set +x
set +v



The output of running this script with bash will be



$ /bin/bash test.sh

+ set -v
+ echo Hi
Hi
+ export PATH=:/export/home/swiki/bin
+ PATH=:/export/home/swiki/bin
+ echo :/export/home/swiki/bin
:/export/home/swiki/bin



Use of a DEBUG function



Add a variable DEBUG_ENABLE on top of your script and set it to parameter value from command line. In this example I am taking the first parameter of the script.




#!/bin/bash
DEBUG_ENABLE=$1
function DEBUG()
{
if [ "$DEBUG_ENABLE" == "debug" ]
then
$@
fi
}

DEBUG set -x
DEBUG set -v
echo 'Hi'
export PATH=$PATH:~/bin
echo $PATH



All the calls to debug related setting should be prefixed by DEBUG function call, this will make sure you don't execute them when you are not debugging.
In this example, when you are running the script you just need to pass a parameter "debug" on the command line and the script will run in debug mode.




$ /bin/bash test.sh debug

+ DEBUG set -v
+ '[' debug == debug ']'
+ set -v
+ echo Hi
Hi
+ export PATH=:/export/home/swiki/bin
+ PATH=:/export/home/swiki/bin
+ echo :/export/home/swiki/bin
:/export/home/swiki/bin



Externalizing the Debugging to a debug script



The above mentioned DEBUG function technique can be commonly used to debug many scripts. So it will not make sense to keep the same DEBUG function inside each script.
A simple way is to create a debug.sh script and source it while running a script. The command line parameter will still stand good for this.

Below are the content of debug.sh script



$ cat debug.sh

#!/bin/bash
_DEBUG=$1
function DEBUG()
{
if [ "$_DEBUG" == "debug" ]
then
$@
fi
}

DEBUG set -x
DEBUG set -v



To use this debug script in your actual script you just need to make a call to the debug.sh as shown below



source ./debug.sh



So now our original test.sh script looks like this



#!/bin/bash
source ./debug.sh
echo 'Hi'
export PATH=$PATH:~/bin
echo $PATH



Now if you run the script with "debug" option then this will be the output.


$ /bin/bash test.sh debug

++ DEBUG set -v
++ '[' debug == debug ']'
++ set -v

echo 'Hi'
+ echo Hi
Hi
+ export PATH=:/export/home/swiki/bin
+ PATH=:/export/home/swiki/bin
+ echo :/export/home/swiki/bin
:/export/home/swiki/bin


If you have noticed the two plust signs (++) at the beginning of outuput, it shows that a script is called inside another script.

Personally I like the last option of externalizing the scripts debugging in case you are writing many scripts. If you write a script once in a while then using the "set -x" option would be easiest/quickest thing to do debugging.

Hope this will make your unix script debugging simpler than before. Don't forget to share with us if you know a better way of debugging unix scripts.




unix debug script, shell script debug, linux script debug, linux debug script, debugging shell script, debug bash script on linux,debug bash script on unix, debug function to debug linux scripts, debug script for debugging scripts

GlassFish Best Practices: Setting Automatic Start for GlassFish v2 Cluster Node Agents

We are trying to run GlassFish V2 on Linux machine and every time we try to restart the node agents using asadmin command it used to prompt password for user as well as master password. We had tough time figuring this out. It was easy for us to setup autostart for the domains but for node agents its was not clearly mentioned anywhere.
[Continue Reading]

Command line deployment at Linux/Unix for Sun GlassFish Enterprise Server

I have always liked the command line deployment on any application server. I still don't trust the admin console for deploying my war file on any server.
Many times I have observed that the server doesn't respond if there is a problem in deployment.
Therefore I prefer the command line deployment. Here are few simple steps, which can be used to do web application war file deployment on glassfish server running at Unix/Linx machine.
[Continue Reading]

Linux Web Browsers - Where is user friendliness ?

I recently started using Fedora Linux from my Sony VAIO Laptop and was trying to get another browser other then Firefox for my system. I was surprised to know that only Firefox is a decent browser available for Linux users. Its not that I don't like Firefox but I always liked to use multiple browsers on a system. I like to experiment and like different things in different browsers. For example to quickly launch a browser window "Google-Chrome" is best browser as it launches very well. Where as for JavaScript developement I prefer Mozilla Firefox as it has Firebug and other plugins for debugging. Internet explorer still remains one of the most popular browsers so I have always tested my web applications on different versions of this browser.

Linux is such an old operating system and Fedora/Red Hat version have really made it user friendly. But its still not their to the level of dumb user friendliness. I always liked using unix environments, like most developers do, but if I want my father use this this Laptop with Linux installed then its going to be impossible for him to even start using it.

Just a small example, I opened YouTube on Firefox and see a message saying "Either JavaScript is disabled, or you have old version of Flash Player.". So I clicked the link do download the FlashPlayer linux version. Here are the steps I had to do

1. Find the Linux tar file
2. Extract the tar file.
3. Copy the .so library file in ~/.mozila/plugin directory.

For me it was easy to do and understandable, but when I think about my father who doesn't know about how computers/softwares work its going to be almost impossible. I don't know why this can not be done the way its done in Windows systems? Why this great open source stuff can't still do that level of automation.

I have seen google doing great things in different areas, but am disaapointed to see that Chrome doesn't work on Linux.

Here are few browsers I found for Linux systems, but none of them as powerfull as Mozilla Firefox.

1. Amaya: The Amaya software is written in C and is available for Windows, Unix platforms and MacOS X. You can read more about this at http://www.w3.org/Amaya

2. Galeon: Galeon is a GNOME Web browser based on Gecko (the mozilla layout engine).

3. Konqueror: Konqueror is an Open Source web browser with HTML 4.01 compliance.

4. Lynx: Lynx is a text based browser. Certainly not user friendly as text based browsing is not easy for every user.

5. Netscape Navigator: Its one of the oldest Linux supported browsers. I have not seen much of development on this recently.