Debugging korn shell scripts
Debugging shell script is a bit difficult. You will always find yourself putting lots of “echo” or “print” statement throughout the script to find where it is going wrong. The drawback of such effort are
1) Spending lots of time in finding issue in those echo output.
2) If you miss printing values for certain variable, you will need to write echo statement which will print the values and re-execute the program.
3) Tapping conditional statement would be difficult to trace with print statement alone.
One way of debugging shell script to enable options while executing script and tracing down the output statement for errors. For example, consider the following script ( lets call this script as test.ksh )
1 2 3 4 5 6 | #!/bin/ksh x="testValue"; while [[ "$x" != "" ]]; do echo "X is : $x "; x=""; done |
Executing the following script would give you the result
1 | X is : testValue |
However if you execute the script with -x option as follows, you will notice every line from script getting printed on standard output which will have variable replaced with the values.
ksh -x test.ksh 2 x=testValue 3 [[ testValue != ]] 4 echo X is : testValue X is : testValue 5 x= 5 [[ != ]]
You would easily know by looking into such output how the variable x in script is changing the values and if there is any issue in script, you can track it down. This shell feature definitely helps in finding issue faster ( atleast as compared to putting lots of “echo” statement ).
The other option which can help in shell script debugging would be “-n”. From kornshell doc,
“You should always run ksh -n on each script you write. The -n option will check for syntax errors on paths that might not even be checked when you run the script. It also produces a number of warning messages.”
Other way of debugging korn shell script is to use kshdb ( korn shell debugger ). I have not used it enough till date. See kshdb manual for more information on how to enable and debug script.
Tags: debug, Korn, Programming, script, shell













My Profile in Linkedin

Comments