Debuging A Bash Script

Debuging A Bash Script

 Debuging A Bash Script

Bash scripting provides an option to debug your script at runtime. You use “set -xv” command inside a shell script or use -xv on the command line while executing the script.

Syntax:

$ bash -xv script.sh

Example – Enable Debug in Script

You can enable debug mode by adding the set -xv option inside a shell script. This is useful to enable debugging for some parts of the script.

#!/bin/bash

set -xv   # this line will enable debug

cd /var/log/
for i in "*.log"; do
 du -sh $i
done

Now execute the script, You will find results like

$ ./script.sh

Output:

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

Example – Enable Debugging Run Time

You can enable debug at script run time using the -xv option. For example

$ bash -xv script.sh
Reactions

Post a Comment

0 Comments

close