Previous Table of Contents Next


Keyword Commands

A keyword command is a shell programming construct. The command must begin with one of the following unquoted words.

     if then else elif fi case esac for while until do done function { }

Keywords must be preceded and followed by a blank. For example,

     {echo A FILE ; cat /etc/group}

results in two errors. The first is "{echo" is not a valid command. The second is that "/etc/group}" is not a valid file. But the command,

     { echo A FILE ; cat /etc/group }

works properly because the braces are interpreted correctly as keyword commands.

Pipeline

A pipeline is one or more simple or keyword commands connected by a vertical bar (|). Each command is executed as a separate process. In a pipeline the standard output of the previous command is "piped" into the standard input of the following command. The shell waits for the last command in the pipeline to complete. The exit status of a pipeline is the return code of the last command in the pipeline. The general format of the pipeline follows.

     command | command

For example,

     nawk -F: '{print $1}' /etc/group | sort | pr -t4

generates a list of valid groups sorted in four-column output. The standard output of nawk is piped into the standard input of sort. Then the standard output of the sort command is read by the standard input of pr. The pr command does not format columns on BSD. Sorry!

List

A list is one or more pipelines separated by ;, &, && or ||. A list may end with a semicolon (;) or an ampersand (&). The pipe symbol (|) has precedence over these four operators. The ; and & have equal precedence and higher precedence than the && and || operators. The && and || operators have equal precedence. The general format of a list command follows

     pipeline ; pipeline

     pipeline & pipeline

     pipeline || pipeline

     pipeline && pipeline

where pipeline is a combination of simple and keyword commands.

To simplify discussion on commands, the word command is used to describe all of the simple, keyword, pipelines, and lists discussed in this book.

COMMAND EXECUTION

When you press Return to send a command to UNIX, the shell parses the command and performs the required substitutions and expansions. The following list outlines the sequence of substitutions and expansions in the order in which they are performed.

*    Aliasing (Module 4)
*    Tilde Substitution (Module 14 or 71)
*    Command Substitution (Module 71)
*    Variable Substitution (Module 99)
*    Blank Interpretation (Module 71)
*    File Name Generation (Module 54)
*    Arithmetic Evaluation (Module 73)

Once the command has been expanded it is executed. If an internal or keyword command is used, the shell executes the command within the current environment (no subshell is spawned). User defined functions are included in the keyword commands. If I/O redirection is performed, a subshell is created and the commands are executed in the new environment.

If the command is an external type, then a process is always created and the shell attempts to execute the command via the exec system call. Internal and external commands are described in a following section of this module.

Searching for Commands

The shell interprets the first parameter on the command line ($0) and, based on the following criteria, locates the program to be executed.

If the command contains a / then the shell searches only the directory specified. The directories in the PATH variable are not searched for the command. If the command is not in the directory, the shell returns a "not found" message. For example,

     cj> /usr/bin/lx
     ksh:/usr/bin/lx: not found
     cj> ../bin/lx
     ksh:../bin/lx: not found

If you do not specify a directory path in the command name (no / is used), the shell searches each directory listed in the PATH variable for the command. If the command is located in one of the directories and is executable, then it is executed. If it is not found in one of the directories, the shell notifies you as follows:

     cj> lx
     ksh: lx: not found

Tracked Aliases

By using alias or set you can have the shell remember where a command is for later use. If a command has been tracked, the next time you attempt to execute the command the shell will not have to search the PATH directories for the command. This reduces overhead and response time in command searching.

INTERNAL AND EXTERNAL COMMANDS

There are two basic types of commands: the external and internal commands.

External Commands

An external command is a computer program. It is a compiled binary file. The shell searches the directories in the PATH variable to find external commands, or the full path is specified on the command line. Once the command has been located, the shell must spawn a child process for the program. Then the shell attempts to execute the program by making an exec system call. If successful, the program is loaded into memory and the UNIX operating system executes the program as a new process.

Internal Commands

An internal command is built-in to the shell code. It is a function or subroutine compiled as part of the shell program (ksh, sh, csh). The shell does NOT have to search the PATH variable for an internal command. No new process is spawned to execute the command. Thus no disk reading or program loading is required. The result is much faster execution.

So why not put all commands in the shell? There are multiple arguments concerning this idea. The conventional operating system concept actually leans more towards command processors and standard commands all being a part of the operating system. But UNIX tends to be more modular oriented. The modularity provides for more flexibility and better management. The more commands that are placed in the ksh program, the larger and less responsive it becomes. Thus the argument is fast response up to a break point versus management of programs.


Previous Table of Contents Next