Thursday, September 8, 2011

[Linux] Debugging Bash scripts.

Not a fun but still a necessary task when it comes to bash scripting.

Debugging the entire script:

bash -x myScript.sh


Debugging parts of script:

    set -x : activate debugging.
    set +x : stop debugging.
Example:

#!/bin/bash

echo "Simple example script\n"

set -x
for i in $(seq 3)
do
   echo "Hello World!"
done

set +x
echo "Done debugging!"

exit 0
Alternative:

Replace the shebang with following:

#!/bin/bash -xv
 

Debugging is twice as hard as writing the code in the first place. Therefore, if you write the code as cleverly as possible, you are, by definition, not smart enough to debug it.
--Brian Kernighan

No comments:

Post a Comment