03-12-2012, 01:36 PM
Unix Shell Scripts
1Unix Shell.pdf (Size: 89.25 KB / Downloads: 67)
Introduction
In previous discussions we have talked about many of the facilities of the C shell, such as command aliasing,
job control, etc. In addition, any collection of csh commands may be stored in a file, and csh can be invoked
to execute the commands in that file. Such a file is known as a shell script file. The language used in
that file is called shell script language. Like other programming languages it has variables and flow control
statements (e.g. if-then-else, while, for, goto).
In Unix there are several shells that can be used, the C shell (csh and its extension, the T C shell tcsh), the
Bourne Shell (sh and its extensions the Bourne Again Shell bash and the highly programmable Korn shell
ksh ) being the more commonly used.
Note that you can run any shell simply by typing its name. For example, if I am now running csh and wish
to switch to ksh, I simply type ksh, and a Korn shell will start up for me. All my commands from that point
on will be read and processed by the Korn shell (though when I eventually want to log off, exiting the Korn
shell will still leave me in the C shell, so I will have to exit from it too).
Shell Variables
Like other programming languages the csh language has variables. Some variables are used to control the
operation of the shell, such as $path and $history, which we discussed earlier. Other variables can be created
and used to control the operation of a shell script file.
Setting Variables
Values of shell variable are all character-based: A value is formally defined to be a list of zero or more
elements, and an element is formally defined to be a character string. In other words, a shell variable
consists of an array of strings.
Command Arguments
Most commands have arguments (parameters), and these are accessible via the shell variable $argv. The first
parameter will be $argv[1], the second $argv[2], and so on. You can also refer to them as $1, $2, etc. The
number of such arguments (analogous to argc in the C language) is $#argv.