Sams Teach Yourself Shell Programming in 24 Hours
(Publisher: Macmillan Computer Publishing)
Author(s): Sriranga Veeraraghavan
ISBN: 0672314819
Publication Date: 01/01/99

Previous Table of Contents Next


Hour 2
Script Basics

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

The UNIX system consists of two components:

  Utilities
  The kernel


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 machine’s 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 machine’s 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 machine’s hard disk, places it in memory, and executes it. When the program finishes executing, it remains in the machine’s 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 machine’s hard disk, but the second and third time the date command usually remains in the machine’s 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.

Logging 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:

1.  Display the prompt login.
2.  Wait for a user to type a username.
3.  After a username has been entered, display the password prompt.
4.  Wait for a user to enter a password.
5.  Give the username and password entered by the user to the login command and exit.

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:

  On Solaris and FreeBSD, it is the Bourne shell.
  On HP-UX, it is the POSIX shell.
  On Linux, it is the Bourne Again shell.

Shell Initialization

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:

  /etc/profile
  profile

The process is as follows:

1.  The shell checks to see whether the file /etc/profile exists.
2.  If it exists, the shell reads it. Otherwise, this file is skipped. No error message is displayed.
3.  The shell checks to see whether the file .profile exists in your home directory. Your home directory is the directory that you start out in after you log in.
4.  If it exists, the shell reads it; otherwise, the shell skips it. No error message is displayed.

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