Previous Table of Contents Next


FUNCTIONS

Functions are a form of commands like aliases, and scripts. They differ from Korn shell scripts, in that they do not have to read in from the disk each time they are referenced, so they execute faster. They also provide a way to organize scripts into routines, like in other high-level programming languages. Since functions can have local variables, recursion is possible. Functions are defined with the following format:

              function name { commands ; }

Local function variables are declared with the typeset command within the function.

Function Commands

return return from a function
return n return from a function; pass back return value of n
typeset –f display a list of functions and their definitions
typeset +f display a list of function names only
typeset –fu display a list of autoloading functions
typeset –fu name make function name autoloading
typeset –fx display a list of exported functions
typeset –fx name export function name
typeset –ft name display function commands and arguments as they are executed
unset –f name remove function name

THE PRINT COMMAND

print [options] arguments display arguments according to options

print Options

treat everything following as an argument, even if it begins with
–n do not add a ending newline to the output
–p redirect the given arguments to a co-process
–r ignore the \ escape conventions
–R ignore the \ escape conventions; do not interpret arguments as options (except –n)
–s redirect the given arguments to the history file
–un redirect arguments to file descriptor n. If the file descriptor is greater than 2, it must first be opened with the exec command. If n is not specified, the default file descriptor is 1 (standard output).

print Escape Characters

\a Bell character
\b Backspace
\c Line without ending newline
\f Formfeed
\n Newline
\r Return
\t Tab
\v Vertical tab
\\ Backslash
\0x 8-bit character whose ASCII code is the 1-, 2-, or 3-digit octal number x

THE READ COMMAND

read [options] variables read input into variables according to options
read name?prompt display prompt and read the response into name

read Options

–p read input line from a co-process
–r do not treat \ as the line continuation character
–s save a copy of input line in the command history file
–un read input line from file descriptor n. If the file descriptor is greater than 2, it must first be opened with the exec command. If n is not specified, the default file descriptor is 0.

MISC

# anything following a # to the end of the current line is treated as a comment and ignored
#!interpreter if the first line of a script starts with this, then the script is run by the specified interpreter
rsh running under the restricted shell is equivalent to ksh, except that the following is not allowed:
• changing directories
• setting the value of ENV, PATH, or SHELL variables
• specifying path or command names containing /
• redirecting output of a command with >, >|, <>, or >>

DEBUGGING KORN SHELL SCRIPTS

The Korn shell provides a number of options that are useful in debugging scripts: noexec (–n), verbose (–v), and xtrace (–x). The noexec (–n) option causes commands to be read without being executed and is used to check for syntax errors. The verbose (–v) option causes the input to displayed as it is read. The xtrace (–x) option causes commands in Korn shell scripts to be displayed as they are executed.

This is the most useful, general debugging option.

For example, tscript could be run in trace mode if invoked "ksh –x tscript".

FILES

$HOME/.profile contains local environment settings, such as the search path, execution options, local variables, aliases, and more. At login time, it is read in and executed after the /etc/profile file.
$HOME/.sh_history contains previously executed commands
$ENV contains the name of the file that has aliases, function, options, variables, and other environment settings that are to be available to subshells
/etc/profile contains the system-wide environment settings, such as a basic search path, a default TERM variable, the system umask value, and more (system dependent). If existent, it is read in and executed at login time before the $HOME/.profile file.
/etc/suid_profile contains local and system environment settings for privileged mode (system dependent)


Previous Table of Contents Next
1