Previous Table of Contents Next


Module 71
ksh/csh/sh

DESCRIPTION

The ksh (pronounced "kay shell") program is a sophisticated command processor. It is also an interpretive programming language. It reads input from the terminal keyboard, interprets the command, and makes requests to the UNIX operating system to perform the requested action. As a programming language the shell lets you store shell commands in a file and execute the file as a program.

The shell used and discussed throughout Illustrated UNIX is the enhanced shell written by David Korn. Thus it is commonly called the "Korn shell." The Korn shell is usually stored on the system as ksh. For clarity the ksh notation is used.

The ksh was added in Release 4 of SV. It is not distributed with standard BSD, although many vendors offer it with their BSD type systems. If you do not have a copy of the Korn shell, you will not be able to use some of the commands discussed in this book. To obtain a copy of the Korn Shell you can contact the AT&T Toolchest Bulletin Board on (201) 974-8317 using a 300 or 1200 Baud modem.

The Korn shell is an enhanced version of the original Bourne shell. It adds such features as:

*  aliasing
*  functions (later versions of the sh support functions)
*  command re-entry of commands stored in history file
*  in-line editing of history file
*  job control (only on systems that support job control)
*  tilde substitution
*  enhanced variable substitution
*  arithmetic expression evaluation
*  automatic field padding
*  filename completion

C Shell
The C shell is also an enhanced version of the Bourne shell. It was originally written in the late 70s and early 80s. The emphasis was to provide a better interactive shell. The command language was designed to resemble the syntax of the C programming language. The C shell added such features as:
*  aliasing
*  functions
*  command re-entry of commands stored in history file
*  in-line editing of history file
*  job control (only on systems that support job control)
*  tilde substitution
*  enhanced variable substitution
*  arithmetic expression evaluation
*  automatic field padding
The C shell became a popular interactive shell, providing a superior interface alternative to the Bourne shell. It also provided the ground work for the Korn shell.

Discussing the shell is sometimes very confusing. In general UNIX terminology "shell program," "a shell," "shell procedure," "shell script," or "script" refer to a shell program. In this book the term "shell script" will be used to refer to a program written in the shell language.

Because the shell is a large and complex program it has been divided into subsections. Some of these subsections have been made into modules and are not included in this module. Most of the built-in commands have been placed in a module of their own for easy reference. Built-in commands are internal shell commands as compared to external UNIX commands that execute as a separate process. An internal command does not spawn a new process when it is executed.

As you read this module you may find it beneficial to read the module for a specific section, then return to this module to continue the learning sequence.

DEFINITIONS

A blank is a tab or space. A word is a contiguous set of characters, no blanks allowed. If a tab or space is quoted, then it is no longer a blank; it is a word. A name is a sequence of digits, letters, or underscores that begins with a letter or underscore. Refer to the Special (Meta)Characters section that follows, and to Module 23, the section "Shell Command Syntax."

COMMAND FORMAT

Following is the general format of the ksh command.

     ksh [ ±acefhikmnprstuvx ] [ ±o optargs ] [ args ]
     rksh [ ±acefhikmnprstuvx ] [ ±o optargs ] [ args ]

C Shell
csh [ -cefinstvVxX ] [ arg_list ]

Options

The following list describes the options and their arguments that may be used to control how ksh and rksh function.

-a Automatically export all variables as they are assigned values. Implies that an export Variable is performed after each variable is assigned a value.
-c string Execute string and exit to current shell. String is any command that the shell can execute. Normally, it is the name of a file containing shell scripts.
-e Exit the shell immediately if running a shell script and an error occurs. If a command terminates abnormally or returns a nonzero return code, then an error occured.
-f Filename generation is disabled. The shell no longer expands wildcard characters. Refer to Module 54.
-i Interactive shell. If the standard input and output is connected to a terminal, then the shell is interactive. In interactive mode the shell changes its signal handling. It ignores the TERMINATE and QUIT signals; therefore, a kill 0 or kill 2 does not affect the shell. It also traps and ignores INTERRUPT (kill 1) so you can interrupt the wait command.
-k Place all keyword arguments in the environment for a command. By default only the keyword arguments preceding the command are exported to the environment.
-m Background jobs run in a separate process group. Upon completion a line is displayed. The exit status of the job is reported in a completion message. This option is set automatically by interactive shells on systems supporting job control.
-n Read but do not execute commands. Useful to check for syntax errors.
-o optargs Allows you to set other and special options. Refer to the set command.
-p Disables processing of the $HOME/.profile file. The /etc/suid_profile file is used instead of the file defined in the ENV variable. If the real uid(gid) is not equal to the effective uid(gid) this option is automatically set. If you turn this option off, the effective uid and gid are set to the read uid and gid.
-r The shell runs as a restricted shell.
-s Commands are read from the standard input; if no arguments remain, then input is switched to the standard input. Standard output is written to the standard error (file descriptor 2).
-t Read and execute one command, then exit the shell.
-u Unset variables cause an error when substitution is performed. If a variable has not been assigned a value and it is referenced, then an error will occur.
-v Verbose. Echo (display) each shell input line as it is read.
-x Debug. Display command lines as they are executed; each command line is preceded by a +.

C Shell
-c Same as ksh
-e Same as ksh
-f Fast start. Forces the csh not to read the .cshrc file. If the shell is a login shell, .login is also ignored.
-i Same as ksh
-n Same as ksh
-s Same as ksh
-t Same as ksh
-v Same as ksh
-V Sets the verbose option before reading the .cshrc file.
-x Same as ksh
-X Sets the echo option before reading the .cshrc file.

The following examples help clarify some of these options.

     cj> ksh -x myscript
        # executes the shell script "myscript"
        # displays each line before executing it

     cj> START=10 ksh -k myscript INCREMENT=2
        # Assigns keyword parameters before the shell command and after it.
        # The variable "START" is set to 10 and "INCREMENT" is set to 2.

The -x can be used to debug shell scripts. The -k option allows you to place keyword assignments after the command has been entered. Without the -k you can only place keyword assignments before the command.


NOTE:  
The set command can be used to change or set options and arguments once a shell has begun execution. Set cannot change or set the -c, -i, -r, and -s options.



Arguments

The following list describes the arguments that may be passed to the ksh command.

args Valid arguments are programs or shell scripts that are executable and their arguments. These args are referred to as positional parameters (see Module 99, Variables/Parameters/Arrays). Parameters may be keyword parameters, constants, tilde substitution, command substitution, or filename generation output.

INVOCATION

If the shell is invoked as -ksh or -rksh, it assumes to be a login shell. A login shell reads commands from the /etc/profile and from either $HOME/.profile or .profile of the current directory if the files exist. If the environment variable ENV is set, then commands are read from the file defined by ENV if it exists. After the shell has finished its login or if it is invoked as a shell script, it reads commands based on the -c, -i, -r, and -s options. Refer to the Options section for further details.


C Shell
If a csh is a login shell, the commands in the $HOME/.cshrc file are read. The /etc/login file is processed next. The $HOME/.login file is then read. The $HOME/.cshrc file is read each time a csh is executed. It is the equivalent of the file defined in the ENV variable of the ksh.


Previous Table of Contents Next