Tuesday, 21 June 2011

Shell Scripting - Getting Started


What is shell ?
A shell is a command interpreter which accepts commands from standard i/o devices like the keyboard and makes operating system calls in correspondence to the interpreted commands. It  is not a part of the kernel but instead is an interface between the user and the kernel.

For example the command ls lists the files in the current directory.
The shell displays its prompt, shown here as $, and then passively awaits your commands. When you type a command and press Enter, you are telling the shell to execute your command.

Few of the many shells available in Linux









To find out which shells are available in the your linux box

$cat /etc/shells

In my case the output was

[jaikumar@localhost ~]$ cat /etc/shells
/bin/sh
/bin/bash
/sbin/nologin
/bin/zsh

What is a shell script?
The shell interpreter accepts commands from the user and then takes the action accordingly. But if we had a sequence of commands to be executed, it would be such a waste of time to enter them one by one on the shell interpreter. The solution to this problem is to write down the sequence of commands in a file and execute this file. This file is called 'shell script'.
Thus Shell script is just a sequence of commands written down to a file. Wait ! Doesn't that sound like a batch file in MS-DOS ? Yes it does, but the shell script is very powerful in terms of the available commands at user's disposal and its usage.


Installing Cygwin (Unix Environment) on Windows
Cygwin is the name for a set of packages that provide a surprisingly large number of Unix-like utilities for Windows systems. With Cygwin installed, you can make your Windows system act very much like a Unix system, at least from a user perspective.
For shells, Cygwin includes ash, bash, tcsh, zsh, and pdksh. The default shell is bash .For Download and install instructions of the Cygwin package refer www.cygwin.com.
Because the Cygwin package includes a number of shells, as well as a plethora of commands, this is probably the best shell package for Windows users.

First Shell Script
1. First we need an editor like vi or nano to write the shell script. We will use vi since it provides syntax highlighting.
2. Open vi at the prompt.

$vi first.sh (You can also use this without .sh extension $ vi first)

3. Press 'esc + i' to enter into insert mode.
4. Now write this simple shell script


The first statement prefixed by # is a comment. Thus # followed by any text is considered as a comment. 
The command clear clears the terminal screen. 
The command echo prints the the statement "This is my first shell script" on the terminal.
5. Press 'esc+:+wq' to write the script on the file and quit the vi editor.
6. To run the script 

$chmod 755 first.sh


This will set read write execute(7) permission for owner, for group and other permission is read and execute only(5).

$./first.sh (in case if file is without extension then $./first)
or
$sh first.sh (in case if file is without extension then $sh first)


 vi reference guide command list










No comments:

Post a Comment

Popular Posts