14-11-2012, 01:58 PM
Unix Shell Scripts
Unix Shell Scripts.pdf (Size: 89.25 KB / Downloads: 56)
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.
Escape Characters
If you download files from the Web, they may have been created under Windows, with names inconsistent
with Unix. Here are a couple of tips for handling this:
The most common problem is file names with embedded spaces, say a file named before July. To
reference such a file from a C shell command line, simply precede each space by a backslash. For
instance, to remove the file before July, type