2007/12/25

The UNIX System Linux shellscript

The UNIX System
Logging In
The UNIX system consists of two components:
l Utilities
l The kernel
Utilities are programs you can run or execute. The programs who and date that you saw in the
previous 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 Working with Directories.
I will discuss files briefly . 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 .
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:
l On Solaris and FreeBSD, it is the Bourne shell.
l On HP-UX, it is the POSIX shell.
l On Linux, it is the Bourne Again shell.


The UNIX System Summary
Shell Initialization Questions
Getting Help Terms
Previous Section Next Section
Shell Initialization
Interactive Versus Noninteractive Shells Making a Shell Script Executable
Initialization File Contents
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:
l /etc/profile
l 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.
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
l The type of terminal you are using
l A list of directories in which to locate commands
l 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, "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
Each of the individual entries separated by the colon character, :, are directories. Directories are discussed.
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:
l Make it executable.
l 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
Here you are using the chmod command. For a complete discussion of how to use this command.
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.
Comments
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.

No comments: