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 23
Scripting for Portability

Shell programming is an important part of UNIX because shell scripts are portable between different versions of UNIX. In many cases, no changes are required for a shell script to function correctly on multiple systems.

The easiest way to ensure that your shell scripts are completely portable is to restrict yourself to using only those commands and features that are available on all versions of UNIX. Sometimes, you have to implement workarounds to deal with the limitations of a particular version of UNIX.

In this chapter, you will first learn how to determine which version of UNIX is running. Then you will learn how to adapt your shell scripts to different versions of UNIX by examining some of the problems encountered when porting scripts between the versions.

Determining UNIX Versions

Before you can begin adjusting shell scripts to be portable, you need to know what the different types of UNIX are and how to tell them apart.

The two major types of UNIX are

  BSD (Berkeley Software Distribution)
  System V

The locations of commands and the options supported by certain commands are different between these two types of UNIX. This chapter highlights the major differences and commands in particular.

BSD Versus System V

BSD UNIX was developed by the Computer Systems Research Group at the University of California at Berkeley. In the early 1980s, the University of California acquired the source code to UNIX from AT&T Bell Labs and significantly modified it to produce BSD UNIX.

Although the University of California has stopped distributing BSD UNIX, current versions of it are available from many sources. The most common versions of BSD are OpenBSD, NetBSD, and FreeBSD. Some older machines from Sun Microsystems run a modified version of BSD called SunOS.

System V (sometimes abbreviated as SysV) is the latest version of UNIX released by AT&T Bell Labs. It is based on the original version of UNIX developed in the early 1970s. System V UNIX is the standard for commercial versions of UNIX. Both Solaris (the newest version of SunOS) and HP-UX are based on System V UNIX.

The main difference between BSD UNIX and System V UNIX is in system administration and networking. System V UNIX is newer than BSD UNIX and provides many standardized tools for configuring a system, installing prepackaged software, and network programming.

Also, the layout of the file system in System V UNIX has changed to some extent. Table 23.1 lists the BSD directories and their System V equivalents.

Table 23.1 System V Equivalents of BSD Directories

BSD System V

/bin /usr/bin
/sbin /usr/sbin
/usr/adm /var/adm
/usr/mail /var/mail or /var/spool/mail
/usr/tmp /var/tmp

The directories /bin and /sbin still exist on some System V–based UNIX versions. On Solaris, these directories are links to /usr/bin and /usr/sbin, respectively. On HP-UX, these directories still contain some commands essential at boot time. The commands stored in these directories are not the same commands as in BSD. Most vendors who have switched from BSD to System V still provide BSD versions in the directory /usr/ucb.

In addition to these changes, many System V–based UNIX versions have introduced the directory /opt in an attempt to standardize the installation locations of prepackaged software products. On older systems, many different locations, including /usr, /usr/ contrib, and /usr/local, were used to install optional software packages.

Linux is hard to classify because it is not based on either BSD or System V source code. It was written from scratch by Linus Torvalds at the University of Helsinki in Finland and is considered by some to be a third type of UNIX that incorporates the best features found in both System V and BSD. The commands and the networking layer in Linux are both based on BSD, whereas the standardized tools for system configuration and installation of prepackaged software are similar to System V. Some of the major vendors of Linux are Caldera and Red Hat.

Using uname

The first step in writing portable shell scripts is to determine which version of UNIX is executing your shell script. You can determine this using the uname command:

uname options

Here, options is one or more of the options given in Table 23.2.

Table 23.2 Options for the uname Command

Option Description

-a Prints all information
-m Prints the current hardware type
-n Prints the hostname of the system
-r Prints the operating system release level
-s Prints the name of the operating system (default)

By default, the uname command prints the name of the operating system. The output looks like the following:

$ uname
Linux

Here, the output indicates that the operating system name of the machine is Linux. Usually, this is enough to determine the UNIX version. For example, on FreeBSD systems, the output is FreeBSD and on HP-UX systems the output is HP-UX. The major exception to this is SunOS.


Previous Table of Contents Next