How to Debug a Shell Script ??
We can debug shell script by using “set -xv” command inside shell script or using -xv on command line while executing script.
Debug Shell Script by adding Command :
$ cat checkdebug.sh
#!/bin/bash set -xv #<< This will enable debug cd /var/log/ for i in "*.log"; do du -sh $i done
Execute above Script and watch the output:
# sh checkdebug.sh
cd /var/log/ + cd /var/log/ for i in "*.log"; do du -sh $i done + for i in '"*.log"' + du -sh boot.log mysqld.log post111.log post1121.log yum.log 0 boot.log 32K mysqld.log 0 post111.log 0 post1121.log 4.0K yum.log
Debug Shell Script using Option:
Using this option we don’t need to add “set -xv” in shell script. Just create a shell script like below
$ cat checkdebug2.sh
#!/bin/bash cd /var/log/ for i in "*.log"; do du -sh $i done
and Execute like below
# sh -xv checkdebug2.sh
#!/bin/bash cd /var/log/ + cd /var/log/ for i in "*.log"; do du -sh $i done + for i in '"*.log"' + du -sh boot.log mysqld.log post111.log post1121.log yum.log 0 boot.log 32K mysqld.log 0 post111.log 0 post1121.log 4.0K yum.log