Friday, September 10, 2010
 
 

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.

 

String to Character Array using Korn Shell

I was trying to reorganize my unix environment for different work. For example, I do not need oracle environment configuration in a machine where I’ll be using informix database and hence I would need informix related configuration only.  For that purpose, I was modifying script to prompt a menu whenever I login with available configuration. I could input my choice as a character and corresponding environment will be loaded.

For example, If my input is “1″,  config variables in file 1 will be loaded in shell.

While doing so, I realized, I  may need related multiple environment configuration in one shell and hence I should be able to input multiple characters as string and corresponding environment variables will be loaded.  My assumption was to read all characters in string and split them in array.  Finally loop through the array and load all required variable in shell.

For example, the following shell script splits the string variable strValue using whitespace as delimiter and store them into charArray.

$ export IFS=" ";
$ strValue="1 2 3 4 5";
$ set -A charArray $strValue
$ echo "First Element: ${charArray[0]}"
First Element: 1

My idea was to make IFS make empty string and perform the same thing to split string into character array and hence the code for the same would be

$ export IFS="";
$ strValue="12345";
$ set -A charArray $strValue

But when I tried to iterate through the array as

$ echo "First Element: ${charArray[0]}"
First Element: 12345

I was so wrong! Making IFS empty string does not split the string as character wise into array. But why ? I tried to find an answer but !

 
 
 

 

Find Me!
View Kunal Kumar's profile on LinkedIn