Shell Scripts in UNIX:Facilities Offered by Unix Shells.

Module 13: Shell Scripts in UNIX

A Shell, as we remarked in module-1, offers a user an interface with the OS kernel. A user obtains OS services through an OS shell. When a user logs in, he has a login shell. The login shell offers the first interface that the user interacts with either as a terminal console, or through a window interface which emulates a terminal. A user needs this command interface to use an OS, and the login shell immediately provides that (as soon as user logs in). Come to think of it, a shell is essentially a process which is a command interpreter!! In this module we shall explore ways in which a shell supports enhancement in a user's productivity.

13.1 Facilities Offered by Unix Shells 

In Figure 13.1, we show how a user interacts with any Unix shell. Note that a shell distinguishes between the commands and a request to use a tool. A tool may have its own operational environment. In that case shell hands in the control to another environment. As an example, if a user wishes to use the editor tool vi, then we notice that it has its own states like edit and text mode, etc. In case we have a built-in command then it has a well- understood interpretation across all the shells. If it is a command which is particular to a specific shell, then it needs interpretation in the context of a specific shell by interpreting its semantics.

Every Unix shell offers two key facilities. In addition to an interactive command interpreter, it also offers a very useful programming environment. The shell programming environment accepts programs written in shell programming language. In later sections in this chapter we shall learn about the shell programming language and how to make use of it for enhancing productivity. A user obtains maximum gains when he learns not only to automate a sequence of commands but also to make choices within command sequences and invoke repeated executions.

clip_image001

Figure 13.1: A Unix system shell.

13.1.1 The Shell Families

One of the oldest shells is the Bourne shell. In most Unix machines it is available either in its original form or as BASH which expands to Bourne Again Shell. An improvement over the Bourne shell was the Korn shell. To properly enmesh with c language programming environment, a new family of shells came along. The first in this family is csh, the c shell. csh has its more recent version as tchs.

The Bourne shell is the primary shell. It has all the primary capabilities. The subsequent shells provided some extensions and some conveniences. Notwithstanding the differences, all shells follow a four-step operational pattern and that is:

1. Read a command line.

2. Parse and interpret it.

3. Invoke the execution of the command line.

4. Go to step 1.

When there are no more command lines, the shell simply awaits one. As a programming environment, a shell programming language allows a user to write a program, often called a shell script. Once a script is presented to a shell, it goes back to its four-step operational pattern and takes commands from the script (exactly as it would have done from the terminal). So what is the advantage of the script over the terminal usage? The advantages manifest in the form of automation. One does not have to repeatedly type the commands. One can make a batch of a set of such commands which need to be repeatedly performed and process the batch command script. We can even automate the decision steps to choose amongst alternative paths in command sequence.

In later sections, we shall study the syntax and associated semantics of shell programming language. We shall use Bourne shell as that is almost always available regardless of which of the Unix family OSs is being used.

13.1.2 Subshells

Since we have mentioned so many shells from different families, it is natural to be curious about the shell currently in use. The following Unix command tells us which is our current shell environment:

echo $SHELL

We shall examine the above command for some of the concepts associated with shell programming. The first part is the command echo and the second part is the argument $SHELL. The latter, in this case, is an environmental variable. First the command “echo". This command asks the shell environment to literally echo some thing based on the text that follows. We shall make use of this command very frequently in shell scripts either to prompt to ourselves some intermediate message, or show a value of interest. Every OS has some environmental variables. To see the values associated with various environmental variables just give the Unix command set or env .

• set /* shows values of all the environmental variables in use */

• env /* shows values of all the environmental variables in use */

In the response, we should get the value of $SHELL as the name of shell currently in use. This should be the same as the value which our echo command prompted. Next, the second part of the echo command. The $ in $SHELL yields a value. Try giving the echo command without a $ in front of SHELL and you will see that echo promptly responds with SHELL. That then explains the role of the leading $ on variable names. One may have user defined variables, as we shall see later, whose values too can be echoed using a leading $ symbol.

In Table 13.1 we show some typical settings for Bourne shell environmental variables. One may open a new shell within an existing shell. In the new shell, one may define variables or organize a control flow for a sequence of shell commands to be executed.

The variables defined in sub-shells scope to their nested level only. The nesting shell variables may be reassigned within the nested shell for local usage.

clip_image002

Table 13.1: A partial list of environmental variables.

Also, one can use a particular shell of his choice. For instance, suppose we wish to use scripts that are specific to Korn shell. We could enter a Korn shell from the current shell by giving the command ksh. To check the location of a specific shell use:

which shell-name

where which is a Unix command and shell-name is the name of the shell whose location you wished to know. To use scripts we need to create a file and use it within a new shell environment.

Comments

Popular posts from this blog

Input Output (IO) Management:HW/SW Interface and Management of Buffers.

Introduction to Operating Systems:Early History: The 1940s and 1950s

Input Output (IO) Management:IO Organization.