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:
To debug this bash shell script use -x bash option during script execution:
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.
Some Linux tools does not properly work with files which include spaces in their names. This simple bash for loop will remove white space from file names and rename/move for all files in the given directory.
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:
Here is a small bash script to test a hard drive transfer speed. It should be take as an approximation. The speed value is taken from Linux dd command output. One way to test your hard drive speed is to use hdparm command:
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: