Shell Script

                                       SHELL SCRIPTING

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 .kom shell and c shell.


Shell
Developed by
Shell
Prompt
Execution
Command
Bash shell
Denis Richie
$
sh
Bourne shell
Steven courne
$
Sh
Korn shell
David korn
$
Ksh
C shell
Bill 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 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 implementations 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 Boume 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.

chsh


Shell program:-
     A shell program is nothing but a series of such commands, instead of specifying one job at a time, we give the shell a to -do list —a program — that carries out an entire procedure. such programs are known as “shell scripts”.

When to use shell scripts:

  1. Customizing your 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 a 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. f xth1e, the 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 unmounting the disk.
  5. Performing same operation on many files. Or example, you may want to rep lace a string with a string myprintf in all the c programs present in a directory
Shell Variables:-
           Variable 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 variables or system variables
2. User defined variables

Unix-defined variables:

          These are standard variables which are always accessible the shell provides the values for these variables these variable are usually used by the system itself and govern the environment we work under. If we so desire we can change the values of these variables as per our preferences and customize the system environment.
          The list of all system variables and their values can be displayed by
Saying at the $prompt,
$set
HOME=/usr/sv
HZ=100
IFS=
LOGNAME=sv
MAII=usr/spool/mail/sv
MAILCHECK=600
OPTINd=l
PATH=/bin :Iusr/b in:/usr/sv:/bin:.
PS1=$
SHELL=/bin/sh
TERM=vt100
TZ=IST-5:30

Variable
Meaning
PS1
Primary shell prompt
PS2
The system prompt 2, default value is “>”
PATH
Defines the path which the shell must search in order to execute any command or file
HOME
Stores the de fault working directory of the user
LOGMAN
Stores the login name of  the user
MAIL
Defines the file where the mail of the user is stored
MAILCHECK
Defines the duration after which the shell checks whether the user has received any mail. By default its value is 600(seconds)
IFS
Defines the name of your default working shell
SHELL
Defines the name of your default working shell
TERM
Define the name of the terminal zone in which you are working
TZ
Defines the name of the Zone in which we are working


User Defined Variables
 These are defined by user and are used most extensively in shell programming.

Rules for creating User Defined Shell Variables: -
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 names are case sensitive, that isName, nAme, name are all different variable names.
5.   Variable name shouldn’t be a reserve word
Shell Keywords:-
Keywords are the Words whose meaning has already been explained to the shell. The keywords are .also called as “Reserve Words”.
The lists of keywords available in Bourne shell are

Echo
if
Read
else
Set
fi
unset
while
Readonly
do
Shift
Done
Export
For

Until
trap
case
wait
esac
eval
break
exec
continue
ulimit
Exit
Umask
return



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.

Eg 1: $echo “Multics is a training institute”
            Multics is a training institute

Note: double quotes are option in echo statement.

Eg 2: $echo “today date is:`date`”
            Today date is sat mar 4 o4:40:i0 1ST 2005
Note: the UNIX command should be in back quotes in echo statement
Otherwise it treats, as text.
Eg 3: $echo “my file has we —I file lines
               My file has 10 lines
Eg 4: $echo my log name is:`logname`.
             My logname is Sv
Eg 5: $echo “my present working directory is :`pwd` ”
                My present working directory is :/usr/sv/abc

Shell variables
Eg 1: $a=10
Note: there are no predefined data types in UNIX. Each and every thing it treats as character. Each character occupies 1 byte of memory.

Eg 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
Eg 1: $n=l00
          $echo $n
             100
Eg 2: $name=”Multics”
          $echo $name
              Multics
$echo welcome to $name
Welcome to Multics
Eg 3: $now=`date`
          $$now
           sat mar 4 o4:40:10 1ST 2005

Eg 4: $mypath=/usr/sv/abc/a 1 /a2
           $cd $mypath
now it changes to a2 directory, to check say at $ promt pwd command $pwd
/usr/sv/abc/a 1 /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 during execution of  the  program
$a=20
$readonly  a

When the variable are made readonly, the shell doe not allow us to change their values, so a 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.


No comments:

Post a Comment