| Mobile | RSS

Basic Shell Scripting

May 31st, 2008 | 7 Comments | Posted in Information, Tutorials

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

About Kunal Gautam

Kunal Gautam a 22 Year Old geek and blogger from Pune, Maharastra, India, Is RHCE in RHEL 5.
Leave a Reply 2545 views, 1 so far today |
Follow Discussion

7 Responses to “Basic Shell Scripting”

  1. Bleu Scrinodeth Says:

    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.

  2. Sathya Says:

    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.

  3. Kunal Gautam Says:

    @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”

  4. Quiz_Master Says:

    Thanks 4 this.. Shell Scripting Paper (BCA 4th Sem)tomorrow.. Need to learn it overnight.!

  5. Kunal Gautam Says:

    @Quiz Master Best Wishes and rox in examination

  6. kanz Says:

    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).

  7. Chaos Says:

    “the file to make it executable file
    command is as follows
    chmod 777 firstscript.sh”  = RWX-Globally
    As others have said this is a very poor practice. Most Linux users that use these tutorials are new and that statement is false. There are better and MUCH safer ways to make a file executable: chmod +x  <file_name>in CLI, or in GUI environment, right click the file select properties, place a tick mark in “Make file Executable”. Now the script file is executable, and you do not have to worry about other users making changes to it.

Leave a Reply