Basic Shell Scripting

Hello to all. Sorry for delayed posting of posts.I was little bit busy in Server settings of RHEL.

So you have in mind what is shell scripting ????

Shell scripts or simply scripts are collection or group of command that are stored in a file.Now you will think what’s the use of it ?
Actually it is not possible to run each and every command manually on pc.If this is a repetative job then its again became a headache. So we use Scripting to store those commands and get Outputs. Even we can sheduled the script to run at particular time.

So here we start to write a simple script which will print “Hello World”

Step One
Create a file name firstscript.sh [ remember to give it .sh extension ]
Now open any text editor of your choice and write following commands

Step two : Commands [ Below this Command Begin , Description Given after the script is completed ]

#! /bin/bash
# You Can Comment any thing here
# make sure u use a hash in begningecho “Hello World”# End of Script

 

Description
First Line should define which shell you are using
there are several shells present in unix system, some of the following are :-
/bin/bash Bash Shell
/bin/sh Bourne Shell
/bin/csh C Shell
/usr/bin/perl Perl Script Shell
/usr/bin/python Python Script Shell

The First Line should start with “#!” Followed by Shell
eg
#! /bin/sh
#! /bin/csh
#! /usr/bin/perl

Then give commands in the file like I’ve supplied “echo” Command in this script
or you can even give direct commands also
like
#! /bin/bash
date
cal
clear

It will give output of date & cal command subsequently and clear screen

You can even provide comment to a command just use a hash “#” in line and it will act as comment

Step 3
Now question arises how to run this script ???
its very simple
there are two methods
Method 1
supply following command
sh firstscript.sh

Method 2
Chmod[chmod command change permission of the file] the file to make it executable file
command is as follows
chmod 777 firstscript.sh

and run script by supplying following thing in your commadn terminal
./firstscript.sh

Stay tuned for more articles on shell scripting

By | 2018-04-09T06:43:41+00:00 May 31st, 2008|Information, Tutorials|7 Comments

About the Author: