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
June 1st, 2008 at 11:52 am
There are several errors in this post.
First problem is that the commands will print to the screen and immediately be cleared. Not a big deal, but hardly a great example for new scripters.
The second is in Method 1. If you run your script with sh, which is symlinked to bash, then ksh and csh scripts will fail. Again, not a big deal for running external commands, but will cause failures when you try to run commands specific to a shell.
The third is in Method 2. To make a file executable there is no need to make it world writable. This is a grievous error to introduce to new scripters. “chmod 755 firstscript.sh” or “chmod +x firstscript.sh” will add execute permissions without making the file world readable.
June 1st, 2008 at 11:54 am
Nice tut. You dont need the .sh extension, unlike Windows, *nix dont need file extensions to disnguish filetypes.
The first line, is called the HashBang or She-Bang
PS: Tutorials spelling is wrong, and delete the ps part after correcting.
Cheers.
June 1st, 2008 at 12:08 pm
@sathya & Bleu Scrinodeth Thanks a ton
Agreed we dont need to provide any Extension to files But still for better recognistion of files I’ve placed .sh extension
CHMOD all to 777 will make it globally read.write&executables for all users and groups [ if script is used on multiuser platform ]
yes “sh” command is required to run sh and bash shell script. To run c shell script simply supply “csh filename”
June 1st, 2008 at 12:25 pm
Thanks 4 this.. Shell Scripting Paper (BCA 4th Sem)tomorrow.. Need to learn it overnight.!
June 1st, 2008 at 1:01 pm
@Quiz Master Best Wishes and rox in examination
October 1st, 2008 at 10:49 am
just a correction: chmod 755 firstscript.sh” or “chmod +x firstscript.sh” will add execute permissions without making the file world writable(not readable as mentioned above).