Friday, August 3, 2007

Debugging Shell Script in Linux.


Bash shell offers debugging options which can be turn on or off using set command.

Execution Trace
set -x : Display commands and their arguments as they are executed.

Verbose Mode
set -v : Display shell input lines as they are read.

Syntax Checking
set -n : Reads all commands, but does not execute them.

You can use above commands in shell script itself:

#!/bin/bash
pwd
ls
# turn on debug mode
set -x
if [ -f "/home/user/file.txt" ]; then
echo "Error reading file: /home/user/file.txt"
exit 1
fi
# turn OFF debug mode
set +x
ls
# more commands.


You can replace standard

#!/bin/bash

with (for debugging)
#!/bin/bash -xv


No comments:

Search About OpenSource

Google