Writing Shell Script on Linux

Introduction

Shell Scripts are the things in linux which can make our lives easier. If you work with Linux based operating system you must have the experience of using command line interfaces in doing work. For some purposes you may have to repeat a several set of commands with same arguments again and again. At that time you may retype or use arrow keys. But let me explain how you can make your life easier at that time.

The solution is using a Shell Script. It’s a simple text file. You can write the commands what you want to execute again and again. After that once you execute that file It’ll execute those commands. So it saves our time. (This works much similar to Batch files in Windows)

How to do it

Here are the steps to make a Shell Script on Linux (Ubuntu).

1. Goto Applications → Terminal.

2. Type “gedit”. Gedit text editor will open for you. (you can also use “vi” text editor.)

3. Type the text shown below.

#! /bin/bash
clear
echo "HELLO WORLD ..........!!!"
echo
pwd

Writing file
Figure 1

 

4.  Now save it as “first.txt”

5. Go back to Terminal and go to the location(directory) where you saved first.txt (use “cd” command).

6. Then type:

chmod 755 first

7. Type ./first

8. That’s all. You will see the commands you type in the text file is executed by the Terminal one by one.

Further more

Let’ have a closer look at what we have done.



What's there
Figure 2

The first line of the file tells what Shell you are going to use. For this example “bash” is used. (there are number of shells in /bin folder)

“clear” is to clear the screen.

“echo” is use to print HELLO WORLD …………..!!! on the Terminal. (double quotation is not necessary)

Likewise you can type commands you use in the Terminal.

When executing file we must use “chmod 755” once. It’ll give the permission “executable” to the file.

After that you can change that file according to your necessity.

Resources:

  1. How to create a simple shell script on linux: http://www.linfo.org/create_shell_1.html
  2. What is Bash shell: http://computertechnos.blogspot.com/2008/08/what-is-ubuntu-bash-shell.html
  3. More about Bash shell: http://bash.cyberciti.biz/guide/Main_Page
Advertisements

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