Shell scripting Introduction

Shell:
Shell is command line interpreter. It takes commands from user and executes them. It is an interface between user and kernel. The three most widely used UNIX shells are Bourne shell, korn shell and c shell.
ShellDeveloped byShell PromtExecution Command
Bourne shellSteven courne$sh
Korn shellDavid  korn$Ksh
C shellBill joy, California university student%Csh
Each shell has merits and demerits of its own. Moreover the shell scripts written for one shell may not work with the other shell. This is because different shells use different mechanism to execute the commands in the shell script. Bourne shell since it is one of the most widely used unix shells in existence today.
Almost all unix implementation offer the Bourne shell as part of their standard configuration. It is smaller than the other two shells and therefore more efficient for most shell processing. However it lacks features offered by the c and korn shell.
All shell programs written for the Bourne shell are likely to work with the korn shell, the reverse however may not be true, this is so since the facilities like arrays, command aliasing and history mechanism available in the korn shell are not supported by the bourne shell.
The c shell programming language resembles the c language and is quite different from the language of the bourne shell, only the very basic shell scripts will run under both the c and bourne shell; a vast majority will not - shell keeps track of commands as you enter them (history) and allows you to go back and execute them again without typing the command. Or, if you want to, you can recall them, make modifications, and then execute the new command.
Shell Program:-
A shell program is a series of such commands, instead of specifying one job at a time, we give the shell to do - a program - that carries out an entire procedure, such programs are known as "shell scripts"
When to use shell scripts?
1) Customizing the work environments, for example, every time you log in if you want to see the current date, a welcome message and the list of users who have logged in, you can write a shell script for the same.
2) Automating your daily tasks, for example, you may want to back up all your programs at the end of day, this can be done using a shell script.
3) Automating repetitive tasks. For example repetitive task of compiling a C program, linking it with some libraries and executing the executable code can be a shell script.
4) Executing important system procedures like shutting down the system, formatting a disk, creating a file system, mounting the file system, letting the user the floppy and finally un mounting the disk.
5) Performing same operation on many files. Or example, you may want to replace a string with a string myprintf in all the c programs present in a directory.
Shell Variables:
Variables is, a data name and it is used to store value. Variable value can change during execution of the program.
Variables in Unix are two types
1) Unix-defined variable or system variables
These are standard variables which are always accessible the shell provides the values for these variable are usually used by the system  itself and govern the environment we work under. If we do desire we can change the values of these variables as per our preference and customize the system environment.
The list of all system variables and their values can be displayed by writing at the $promt,
$set
HOME=/usr/giri
HZ=100
IFS=
LOGNAME=giri
MALL=usr/spool/mail/giri
MAILCHECK=600
OPTINd=l
PATH=/bin :usr/b in:/usr/giri:/bin:
PS1=$
SHELL=/bin/sh
TERM=vt100
TZ=IST-5:30
VariableMeaning
PS1Primary shell prompt
PS2The system promt 2, default value is “>”
PATHDefines the path which the shell must search in order to execute any command or file
HOMEStores the default working directory of the user
LOGMANStores the login name of the user
MAILDefines the file where the mail of the user is stored
MAIL CHECKDefines the duration after which the shell checks whether user has received any mail. By default its value is 600 (seconds)
IFSDefines the name of your default working shell
SHELLDefines the name of your default working shell
TERMDefines the name of the terminal zone in which you are working
TZDefines the name of the zone in which we are working
2. User defined variables
These are defined by user and are used in most extensively in shell programming.
Rules for creating user defined shell variable: 
1) The first character of a variable name should be alphabet or underscore
2) No commas or blanks are allowed within a variable name
3) Variables names should be of any reasonable length 
4) variable name are case sensitive that is Name, nAme, name are all different names.
5) Variable name shouldn't be a reverse word.
Shell keywords: 
Keywords are the Words whose meaning has already been explained to the shell. The keywords are also called "Reverse Words"
The lists of keywords available in Bourne shell are
Echodoesac
ifshifteval
ReadDonebreak
elseExportexec
SetForcontinue
fiUntilulimit
unsettrapExit
whilecaseUmask
Readonlywaitreturn
1) Echo
echo command is used to display the messages on the screen and is used to display the value stored in a shell variable.
Ex 1: $echo "Multics is a training institute"
Multics is a training institute
Note:double quotes are optional in echo statement.
Ex 2: $echo "today date is 'date'"
Today date is sat mar 4 04:40:i0 IST 2005
Note: The unix command should be in back quotes in echo statement, otherwise it treats as text.
Ex 3: $echo "my file has we - I file lines"
My file has 10 lines
Ex 4: $echo my log name is: 'logname'
My log name is Giri
Ex 5: $"my present working directory is : 'pwd'"
My present working directory is :/usr/giri/abc
Shell variables:
Ex 1: $a=10
Note: There are no predefined data types in unix. Each and everything it treats as character. Each character occupies 1 byte of memory
Ex 2: $b2000
In above example a occupies 2 bytes and b occupies 4 bytes of memory.
reading of variable
$ is the operator to read variable value 
Ex 1: $n=100
$echo $n
100
Ex 2: $name="Multics"
$echo $name
Multics
$echo welcome to mutics
Ex 3: $now='date'
$$now
sat mar 4 04:40:i0 IST 2005 

Ex 4: $mypath=/usr/giri/abc/a1/a2
$cd $mypath
now it changes to a2 directory, to check say at $ promt pwd command $pwd
/usr/giri/abc/a1/a2
Null variables
A variable which has been defined but has not been given any value is known as a null variable. A null variable can be created in any of the following ways.
1) $n=""
2) $n="
3) $n=
$echo n
on echoing a null variable, only a blank line appears on the screen
Constant:
Constant is a fixed value. It doesn't change execution of the program
$a=20
$readonly a
When the variable are made readonly, the shell does not allow us to change their values, so value can read but can't change.
Note: If we want the shell to forget about a variable altogether, we use the unset command.
$unset a
on issuing the above command the variable a and with it the value assigned to it are erased from the shell's memory
Shell Programs
1. Was, to display list of files, the current working user's list and present working directory
$vi sp1
ls -x
who
pwd
:wq (save and quit)
Execution of shell program:
Sh is the command to execute bourne shell programs
Ex: $sh sp1
or $chmod 744 sp1
$./sp1
2. Was, to display address
$vi sp2
echo "Multics Scripting Institute"

No comments:

Post a Comment