Previous Table of Contents Next

Introduction to Shell Programming

This part introduces shell programming in two chapters: Chapter 16 introduces the basic concepts of shell programming and the three shells available with Solaris 2.x system software. It describes how shells work, describes the programming elements, and provides reference tables comparing shell syntax. Chapter 17 contains examples of shell scripts.

Understanding shell programs can help you interpret system scripts, such as the run control (rc) scripts, and write your own scripts to automate system administration tasks. Refer to these two chapters if you want to familiarize yourself with the basics of shell programming and to decide which shell language you want to use to perform a specific task. This book does not provide in-depth instructions for writing scripts in the Bourne, Korn, and C shell programming languages. Refer to one of the many books that have been written on the subject for complete instructions on how to use any of the shell programming languages. Refer to the Bibliography at the end of this book for a partial list of references.

Writing Shell

Basic Concepts
Input and Output
Testing Conditions
Controlling the Flow
Exit Status
Mathematical Operations
User-Defined Functions
Debugging Shell Scripts

SOLARIS 2.X SYSTEM SOFTWARE INCLUDES THREE SHELLS: BOURNE, KORN, AND C. EACH shell has its own high-level programming language that you can use to execute sequences of commands, select among alternative operations, perform logical tests, and repeat program actions. The Bourne and Korn shells use almost identical syntax, although the Korn shell is a superset of the Bourne shell and provides more functionality. The Bourne shell is used for most scripts that are distributed with Solaris 2.x system software. The C shell uses a syntax that is similar to C programming language syntax, and it has built-in capabilities not provided with the Bourne shell, such as history and array capability.

This chapter introduces the basic concepts of shell programming and the three shells, describes how shells work, and compares the syntax of the three shells; reference tables are provided throughout this chapter and are repeated in Chapter 17.

Basic Concepts

A shell is a specialized Solaris 2.x utility that provides an interface between the user and the operating system kernel. The kernel is the central core of the operating system and controls all basic aspects of a computer's operation. The kernel coordinates all of the executing utilities and manages the system's resources. The shell is a special command interpreter that invokes and interacts with the kernel to provide a way for users to execute utilities and other programs.

Each user is assigned a default shell that starts each time the user logs in to a system or opens a new Command Tool or Shell Tool window. The shell interprets the commands that it reads. You can type those commands directly into the shell at the prompt, or the shell can read the commands from a file. A file that contains shell commands is called a shell program or shell script.

Shell programs are interpreted, not compiled: The commands are read and executed one by one, in sequence. A compiled program, on the other hand, is initially read and converted to a form that can be directly executed by the CPU, and thereafter executed all at once. Because shell scripts are interpreted, even the fastest shell script always runs more slowly than an equivalent program written in a compiled language such as C.

Introducing Bourne, Korn, and C Shells

The Bourne, Korn, and C shells each have their own environment and syntax. Table 16-1 compares the initialization files that define the shell environment at startup.

Table 16-1 Shell Initialization Files

Feature Bourne Korn C
Read at login .profile .profile .login
Read at invocation of shell N/A Any file specified in .profile with ENV=.file .cshrc

The initialization files contain environment variables and other settings that configure the user's environment when a shell starts. Refer to the section "Environment Variables" for more information. The .profile (Bourne and Korn shells) and .login (C shell) files execute when a user logs in to a system. The Korn shell and .cshrc (C shell) environment files execute each time a new shell starts. Use these environment files to define aliases and functions for interactive use, and to set variables that you want to apply to the current shell.

Previous Table of Contents Next