Saturday, August 25, 2012

UNIX Shell - Script Basics



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

Logging In

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

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 blog 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.


Interactive Versus Noninteractive Shells


When the shell displays a prompt for you, it is running in interactive mode.

Interactive mode means that the shell expects to read input from you and execute the commands that you specify. This mode is called interactive because the shell is interacting with a user. This is usually the mode of the shell that most users are familiar with: you log in, execute some commands, and log out. When you log out using the exit command, the shell exits. The shell can be run in another mode, called noninteractive mode . In this mode, the shell does not interact with you; instead it reads commands stored in a file and executes them. When it reaches the end of the file, the shell exits.

How login Starts a Shell


When the login program starts a shell, it basically executes the following command:

/bin/sh

By issuing this command, it puts the shell into interactive mode. You can start a shell in interactive mode by issuing the same command at the prompt:

$ /bin/sh
$

The first prompt $ is displayed by the shell that login started; the second one is displayed by the shell you started. To exit from this shell, use the exit command:

$ exit
$

The prompt that is displayed now is from the original shell started by login. Typing exit at this prompt logs you out.

How to Start the Shell Noninteractively

You can start the shell noninteractively as follows:

$ /bin/sh filename

Here filename is the name of a file that contains commands to execute. As an example, consider the compound command:

$ date ; who

Put these commands into a file called logins. First open a file called logins in an editor and type the command shown previously. Assuming that the file is located in the current directory, after the file is saved, the command can run as

$ /bin/sh logins

This executes the compound command and displays its output.

This is the first example of a shell script . Basically, a shell script is a list of commands stored in a file that the  shell executes noninteractively.

Initialization File Contents

Usually the shell initialization files are quite short. They are designed to provide a complete working environment with as little overhead as possible for both interactive and noninteractive shells.
The file /etc/profile is maintained by the system administrator of your UNIX machine and contains shell initialization information required by all users on a system.
The file .profile is under your control. You can add as much shell customization information as you want to this file. The minimum set of information that you need to configure includes
  •  The type of terminal you are using
  •  A list of directories in which to locate commands
  •  A list of directories in which to locate manual pages for commands

Setting the Terminal Type

Usually the type of terminal you are using is automatically configured by either the login or getty programs. Sometimes, the autoconfiguration process guesses your terminal incorrectly. This can occur when you are using a dial-up or modem connection. If your terminal is set incorrectly, the output of commands might look strange, or you might not be able to interact with the shell properly. To make sure that this is not the case, most users set their terminal to the lowest common denominator as follows:

TERM=vt100

When I introduce the case statement in Chapter 10, "Flow Control," you will see a more advanced method of setting the terminal type that enables access to advanced terminal features.
Setting the PATH
When you type the command
$ date
the shell has to locate the command date before it can be executed. The PATH specifies the locations in which the shell should look for commands. Usually it is set as follows:

PATH=/bin:/usr/bin

If you request the shell to execute a command and it cannot find it in any of the directories given in the PATH variable, a message similar to the following appears:

$ hello
hello: not found

Setting the MANPATH

In UNIX, online help has been available since the beginning. In the section "Getting Help" I will discuss how to access it using the man command.
In order for you to access all the available help, you have to tell the shell where to look for the online help pages. This information is specified using the MANPATH. A common setting is 

MANPATH=/usr/man:/usr/share/man

Like the path, each of the individual entries separated by the colon character, :, are directories.
When you use the man command to request online help as follows, the man command searches every directory given in the MANPATH for an online help page corresponding to the topic you requested.

$ man who

In this case it looks for the online help page corresponding to the who command. If this page is found, it is displayed as discussed in the next section.

Making a Shell Script Executable

One of the most important tasks in writing shell scripts is making the shell script executable and making sure that the correct shell is invoked on the script.
In a previous example, you created the logins script that executes the following compound command:
date ; who ;
If you wanted to run the script by typing its name, you need to do two things:
  •  Make it executable.
  •  Make sure that the right shell is used when the script is run.
To make this script executable, do the following:

chmod a+x ./logins

To ensure that the correct shell is used to run the script, you must add the following "magic" line to the beginning of the script:

#!/bin/sh

Your script then has two lines:

#/bin/sh
date ; who ;

The magic line causes a new shell (in this case, /bin/sh) to be called to execute the script. Without the magic line, the current shell is always used to evaluate the script, regardless of which shell the script was written for. For example, without a magic line, csh and tcsh users might not be able to get a Bourne shell (sh) script to run correctly.

The Magic of #!/bin/sh
The #!/bin/sh must be the first line of a shell script in order for sh to be used to run the script. If this appears on any other line, it is treated as a comment and ignored by all shells.

The magic first line #!/bin/sh introduces the topic of comments. A comment is a statement that is embedded  in a shell script but should not be executed by the shell.
In shell scripts, comments start with the # character. Everything between the # and end of the line are considered part of the comment and are ignored by the shell. 
Adding comments to a script is quite simple: Open the script using an editor and add lines that start with the
# character. 
For example, to add the following line to the logins shell script:
# print out the date and who's logged on

I opened the file logins with my editor and inserted this line as the second line in the file. The shell script is now as follows:

#!/bin/sh
# print out the date and who's logged on
date ; who ;

There is no change in the output of the script because comments are ignored. Also comments do not slow down a script because the shell can easily skip them.

You can also add comments to lines that contain commands by adding the # character after the commands.
For example, you can add a comment to the line date ; who ; as follows:

date ; who ; # execute the date and who commands

When you are writing a shell script, make sure to use comments to explain what you are doing in case someone else has to look at your shell script. You might find that this helps you figure out what your own scripts are doing, months after you write them.

Getting Help

As you read through this blog, you will want to get more information about the commands and features I discuss. This information is available by using the online help feature of UNIX. Every version of UNIX  comes with an extensive collection of online help pages called manual pages. These are often referred to as man pages . The man pages are the authoritative source about your UNIX system. They contain complete information about both the kernel and all the utilities.

Using the man Command

To access a man page you need to use the man (man as in manual) command as follows:

man command

Here, command is the name of a command that you want more information about. As an example,

$ man uptime

displays the following on a Solaris machine:

User Commands uptime(1)
NAME
uptime - show how long the system has been up
SYNOPSIS
uptime
DESCRIPTION
The uptime command prints the current time, the length of time the system has been up, and the average number of jobs in the run queue over the last 1, 5 and 15 minutes. It is, essentially, the first line of a w(1) command.
EXAMPLE
Below is an example of the output uptime provides:
example% uptime
10:47am up 27 day(s), 50 mins, 1 user, load average: 0.18, 0.26, 0.20
SEE ALSO
w(1), who(1), whodo(1M), attributes(5)
NOTES
who -b gives the time the system was last booted.

Sections in a Man Page

Section
Description
NAME
This section gives the name of the command along with a short description of the
command.
SYNOPSIS
This section describes all the different modes in which the command can be run. If a
command accepts arguments they are shown in this section.
DESCRIPTION
This section includes a verbose description of the command. If a command accepts
arguments, each argument will be fully explained in this section
EXAMPLE
This section usually shows you how to execute a command, along with some sample
output.
SEE ALSO
This section lists other commands that are related to this command.
NOTES
This section usually lists some additional information about the command. Sometimes it
lists known bugs with a particular command.
Try using the man command to get more information on some of the commands I have discussed in this chapter.
If the man command cannot find a man page corresponding to the command you requested, it issues an error message. For example, the command

$ man apple

produces an error message similar to the following on my system:

No manual entry for apple

The exact error message depends on your version of UNIX.

Summary

In this chapter, I explained in greater detail what the shell is and how it works. You saw how the login process works and what the login command does to start a shell. From this you were able to look at the two modes in which the shell can be run:
  •  Interactively
  •  Noninteractively
In shell programming, the noninteractive use of the shell should interest you the most. This mode enables you to specify commands inside a file and then have the shell execute the commands within that file. You also saw how to make a file containing commands executable. This enables you to treat shell scripts as new commands.You also looked at some details of shell initialization and getting help using the man command.

The next chapter formally introduces the concept of files by showing you how to list files, view the contents of files, and manipulate files.

Questions

1. What are the two files used by the shell to initialize itself?

2. Why do you need to set PATH and MANPATH?

3. What purpose does the following line
#!/bin/sh
serve in a script?

4. What command should you use to access the online help?


Terms

Utilities Utilities are programs, such as who and date, that you can run or execute.

Commands A command is the name of a program and any arguments you specify to that program to cause its behavior to change. You might see the term command used instead of the term utility for simple commands, where only the program name to execute is given.

Kernel 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.

Uninitialized Shell When a shell is started it is uninitialized. This means that important parameters required  by the shell to function correctly are not defined.

Shell Initialization After a shell is started it undergoes a phase called initialization to set up some important parameters. This is usually a two step process that involves the shell reading the files /etc/profile and .profile.

Interactive Mode In interactive mode the shell reads input from you and executes the commands that you specify. This mode is called interactive because the shell is interacting with a user.

Noninteractive Mode In noninteractive mode, the shell does not interact with you; instead it reads commands stored in a file and executes them. When it reaches the end of the file, the shell exits.

Shell Script A shell script is a list of commands stored in a file that the shell executes noninteractively.

Home Directory Your home directory is the directory in which you start out after you log in.

Comments A comment is a statement that is embedded in a shell script but should not be executed by the shell.

Man Pages Every version of UNIX comes with an extensive collection of online help pages called man pages (short for manual pages ). The man pages are the authoritative source about your UNIX system. They contain complete information about both the kernel and all the utilities.

Next Topic --- Working with Files

No comments:

Post a Comment