Location via proxy:   [ UP ]  
[Report a bug]   [Manage cookies]                
0% found this document useful (0 votes)
47 views

Bash Debugging

This document discusses different techniques for debugging Bash scripts including using print statements, enabling debug mode, setting breakpoints, and using the Bashdb debugger. Print statements and debug mode allow tracing script execution. Bashdb is a powerful debugger that allows stepping through code, setting breakpoints, and inspecting variables.

Uploaded by

takuminft
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
47 views

Bash Debugging

This document discusses different techniques for debugging Bash scripts including using print statements, enabling debug mode, setting breakpoints, and using the Bashdb debugger. Print statements and debug mode allow tracing script execution. Bashdb is a powerful debugger that allows stepping through code, setting breakpoints, and inspecting variables.

Uploaded by

takuminft
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 4

BASH DEBUGGING

Bash Script Debugging


• Debugging Bash scripts is an essential skill for any Linux or Unix system
administrator, developer, or power user.
• Debugging helps in identifying and fixing errors efficiently.
• Bash (Bourne Again Shell) is a powerful scripting language used for automat-
ing tasks, system administration, and more.

Print Statements
• Adding echo statements to your script can help you inspect variable values,
command outputs, and execution flow.

Example:

#!/bin/bash

# Debugging with print statements


echo "Starting script..."
echo "Current directory: $(pwd)"

Enable Debug Mode


• Bash provides a debug mode that allows you to trace the execution of a script
line by line.
• You can enable debug mode by adding the -x option to your script or setting
the set -x command within the script.

Example:

#!/bin/bash
set -x

# Your script commands go here


echo "Hello, World!"

• When you run the script with debug mode enabled (bash script.sh), each
line of the script will be printed to the terminal before execution, allowing you
to see the flow of execution and identify any issues.

1
Debugging on part(s) of the script
• Using the set Bash built-in you can run in normal mode those portions of the
script of which you are sure they are without fault, and display debugging
information only for troublesome zones.
• Say we are not sure what the somes commands (w) will do in the script, then
we could enclose it in the script like this:

#!/bin/bash
set -x # activate debugging from here
w
set +x # stop debugging from here

Debugging with Bashdb


• Bashdb is a powerful debugger for Bash scripts that allows you to set break-
points, step through code, inspect variables, and more.
• By using Bashdb’s commands, you can efficiently identify and fix issues in your
Bash scripts, making them more reliable and robust.
• Before you can start debugging with Bashdb, you need to install it on your
system.

Installation on Debian/Ubuntu:

sudo apt-get install bashdb

Installation on CentOS/RHEL:

sudo yum install bashdb

Example
• Let’s start with a simple Bash script and learn how to debug it using Bashdb.

#!/bin/bash
echo -n "Enter The Number: "
read -r a
fact=1
while [ "$a" -ne 0 ]; do
fact=$((fact * a))
a=$((a - 1))
done
echo $fact

2
Debugging
• To debug this script with Bashdb, follow these steps:

1. Open a terminal and navigate to the directory containing your script.


2. Run Bashdb with your script as an argument:

bashdb factorial.sh

3. You’ll enter the Bashdb debugger prompt ((bashdb)). Now you can use vari-
ous commands to control the debugging process.

Debugging Commands
• Here are some commonly used commands in Bashdb:

– run: Start executing the script.


– next or n: Execute the current line and move to the next line.
– step or s: Step into function calls.
– list or l: Show the current code context.
– break or b: Set a breakpoint at a specific line or function.
– delete or d: Delete a breakpoint.
– info breakpoints: Show information about breakpoints.
– print or p: Print the value of a variable.
– quit or q: Quit the debugger.

Example
• Let’s debug our example script step by step:

1. Start the debugger:

bashdb factorial.sh

2. Set a breakpoint at the factorial function:

(bashdb) break 1

Example
3. The debugger will stop at the beginning of the factorial function. Use the
list command to see the code:

(bashdb) list

4. Step through the function using the step command:

3
(bashdb) step

5. Use the print command to inspect the value of variables:

(bashdb) print $a

6. Continue stepping through the function until you reach the end.
7. Use the quit command to exit the debugger when you’re done.

References
• https://bashdb.sourceforge.net/bashdb.html
• https://tldp.org/LDP/Bash-Beginners-Guide/html/sect_02_03.html

You might also like