Bash scripting – Basics- I

by
Share this Article: Facebook0Google+1Twitter0LinkedIn0Reddit0StumbleUpon0

If you have been using computers for a while, whether you have been using Unix, Windows or Mac operating systems, you’re probably already familiar with theterminal term shell. A shell is piece of software that is inbuilt into most operating systems which acts as an interface to the kernel and other software. Shells come in various shapes and sizes, but in most cases command line shells will always come with some type of scripting language which administrators can use.

Shell scripting languages are typically easy to learn and even if you don’t have any related programming experience, you can usually pick up the basics in a matter of days.

Some of the more common shells you might be familiar with are the command prompt in Windows or cmd.exe, which uses the batch scripting language or Windows power shell which is the object oriented successor of cmd.exe. People more familiar with Unix might be familiar with the C or Bourne shell, while Linux users typically these days will use the bash or Bourne again shell. Throughout the tutorials in the upcoming weeks I will take you through some of the basics of bash scripting and provide you with small samples of code to help you with the learning process. Though it will be near impossible for me to cover everything you might want to know, my aim is to help you atleast help you develop a foundation on some of the basics which you can then build on.

Hashbang

The hashbang is always the first and most important line of any bash script. It is given the name hashbang because of the # hash followed by an exclamation mark or bang. The hashbang tells the command interpreter where to look for the shell where it will pass the code onto. It contains a hash or comment character, so that the interpreter knows not to run this code and simply skip to the next line. The bash shell is usually located in the /bin directory, so in this case we would write the hash bang as seen below.

 

{codecitation}#!/bin/bash{/codecitation}

Comments

If you have used certain languages before such as PHP, Python or Ruby you will be familiar with the hash (#) being used to add comments to a script. This can also be seen throughout Linux in files such as configuration files. The hash is a single line comment and anything displayed on the same line after the hash will not be run.

{codecitation}# This is an example comment{/codecitation}

Typically most languages also use characters for including multiple line comments and although bash doesn’t use the /* */ characters that you may be used to in C or PHP it can use a here document process which can be useful for storing multiple lines of comments. The here document process preserves white spaces and new lines so that any text including commands will not be run.

{codecitation}#/bin/bash

echo “This line is displayed”

: << “–comment–”

echo “This line is not displayed”

echo “This line is not displayed either”

–comment–

echo “This line is displayed”{/codecitation}

 

Running your script

Running your first script is fairly simple; you can edit your script in almost any text editor and give it any name you prefer. It’s always a good idea though to use some sort of naming convention especially if you have a large amount of scripts, so in this case we will take the contents of the script above and name it multiline.sh (.sh stands for shell).

Next you will need to give your script run and execute permissions. Depending on your setup you might want to give your script the octal permission 555, but in this case since we will be editing the script we also want the user to have write permission, so let’s run “chmod 755 multiline.sh” on our script.

Finally you will need to run your script using the dot slash method if your in the same directory, for example “./multiline.sh”. Throughout Unix and Linux environments dot slash simple means the current directory. If you had the bash script in another directory such as the home directory and you were in say the bin directory, you would then use the absolute path of the script which would be “/home/multiline.sh”.

For questions please refer to our Q/A forum at : http://ask.unixmen.com/

Share this Article: Facebook0Google+1Twitter0LinkedIn0Reddit0StumbleUpon0