Lead Image © Andre Zhak, 123RF.com

Lead Image © Andre Zhak, 123RF.com

Debugging Bash scripts automatically

Bug Hunting

Article from ADMIN 48/2018
By
We look at various extension frameworks that make the life of developers and administrators easier when debugging a script.

In times of almost infinite computing power, it makes no sense to rely on hard copy and a magnifying glass when you're debugging a script. I'll look at the extensive Eclipse integrated development environment (IDE) that offers a number of very convenient debugging functions. To begin, you should download the Eclipse IDE for Java Developers [1] with the Eclipse installer – the steps that follow are based on the Oxygen 2 version from December 2017.

Next, get the shell extension from the Eclipse Marketplace [2] and drag and drop the Install button to start the shell extension installation. Make sure you do not place the button on the Welcome screen; use an empty editor page instead (Figure 1). The Marketplace client integrated into new versions of Eclipse then automatically starts installing the plugin.

Figure 1: To install the shell extension for Eclipse, drag and drop the Install button onto an empty editor page.

Setting Up Eclipse

Annoyingly, the Eclipse plugin uses the Dash shell by default, which cannot compare with Bash in terms of ease of use. To change this, click Window | Preferences | Shell Script | Interpreters and check bash (Figure 2).

Figure 2: Setting up the Bash interpreter for Eclipse's shell extension is not very complicated.

The next step is to create a new shell script project under File | New | Other . This container converts shell scripts to a format that the Eclipse IDE understands. The newly created project has no content at the beginning, so right-click it and select the New | Other option to add a new script.

Although you can edit shell scripts using the syntax highlighting feature of Eclipse and run them locally, you cannot set breakpoints at the moment. To do so, you first need to download and extract the BashEclipse ZIP archive from SourceForge [3]; while the IDE is shut down, move the two JAR files to the /home/<home>/java-oxygen/eclipse/dropins directory.

Note that Eclipse tends to exhibit several bugs when using OpenJDK, so make sure you run Eclipse in an Oracle JDK environment to avoid them.

Changing Paths

Eclipse and BashEclipse communicate via _DEBUG.sh, which contains:

exec 33<>/dev/tcp/localhost/33333
function _________DEBUG_TRAP ()
{
    local _________DEBUG_COMMAND
    read -u 33 _________DEBUG_COMMAND
    eval $_________DEBUG_COMMAND
}
set -o functrace
trap _________DEBUG_TRAP DEBUG
This code uses the operating system's <C>trap<C> feature to set up a function that takes incoming commands from the remote debugger and parses them in the open session. Although this setup is not necessarily secure, as long as you're working on a secure network, it should not be too risky.

After copying the file into the project directory, create the script you want to try out. In the following steps, the test object is a group of Bash-specific language constructs:

. _DEBUG.sh
echo $SHELL
a=1234567890
echo ${a}
echo ${a:3}

Of particular importance here is the inclusion of the debug script, which establishes a connection between the shell and the development environment.

In the next step, you need to create an execution configuration by clicking Run | Debug Configurations and create a new configuration based on the Bash script template. The execution configuration generator known from the normal Eclipse workflow then shows you a setup wizard, which you parameterize (Figure 3). By the way, the debugger port is always 33333 – at least as long as you don't customize _DEBUG.sh.

Figure 3: Be sure to adapt the paths to your situation when debugging with Eclipse.

Integrating the Last Eclipse Extension

In the next step, click on Debug in Eclipse to run the program. Note that the IDE itself is not able to launch the small script. Instead, switch to the Debugger perspective by clicking the icon in the upper right corner of the Eclipse workspace; then, open a new shell window in which you request the execution of the script by entering bash ./<script_name>:

tamhan@TAMHAN14:~/workspace$ bash ./firsttest.sh
/bin/bash
1234567890
4567890

In the present execution configuration, Eclipse reacts by displaying the error message Unable to open editor, unknown editor ID , because the Bash debugger requires a specific editor path on the Eclipse side.

The solution to this problem is to visit the discontinued SourceForge website of the ShellEd project [4]. Ignore the hints that the information contained there is outdated and download the net.sourceforge.shelled-site-2.0.3.zip file.

In the next step, open Eclipse and choose Help | Install new software . In the dialog that appears, you need to press the Add button next to the address bar to open the window for adding local repositories. Use the Location field to add a link to the archive.

After confirming, the installation process starts, which requires a restart of the IDE and outputs some warnings about obsolete certificates. From this point on, you can debug scripts in the same way you would debug Java or C code.

Buy this article as PDF

Express-Checkout as PDF
Price $2.95
(incl. VAT)

Buy ADMIN Magazine

SINGLE ISSUES
 
SUBSCRIPTIONS
 
TABLET & SMARTPHONE APPS
Get it on Google Play

US / Canada

Get it on Google Play

UK / Australia

Related content

comments powered by Disqus
Subscribe to our ADMIN Newsletters
Subscribe to our Linux Newsletters
Find Linux and Open Source Jobs



Support Our Work

ADMIN content is made possible with support from readers like you. Please consider contributing when you've found an article to be beneficial.

Learn More”>
	</a>

<hr>		    
			</div>
		    		</div>

		<div class=