Blessen Cherian, CTO and Executive Team Member of bobcares.com writes, "Shell scripting is nothing but a group of commands put together and executed one after another in a sequential way. Let's start by mentioning the steps to write and execute a shell script."

 

Step 1: touch a file

e.g.: touch Firstshellscript.sh

Step 2: Open the file using the command vi or pico

e.g.: vi Firstshellscript.sh

Step 3: All shell script should begin with "#!/bin/bash ". This line is called the Shebang and this line looks like a comment but its not. It's a message which talks about the interpreter to be used for this script.

Normally the format is #! <path of the bash installation if its a bash script >

Step 4: Write the code / script which you want to develop in the file named Firstshellscript.sh. Here let's write our first shell script and let it be the normal hello world display script.

Step 5: For hello world to be displayed in the shell script. Put this content in the file Firstshellscript.sh.

echo "Hello World"

Step 6: Next step is to make the script executable by using the command chmod

e.g.: chmod 744 Firstshellscript.sh

or

chmod u + x Firstshellscript.sh

Step 7: Execute the script using the command sh

i.e. bash> sh Firstshellscript.sh

If you want to see the entire execution then use the command

sh -xv < script name>

Step 8: The above command would display the contents like what is shown below

bash> sh Firstshellscript.sh

bash > Hello World

Finally Success, we have written our first shellscript and executed it.

A hello world script will look like what is shown below, if you cat or open the script named Firstshellscript.sh

cat Firstshellscript.sh

#!/bin/bash

echo Hello World

Comments in a Shell

All lines beginning with # is a comment in shell scripting. You can have multiple comments by using colon and single quotes.

e.g.:

:' This is a comment line

Again this is a comment line

My God again this is a comment line'

Notes: This will not work if there is a single quote in between the contents.

Variables

As you all know, variables are the most significant part of any software language, be it Perl, C or anything. Similarly, in shell scripting as well variables are very significant and is classified mainly into 2. They are System Variables and User Defined Variables.

  • System Variables

The System Variables are variables which are already defined and kept in the OS and they are also called Environment Variables. These variables are all named in capital letters. One can see these variables and their values by executing the command set. Examples of System variables are PWD, HOME, USER etc. The values of these system variables can be displayed individually by echoing the System variables i.e. echo $HOME, will display the value stored in the system variable HOME.

To set a System Variable use "set" command

e.g.: bash > set $PATH=/home/blessen/shellscript

  • User Defined Variables

These kinds of variables are commonly used in scripting. They are normal variables but their variable name should not be in capital letters, should not start with a number etc. An ideal naming of variable will be like _define_tempval.

To set or define a user defined variable, please read below.

When we assign or create a variable we just write the variable name, equal to its value i.e. _define_tempval = blessen. Now to use / display the value in the variable _define_tempval we have to use echo command i.e.

echo _define_tempval. The out put of which will be blessen

Please find an example script which will set a variable named username and displays its content on the screen when it is executed.

#!/bin/bash

username=blessen

echo " The username is $username"

  • Command Line Arguments

These are variables which pass values or argument to a script to process it. These variables which are passed into the script are accessed using $1,$2...$n where $1 is the first command line argument and $2 the next etc. The delimiter is space here. $0 is the name of the script. The variable $# will display the number of command line argument supplied.

Let me explain. Consider a script which will take in 2 command line arguments and displays it. The name of the script is commandline.sh and the script will look like the one below

#!/bin/bash

echo "The first variable is $1"

echo "The second variable is $2"

When I execute commandline.sh with command line argument like blessen and lijoe then the output of the script will be like the one shown below

bash>sh commandline.sh blessen lijoe

The first variable is blessen

The second variable is lijoe

  • Exit status variable

This variable tells us if the command executed just above this was successful or not. The variable is represented using $?. If the value is 0, it means that the command which was executed just above this was successful. But if the value is any other number it means that the above command was unsuccessful. Thus it is very useful in scripting.

For testing, create a file named test by using the command touch test. Then try cating the file

bash > cat test

Then check the value of $?.

bash> echo $?

0

The value is zero because the command was successful. Now try catting a file which is not there. Let it be xyz1.

bash> cat xyz1

bash> echo $?

1

The value 1 for the exit status shows that the above command was unsuccessful.

  • Scope of a Variable

I am sure most of the programmers have learned and most probably worked with variables and its scope. In shell also we use the scope of a variable for various programming activities. In shell there are 2 types of scope. One is global and other is local scope. From the name itself you can understand that scope of global variable is throughout the program i.e. any other shell program can use these variables for its functioning and its set using the export command.

Syntax is:

variable1=<its value>

export variable1

In shell program the local variables are defined using a local tag before the variables, while it is defined.

Syntax is:

local variable=<some value>

The below script will demonstrate the scope of a local and global variable.

#!/bin/bash

function display()

{

local local_var=100

global_var=blessen

echo " local variable is $local_var\n";

echo "global variable is $global_var\n";

}

echo " ======================"

display

echo "=======outside ========"

echo "local variable outside function is $local_var\n";

echo "global variable outside function is $global_var\n";

Input and Output in shell scripting

For taking inputs from keyboard we will have to use a tool provided by shell which is called read. The read command will read the values which are typed from keyboard and assigns it to the variable mentioned along with it.

Syntax is: read <variable name>

For outputting, we use echo command and we have already dealt with it in our above explanation.

Syntax is: echo "statement to be displayed"

Arithmetic Operations in shell scripting

Like all other scripting languages shell script also allows us to play with numerical and functions associated with it like addition, subtraction, multiplication and division. To do these arithmetic operations a function called expr is used, which tells the shell script interpreter that these are numerical on which the specified function is to be performed, i.e. expr a + b means add a and b .

Syntax: expr <expression>

e.g.: sum=`expr 12 + 20`

Similarly syntax can be used for Subtraction, Division and Multiplication. There is another way to handle Arithmetic operations; include the variables and function inside a square bracket which starts with a $sign. The syntax is

Syntax is: $[expression / statement]

e.g.: echo $[12 + 10]

Conditional Loops

Lets have some fun with a conditional statements like "if condition