Previous | Table of Contents | Next |
In Chapter 1, Shell Basics, I introduced the concept of a shell and commands. I showed you how the shell reads your input and executes the command you requested.
In this chapter I will explain in detail what the shell is and how it works. You will learn how the shell is started during the login process and what happens when you log out.
After I explain this behavior, I will show you how to group commands that are normally executed interactively into a file to create a script. Scripts are the power behind the shell because they enable you to group commands together to create new commands.
The UNIX system consists of two components:
Utilities are programs you can run or execute. The programs who and date that you saw in the previous chapter are examples of utilities. Almost every program that you know is considered a utility.
Commands are slightly different than utilities. The term utility refers to the name of a program, whereas the term command refers to the program and any arguments you specify to that program to change its behavior. You might see the term command used instead of the term utility for simple commands, where only the program name to execute is given.
The kernel is the heart of the UNIX system. It provides utilities with a means of accessing a machines hardware. It also handles the scheduling and execution of commands.
When a machine is turned off, both the kernel and the utilities are stored on the machines hard disks. But when the computer is booted, the kernel is loaded from disk into memory. The kernel remains in memory until the machine is turned off.
Utilities, on the other hand, are stored on disk and loaded into memory only when they are executed. For example, when you execute the command
$ who
the kernel loads the who command from the machines hard disk, places it in memory, and executes it. When the program finishes executing, it remains in the machines memory for a short period of time before it is removed. This enables frequently used commands to execute faster. Consider what happens when you execute the date command three times:
$ date Sun Dec 27 09:42:37 PST 1998 $ date Sun Dec 27 09:42:38 PST 1998 $ date Sun Dec 27 09:42:39 PST 1998
The first time the date command can be loaded from the machines hard disk, but the second and third time the date command usually remains in the machines memory allowing it to execute faster.
The shell is a program similar to the who command. The main difference is that the shell is loaded into memory when you log in.
When you first connect to a UNIX system, you usually see a prompt such as the following:
login:
You need to enter your username at this prompt. After you enter your username, another prompt is presented:
login: ranga Password:
You need to enter your password at this prompt.
These two prompts are presented by a program called getty. These are its tasks:
After login receives your username and password, it looks through the file /etc/passwd for an entry matching the information you provided. If it finds a match, login executes a shell and exits.
As an example, on my system the matching entry for my username, ranga, in file /etc/passwd is:
ranga:x:500:100:Sriranga Veeraraghavan:/home/ranga:/bin/bash
As you progress through the book, I will explain the information stored here.
Note:
For those readers who are not familiar with UNIX files or filenames such as /etc/passwd, this topic is covered extensively in Chapters 3, Working with Files, and 4, Working with Directories.I will discuss files briefly in this chapter. A general idea from other operating systems of what files are is enough to understand these examples.
If no match is found, the login program issues an error message and exits. At this point the getty program takes over and displays a new login prompt.
The shell that login executes is specified in the file /etc/passwd. Usually this is one of the shells that I covered in the previous chapter.
In this book I assume that the shell started by the login program is /bin/sh. Depending on the version of UNIX you are running, this might or might not be the Bourne shell:
When the login program executes a shell, that shell is uninitialized. When a shell is uninitialized, important parameters required by the shell to function correctly are not defined.
The shell undergoes a phase called initialization to set up these parameters. This is usually a two step process that involves the shell reading the following files:
The process is as follows:
As soon as both of these files have been read, the shell displays a prompt:
$
This is the prompt where you can enter commands in order to have them execute.
Note:
The shell initialization process detailed here applies to all Bourne type shells, but some additional files are used by bash and ksh.You can obtain more information about this process for a particular shell using the man command explained later in this chapter.
Previous | Table of Contents | Next |