debugging scripts | set -x | _DEBUG="on"

set -x: Displays arguments and commands upon their executio
set +x: Disables debugging

#!/bin/bash
#Filename:script.sh
for i in {1..4}
do
set -x
echo $i
set +x
done
----

+ echo {1..4}
{1..4}
+ set +x
-------------------
Shebang hack The shebang can be changed from #!/bin/bash to #!/bin/bash –xv to enable debugging without any additional flags (-xv flags themselves).
-----------------
Use of intelligent DEBUG function
First, add a special variable called _DEBUG. Set _DEBUG to ‘on’ when you need to debug a script:
_DEBUG="on"
Put the following function at the beginning of the script:
function DEBUG()
{
 [ "$_DEBUG" == "on" ] &&  $@
}
Now wherever you need debugging simply use the DEBUG function as follows:
DEBUG echo "File is $filename"
OR
DEBUG set -x
Cmd1
Cmd2
DEBUG set +x
When done with debugging (and before moving your script to production) set _DEBUG to ‘off’. No need to delete debug lines.
_DEBUG="off" # set to anything but not to 'on'
Sample script:
#!/bin/bash
_DEBUG="on"
function DEBUG()
{
 [ "$_DEBUG" == "on" ] &&  $@
}
 
DEBUG echo 'Reading files'
for i in *
do
  grep 'something' $i > /dev/null
  [ $? -eq 0 ] && echo "Found in $i file"
done
DEBUG set -x
a=2
b=3
c=$(( $a + $b ))
DEBUG set +x
echo "$a + $b = $c"
Save and close the file. Run the script as follows:
$ ./script.sh
Output:
Reading files
Found in xyz.txt file
+ a=2
+ b=3
+ c=5
+ DEBUG set +x
+ '[' on == on ']'
+ set +x
2 + 3 = 5
Now set DEBUG to off (you need to edit the file):
_DEBUG="off"
Run script:
$ ./script.sh
Output:
Found in xyz.txt file
2 + 3 = 5
Above is a simple but quite effective technique. You can also try to use DEBUG as an alias instead of function.

Debugging Common Bash Shell Scripting Errors

Bash or sh or ksh gives various error messages on screen and in many case the error message may not provide detailed information.

End of file unexpected Error

If you are getting an End of file unexpected error message, open your script file and and make sure it has both opening and closing quotes. In this example, the echo statement has an opening quote but no closing quote:
#!/bin/bash
...
....
echo 'Error: File not found
                                        ^^^^^^^
                                        missing quote
Also make sure you check for missing parentheses and braces ({}):
#!/bin/bash
.....
[ ! -d $DIRNAME ] && { echo "Error: Chroot dir not found"; exit 1;
                                                                    ^^^^^^^^^^^^^
                                                                    missing brace }
...

Missing Keywords Such As fi, esac, ;;, etc.

If you missed ending keyword such as fi or ;; you will get an error such as as “xxx unexpected”. So make sure all nested if and case statements ends with proper keywords. See bash man page for syntax requirements. In this example, fi is missing:
#!/bin/bash
echo "Starting..."
....
if [ $1 -eq 10 ]
then
   if [ $2 -eq 100 ]
   then
      echo "Do something"
fi
 
for f in $files
do
  echo $f
done
 
# note fi is missing

Tip#1: Send Debug Message To stderr

Standard error is the default error output device, which is used to write all system error messages. So it is a good idea to send messages to the default error device:
# Write error to stdout
echo "Error: $1 file not found"
#
# Write error to stderr (note 1>&2 at the end of echo command)
#
echo "Error: $1 file not found" 1>&2

Tip#2: Turn On Syntax Highlighting

Most modern text editors allows you to set syntax highlighting option. This is useful to detect syntax and prevent common errors such as opening or closing quote. You can see bash script in different colors. This feature eases writing in a shell script structures and syntax errors are visually distinct. Highlighting does not affect the meaning of the text itself; it’s made only for you. In this example, vim syntax highlighting is used for my bash script:
Fig.01: Bash shell script syntax highlighting using vim text editor
Fig.01: Bash shell script syntax highlighting using vim text editor

Comments

Popular posts from this blog

HAproxy logging

teamcity Automatic Agent Start under Linux

NFS mount add in fstab _netdev instead of default | firewall-cmd --list-all