Beruflich Dokumente
Kultur Dokumente
Unix Programming
Korn Shell
Contents
Contents .................................................................................................................................................. 1
1 General information........................................................................................................................ 2
2 Getting started ................................................................................................................................ 2
3 Using the korn shell ......................................................................................................................... 2
3.1 Executing Commands .............................................................................................................. 2
3.2 Shell Commands ...................................................................................................................... 2
3.3 Conditional Expressions .......................................................................................................... 4
3.4 Built-in Commands .................................................................................................................. 5
3.5 Job Control .............................................................................................................................. 5
3.6 Command Substitution............................................................................................................ 6
3.7 File Name Substitution ............................................................................................................ 6
3.8 Redirecting Input and Output ................................................................................................. 7
3.9 Aliases...................................................................................................................................... 7
3.10 Tilde (~) Substitution ............................................................................................................... 7
3.11 Environment Variables ............................................................................................................ 7
3.12 Emacs Editing Mode ................................................................................................................ 9
4 Helpful hints .................................................................................................................................... 9
5 References/manuals...................................................................................................................... 10
1
1 General information
The Korn shell is an interactive command interpreter and command programming language.
The shell carries out commands specified at the terminal or from a file. The Korn shell is
backwards compatible with the Bourne shell and contains all of its features. In addition, some
of the better features of the C shell are also included.
2 Getting started
The Korn shell is executed by the login program if it is your default shell, or by issuing the
ksh command. When executed, the shell runs commands found in the /etc/profile file and
then in your $HOME/.profile file. If you specify a file on the command line of the subshell
command, the shell will attempt to execute the file as if it were a script file.
By default the shell will search for external commands based on the value of the PATH
environment variable. If the command can not be found in any of the directories specified by
the path, it searches the current directory. If you specify a path with the command, the shell
will not search the path for the specified command. Instead, it only searches the path specified
on the command line.
The for command lets you execute a specified list of commands. The syntax for this
command is:
where identifier is a variable which is assigned the value(s) specified by word ... and
executes the commands specified by list. If the word option is not specified, the shell
executes the list of commands for each positional parameter that is set.
The select command will write the specified words to standard error. The syntax is:
The case command can be used to executes commands based on a particular setting of another
variable. The syntax for this command is:
where word is the variable to match with one of the specified patterns. When a matching
pattern is found, the commands specified by list are executed. The vertical bar is used to
denote an "or" operation.
You can also use an if construct to specify conditions in the script. The syntax for this
command is:
if List ;then list [elif List; then list] ... [;else list] fi
where the commands specified by list are executed if the last command executed by List has
a return value of zero. The elif construct represents an else if phrase and the corresponding list
commands are executed if the previous List returns a non zero value and the last command
executed by the List following the elif phrase returns a value of zero. The else clause is
executed only if all other conditions are return non-zero values.
The while clause can be used to execute a list of commands while a certain condition holds
true. The syntax for the while command is:
where the commands specified by list are executed after the last command specified by the
List variable returns a zero value. The shell will continue to execute the commands until the
last command specified in List returns a non-zero value.
The until command works like the while command except the return values are reversed. This
means the commands in list are executed as long as the last command specified by List
returns a non-zero value. This repeats until the last command in List returns a zero value.
Commands executed within parentheses are run within a subshell. To execute commands
within the current shell, enclose them in braces ('{' and '}').
Specifying an expression within a double set of brackets will cause the shell to evaluate the
expression. If the expression has a true value, it will have a return code of zero.
You can define a function for use with other shell commands. To do this, specify the name of
the function followed by a set of parenthesis. The commands that are associated with the
name should follow, and be enclosed within braces and separated by semi-colons. The syntax
for this is:
name () {list;}
where name is the name of the function and list is a set of commands that are executed when
the function is invoked.
3
The Korn shell will also let you measure the execution time of a command. To do this, specify
the command as the argument to the time command.
4
3.4 Built-in Commands
The following table shows a listing of the special commands to the Korn shell. Next to each
command will be a brief description of the command.
Command Description
---------------------------------------------------------------------------
----
: [argument] The null command returns a zero exit value
. file [argument...] Executes commands specified in file using the
search path and not starting a subshell
alias [name[=value]] Specify a nickname for the command, value
bg [job...] Place the specified job into the background
break [n] Exits the nth iteration from the for, while, or
until loop.
cd [directory] Changes to the specified directory, or to $HOME if
none specified
continue [n] Resumes the nth iteration of the for, while, or
until loop
echo [string] Writes string to standard output
eval [list] Executes the commands specified in list
exec [list] Executes the commands in list in place of the
current shell
exit [n] Causes the shell to exit with a return code of n
export [name] Marks the specified name for export to the
environment of subsequent commands.
fc [first [last]] Selects a range of commands from first to last from
the command history
fg [job...] Places the job in the foreground
jobs [job] Displays all the active jobs or the specified job
kill job Terminates the execution of the specified job
newgrp [group] Same as the /usr/bin/newgrp command
let expression Evaluates the specified expression
print [arguments] This is the shell output mechanism
pwd Displays the current working directory
read [name ...] Reads one line from standard input and assigns to
name
readonly [name...] Marks name to be read-only
return [n] Causes a function to return a value of n
set [flag [argument]] Sets the specified flags
shift [n] Shifts the command line arguments to the left n
places ($0 is never shifted)
times Displays the accumulated user and system times for
processes running from the shell
trap [command] [n] Runs command when the shell receives a signal n
typeset [name...] Sets attributes and values for shell parameters
ulimit [flag [var]] Sets or queries process size limits using a
specified flag
umask [nnn] Sets the access mode mask to be used with newly
created files.
unalias name Removes the alias for the nickname name
unset [name...] Removes the specified variable, name from the
environment
wait [n] Waits for the child process whose process number is
n or all if none specified
whence name For each name specified, indicate how it would be
used as a command
5
well as the background. The easiest of these functions is the [CTRL] [Z] key sequence. This
will stop the current process and return you to the OS prompt. Any job can be placed in the
background by issuing the bg command. The fg command should be issued when you wish to
place a job in the foreground. Both of these commands take a job number as an argument. The
job number is returned by the jobs command. The job number is also displayed in brackets
when you place it in the background.
A command can be told to execute in the background if an ampersand (& ) is placed at the
end of the command line. When the shell sees the ampersand, it will display the job number
for that process in square brackets followed by its process ID. If the background process tries
to read from the terminal, it will be stopped. These jobs will also stop when trying to write
output if the stty tostop command has been given to the shell.
When using any of the commands to control a job (bg, fg, and kill), an argument must be
passed specifying which job is to be affected. This argument represents a specific job and can
be specified by the job ID preceded by a percent sign. As an alternative, you can specify the
command string after the percent sign to specify a job. If you wish to specify a series of jobs
that match a specific string, precede the string with the %? characters. If no arguments are
given, the commands will assume you wish to choose the previously affected job.
Before logging out, you should know the statuses of any jobs which still exist. If you have
stopped jobs, the shell will not allow you to logout. Before you can logout, you must resume
or kill the job. If you have a job running in the background, you will be allowed to logout and
the process will continue running. This, however, is strongly discouraged because it ties up
valuable system resources.
There is an exception to the filename substitution rules. Any file that begins with a period will
not match a pattern. In other words, to see a list of all files that begin with a period, you would
have to use the pattern .* . If the pattern does not match any file names, the pattern is returned.
Infinite recursion can occur if files have names that include any of the special pattern
characters.
6
3.8 Redirecting Input and Output
Many AIX commands take input from stdin, send output to stdout, and send errors to stderr.
The Korn shell allows you to redirect standard input, standard output, or standard error to
another file or command. These devices are given file descriptors zero through two
respectively. File descriptors three through nine are free to use by your applications and can
also be redirected as needed. File name substitution or pattern matching does not take place
after a redirection symbol. Therefore, care should be taken to ensure files are not created
using any of the special pattern characters.
Redirection symbols should be used after the command and any of its arguments. The
redirection symbol should then be followed by the file or command for which the redirection
is occurring. The < symbol is used to redirect standard input from the specified file. The >
sign redirects standard output to the specified file. The > > symbol appends the output of the
command to file specified, creating it if it does not exist. You can redirect a specific file
descriptor by placing its number directly after the redirection symbol and an ampersand. For
example, cc prog1 > > & 2 error.file will redirect all errors produced by the c compiler to the
file error.file. You can redirect more than one file descriptor by placing each one with its
own redirection symbol.
3.9 Aliases
Aliases can be used to give a nickname to a command or to give a name to a command with a
specific set of options. You can alias a command by issuing an alias command in your .profile
script. The syntax for the alias command is
where nickname is the nickname to be used and command is the AIX command with the
specified options being aliased. When the shell finds a command being executed that matches
a nickname in the alias list, it will replace the nickname on the command line with
command options. This substitution is followed by a space character so the substituted string
does not combine with the remainder of the command line.
The Korn shell lets you create and assign values to system environment variables as well as
user defined variables. A variable name is a sequence of letters and numbers and can include
7
underscores. The only restriction is that it not begin with a number. A value can be assigned
to a variable by typing the variable name followed by an equals sign and then the value to be
assigned. Do not place any spaces around the equals sign, or they will be included as part of
the variable name or value. If you wish to use spaces in the value, place the entire value
within quotation marks (" "). If double quotation marks are used, the shell will still perform
variable name substitution. In other words, values are substituted for their variables. Once a
value has been assigned to a variable, it can be used by placing a $ before the variable name.
If the value is to be used within a string, you can include the variable name (but not the $)
within braces. The braces will separate the variable name from the rest of the string. For
example, to show the value of the environment variable HOME, use the command
echo $HOME
Environment variables can be exported to subsequent subshells by using the export command.
Below is a table showing the system environment variables and a brief description about them.
8
PATH The search path for commands separated by colons
PPID The process number of the parent of the shell
PS1 The string to be used as the primary system prompt
PS2 The value of the secondary prompt (when the shell finds a
new-line character)
PS3 The value of the selection prompt string used within a
select loop
PS4 This value precedes each line of an execution trace
PWD The present working directory
RANDOM Generates a random number between 0 and 32767
REPLY Set by the select and read special commands when no
arguments are given
SECONDS The number of seconds since the shell was invoked
SHELL The path name of the shell (should be exported by the
$HOME/.profile script)
TMEOUT The number of minutes the shell remains inactive before it
exits.
4 Helpful hints
You can determine the number of arguments passed to a shell by using the $# environment
variable.
The value of the last command to be executed will store its return code in the $? variable.
You can see the value of user variables by using the set command. The env command can be
used to display the values of the system variables.
9
5 References/manuals
InfoExplorer provides online help regarding the Korn shell
Many books exist in the library and your local bookstore that cover AIX (UNIX) and the
Korn shell. Search through these for one that explains the amount of information you need at
a level corresponding to your expertise with AIX.
10