Although Bash scripting is not a full fledged compile-able programming language, it is still very powerful and the script size can grow to enormous size. Therefore, even when you are just occasional bash scripting user or complete beginner some techniques is good to know in order to help debug you bash code. In this article we list some most command and basic bash scripting debbuging techniques.

The first debugging technique is bu using -x bash option during script execution. This will print all lines of code that is execute. For an illustration let's consider a following example:
#!/bin/bash 

echo hello

myvar=3
echo $myvar
To debug this bash shell script use -x bash option during script execution:
$ bash -x bash-script.sh 
+ echo hello
hello
+ myvar=3
+ echo 3
3

After reading this short shell scripting tutorial you will be able to to create and execute your own bash shell script. Not previous knowledge of shell scripting is required. However, you are expected to have some knowledge of hot ot start terminal and how to edit text files with some text editor of your choice.

Writing your own first Shell Script

In this section and as well as a first step we are going to create a simple shell script doing nothing else but printing "Shell Scripting" on the terminal output. This will will your starting point. To your first basic script together you need to know two things:

This series of articles is dedicated to development on Linux systems. This tutorial focuses on C programming and covers such concepts as types, operators and variables, flow control, functions, pointers and arrays, structures, basic I/O, coding style and building a program as well as packaging for Debian and Fedora or getting a package in the official Debian repository.

Very often I need to upload some files to my web server from a command line. entering a user name and password, changing directory can be little tedious work. Here is a small bash script which make this work easier. This script first defines a variables such as hostname of the ftp server, username and password and then it creates ftp session and uploads file into your selected directory:

This is a simple skeleton of a bash countdown script. The script takes two arguments . Here are some examples of its usage:

  • countdown time to 90 minutes from now:
./bash-countdown.sh -m 90
  • countdown time to 23.3.2036 from now:
./bash-countdown.sh -d "Mar 23 2036"
  • countdown time to 21:06 from now:
./bash-countdown.sh -d 21:06
  • countdown time to 21:06:45 from now:
./bash-countdown.sh -d 21:06:45

This simple perl script will print your external IP address assigned by you internet provider ( great for someone with dynamic IP address ).

First, we download a html file from ipchicken.com and then phrase this file to match for IP address in format X.X.X.X using regular expression.

Next, the script removes all unnecessary characters and prints your external IP address on the terminal. Lastly, the script removes previously downloaded index.html file.

This Perl programming tutorial is a great scripting guide to help you fully understand Perl script. Find Perl tutorials and programming examples to master your knowledge of Perl Scripting.

This article provides few simple scripts to scan and monitor network using combination of bash and ping command. Obviously, these scripts are no match to a full monitoring dedicated software like nagios but they could be useful for a small home brand networks, where implementing sophisticated monitoring system can become an overhead.

Scan network subnet

In this example the bash script will scan network for hosts attached to an IP address 10.1.1.1 - 255. The script will print message Node with IP: IP-address is up if ping command was successful. Feel free to modify the script to scan your hosts range.

When writing a bash scripts most of us by default use echo command as means to print to standard output stream. echo is easy to use and mostly it fits our needs without any problem. However, with simplicity very often comes limitation. This is also the case with echo command. Formatting an echo command output can be a nightmare and very often impossible task to do.

The solution to this can be a good old friend of all C/C++ the “printf” tool. printf can be just as easily implemented into a bash script is it is used with C/C++ programs. This article describes some basics of printf along with practical examples:

FIND LATEST LINUX JOBS on LinuxCareers.com
Submit your RESUME, create a JOB ALERT.
LINUX CAREER NEWSLETTER
Subscribe to NEWSLETTER and receive latest news, jobs, career advice and tutorials.
DO YOU NEED ADDITIONAL HELP?
Get extra help by visiting our LINUX FORUM or simply use comments below.

You may also be interested in: