In a recent tutorial, CIH introduced a powerful technique for debugging Shell scripts that eliminates the need for inserting numerous echo statements into the code.
Simple Methodology
To enable this debugging method, execute the following command:
|
|
How It Works
- exec 111> log
This command opens a file named log for writing and associates it with file descriptor 111. Subsequent outputs can be redirected to this file. - export SHELLOPTS
- BASH_XTRACEFD=111
- PS4=’($BASH_SOURCE:$LINENO:$FUNCNAME): '
- set -x
- ./cih.sh
Assuming cih.sh contains a function to generate a random number:
|
|
Expected Output
After running the command, the output in log will look something like this:
|
|
Such advanced techniques significantly elevate the debugging process.