What is Shell Scripting

Shell is an interface between the Linux Operating System and the users. Often the word kernel is utilized for the most important part of the OS (Some even call OS as kernel). Even if you take the meaning of the words shell and kernel literally,you feel that the words have been rightly chosen in the Computer Science World. A kernel is soft or delicate part of a fruit or like a seed packed in a husk or consider the white portion of the coconut inside the husk. Just like the shellor husk protects the delicate or soft inner part of the fruit, in the same way a shell protects the delicate , central or essential part of the OS- i.e., the kernel form user mistakes.

A shell provides a very simple interface to the user to access the kernel services.

Suppose there was no shell, You can’t use command like play to play a music file or cd to change a directory or even ls to list the files.

Rather you would have to think about the location of the file in the disk(its physical location), open the file and read the data byte by byte, forward it to the speaker to play….(and many other things) and include all this in OS. But how will you invoke??But thanks to the shell, you can simply write a command at the shell, like ls, cd and play and you are done.We have so far seen what is shell? Let’s see what is shell scriptingSuppose you do a series of commands to achieve a particular task. And this you do regularly (daily, many times a day). There is a solution to it. Write a shell script.
Steps involved:
1. Create a file (touch or use vi)
2. Put the series of commands in the file
3. Save the file (Give it a name: file_name)
4. Change the mode of this file to executable state (use chmod: chmod 0744 file_name)
5. Finally execute the file
i.e., simply write ./file_name and you are doneWonderful! never again you need to repeat those stupid actions(commands) and you have created a new command: file_name to achieve all those steps in one command.This is called shell scripting.

Let’s take a simple example.
Suppose you are a programmer, and if you have written programs like count.c test.c and the main function is in the program main.c.

Normally you would first create object files for the program.
count.o and test.o

$ gcc -c test.c count.c

This creates the respective object files.

Next you would compile your main program main.c along with the object files

$ gcc -o count main.c count.o test.o

This generates an output file called ‘count’, which you would run

$ ./count

This is the series of commands that you would do normally. And suppose you make small changes to test.c and main.c, you would have to again repeat all the above steps

Let’s write a small shell script for that

1. Open a file first.sh
2. Write
#! /bin/sh #normally required to tell which shell to use (There are many shells like tcsh, bash, csh…)
# Choose the one you like
# Note a phrase with # starts the comment. You can write anything in the line after # and the shell will not
# try to interpret it.

gcc -c test.c count.c
gcc -o count main.c count.o test.o

3. Save the file
4. Change permissions of the file so as to give execute permissions from the program

$ chmod u+x first.sh

5. Now your shell script is ready. Now execute the shell script

$ ./first.sh

Thus in a shell script, you can write a series of commands which you perform repeatedly. And from next time onwards, execute the shell script. So there is no need to repeat the commands again, but achieve it with a single command (shell script you wrote now will act as a command)


Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s