Wednesday, August 19, 2009

Shell Scripting

Here I am trying to give kind of a basic idea for people who are interested in shell scripting. Today in computer world Linux is the most famous topic therefor I thought of writing something which is very helpful for Linux users.

What do you need to write Shell Scripts?
Simply you need only the Linux OS and it's bash shell (Available with almost all Linux Distributions. By default bash is default shell for Red Hat Linux Distribution).

What is Linux Shell?
Computer understand the language of 0's and 1's and this is called binary language. Shell is a user program which helps user to interact with the kernel. Simply it takes user's commands in English and if it is a valid command it is passed to the kernel. Commands which were mentioned above are Linux Commands.

What is a Shell Script?
Shell script is a series of commands which are written in a plain text file. Shell script something similar to MS-DOS batch file but it is more powerful than the MS-DOS batch file.

How to write your first Shell Script?
1. Use any editor like 'vim' to write the shell script.
2. After writing the script set the execute permit ion to your
shell script as follows,

chmod 755 your_shell_script_name

Note: This will set read write execute (7) permit ion to owner and for group and other permition is read and execute(5).

3. Here we go...Now the script can be run by using one of following three ways,

Assume your shell script name is "myFirstScript"
2)bash myFirstScript
3)sh myFirstScript

following are some examples for shell scripts.
example 1

prints "Hello World" on the screen.
echo "Hello World"
example 2
Script to print user information who currently login,current date& time
echo "Hello $USER"
echo "Today is ";date
echo "Number of user login :";who | wc -l
echo "Calender"
exit 0
#end of script
example 3
This read your name from the key board and print it.
echo "Your name please :"
read name
echo "Hello $name, lets be friend!"

If else in Shell Scripting
if condition
condition is zero (true - 0)
execute all commands up to else statement
if condition is not true then
execute all commands up to fi
example 4
if [ $# -eq 0 ]
echo "$0 : You must type one integer"
exit 1
if test $1 -gt 0
echo "$1 is a positive number"
echo "$1 is a negative number"
Run the above script as follows,
./script_name integer_number

assuming sript name is "test"
./test 10

output is as follows,
10 is a positive number

Detailed explanation
First script checks whether command line argument is given or not, if not given then it print error message as "./test : You must type one integer". if statement checks whether number of argument ($#) passed to script is not equal (-eq) to 0, if we passed any argument to script then this if statement is false and if no command line argument is given then this if statement is true.

Those are few examples for you to get an idea what shell script is. Hope you would enjoy with note. Finally there are lot more about Shell Scripts. Here hoped to give you a brief introducion.


  1. wow...It was realy useful machan!!!
    Thanx ;-)

  2. well done dude..great work as a CS student..
    hope u ll add many more study guidance like dis in future...

  3. Excellent post! Nicely structured and well written. Keep it up! :)