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


Using the Operating System Release Level

As previously mentioned, SunOS is the name of the UNIX operating system developed by Sun Microsystems. SunOS was originally based on BSD UNIX but has since changed to be based on System V UNIX. Although Sun Microsystems changed the marketing name of the new version to Solaris, both versions produce the output SunOS when uname is run.

To use the correct versions of commands, shell scripts that have to run on both Solaris and the old SunOS must be able to detect the difference between these two versions.

To determine whether a system is running Solaris or SunOS, you need to determine the version of the operating system. SunOS versions 5 and higher are Solaris (System V–based); SunOS versions 4 and lower are SunOS (BSD-based).

To determine the version of the operating system, use the -r option of uname:

$ uname –r
5.5.1

This indicates that the version of the operating system is 5.5.1. If you want to add the operating system’s name to this output, use the -r and the -s options:

$ uname –rs
SunOS 5.5.1

This indicates the machine is running Solaris. A machine running the BSD-based SunOS displays the following output:

SunOS 4.1.3

Determining the Hardware Type

Sometimes a shell script is written as a wrapper around a hardware-specific program. For example, install scripts are usually the same for different hardware platforms supported by a particular operating system. Although the install script might be the same for every hardware platform, the files that are installed are usually different.

To determine the hardware type, use the -m option of the uname command:

$ uname –m
sun4m

Some common return values and their hardware types are given in Table 23.3.

Table 23.3 Hardware Types Returned by the uname Command

Hardware Description

9000/xxx Hewlett-Packard 9000 series workstation. Some common values of xxx are 700, 712, 715, and 750.
i386 Intel 386-, 486-, Pentium-, or Pentium II–based workstation.
sun4x A Sun Microsystems workstation. Some common values of x are c (SparcStation 1 and 2), m (SparcStation 10 and 20), and u (UltraSparc).
alpha A workstation based on the Digital Electronics Corporation ALPHA microprocessor.

Determining the hostname of a System

Many shell scripts need to check the hostname of a system. The traditional method of doing this on BSD systems is to use the hostname command, as in the following example:

$ hostname
soda.CSUA.Berkeley.EDU

In System V, the hostname command is not always available. The uname -n command is used instead:

$ uname –n
kashi

Because the uname -n command is available on both System V and BSD UNIX, it is preferred for use in portable shell scripts.

Determining the UNIX Version Using a Function

Now that you have looked at using the uname command to gather information about the version of UNIX that is being used, you need a method for using this information in a shell script. As you saw in Chapter 21, “Problem Solving With Functions,” creating a shell function that determines the version of UNIX gives the greatest flexibility.

A shell function that returns the operating system type is as follows:

getOSName() {
    case `uname -s` in
        *BSD)
            echo bsd ;;
        SunOS)
            case `uname -r` in
                5.*) echo solaris ;;
                  *) echo sunos ;;
            esac
            ;;
        Linux)
            echo linux ;;
        HP-UX)
            echo hpux ;;
        AIX)
            echo aix ;;
        *) echo unknown ;;
    esac
}

As you can see, this function is not very complicated. It checks the output of uname -s and looks for a match. In the case of SunOS, it also checks the output of uname -r to determine whether the operating system is Solaris or SunOS.

In many cases, you need to tailor the options of a command, such as ps or df, so that the command can generate the desired output. In such cases, you need the capability to “ask” whether the operating system is of a certain type. A shell function that performs this task follows:

isOS() {
    if [ $# -lt 1 ] ; then
        echo "ERROR: Insufficient Aruments." >&2
        return 1
    fi

    REQ=`echo $1 | tr '[A-Z]' '[a-z]'`
    if [ "$REQ" = "`getOSName`" ] ; then return 0 ; fi
    return 1
}

This function compares its first argument to the output of the function getOSName and returns 0 (true) if they are the same; otherwise, it returns 1 (false). Using this function, you write if statements of the following type:

if isOS hpux ; then
    : # HP-UX specific commands here
elif isOS solaris ; then
    : # Solaris specific comands here
else
    : # generic unix commands here
fi

The reason that you do not directly check the value of $1 but instead use the variable REQ, is that this enables a greater flexibility on the part of the function’s user. For example, you can use either of the following to check whether a system is Linux:

isOS LINUX
isOS linux

Techniques for Increasing Portability

Shell scripts that run on multiple versions of UNIX often include code that is version-specific. For example, you might need to use a different command on Linux than Solaris to obtain some system information.

There are two common techniques to increase the portability of a shell script between different versions of UNIX:

  Conditional execution
  Abstraction

Conditional execution alters the execution of a script based on the system type, whereas abstraction retains the same basic flow of the script by placing the conditional statements within functions.


Previous Table of Contents Next