Académique Documents
Professionnel Documents
Culture Documents
provided by:
Context-Switch Limited
Context-Switch Limited
Egham, Surrey, UK
http://www.context-switch.com
Korn Shell Syntax - A Reference Guide
The shell will substitute, on the command line, any file or directory name that matches the
pattern supplied on the command line.
variable_name=value
Syntax Meaning
* NOTE: When variables are assigned Left or Right-alignment formatting, using the typeset
command, the typeset display output characteristics will only be applied if the variable is
quoted within double-quotes (“) on the shell command line. (See Variable Substitution)
Korn Shell Syntax - A Reference Guide
Syntax Meaning
Variable Substitution:
The Korn shell has a number of metacharacters and pattern-structures which can be used to
substitute the contents of variables on a command line. These are described below.
The dollar character ($) must precede the variable name to force the shell to perform a
substitution.
Syntax Meaning
If the literal contents of the variable are to be substituted, the variables substitution controls
must be enclosed in double-quotes ("). Also, variables that have been left- or right-aligned,
using the typeset command, must be enclosed within double-quotes for their formatting to
be applicable at the time of substitution.
For example, if the literal contents of variable, myvar, consist of the following two lines of
text:
This is line 1
This is line 2
The output of quoted and unquoted variable substitions would be as shown below:
$ echo $myvar
This is line 1 This is line 2
$ echo "$myvar"
This is line 1
This is line 2
A number of shell-maintained variables are available within the Korn shell. These variables are
regularly updated by the shell, as comman lines are executed, directories are changed and such
like. These variables include:
Syntax Meaning
It is common practice to use the $$ (current PID value) as an extension to named files. For
example:
Command Substitution:
In the Korn shell, it is possible to substitute the output of a command within a command line,
in a similar manner to using variable or filename substitution.
The Korn shell supports the older, Bourne, shell form of syntax but also has its own specific
form of syntax for command substitution.
Bourne shell syntax for command substitution uses the command quotes (back quotes):
$ command `command_to_substitute`
The command_to_substitute would be run first and its output would be substituted onto the
command line, to be used by command. For example:
$ echo `date`
would echo the current date and time details (which were output by the date command).
The Korn shell alternative to the command quotes is: $(...) where ... represents the command
being run. An example is shown below.
$ command $(command_to_substitute)
The two commands, listed below, are identical in terms of their interpretation by the Korn shell:
$ echo `date`
$ echo $(date)
Variables can also be assigned the output of a command. An example is provided below:
$ var=$(date)
In the example above, the variable var will contain the output of the date command. Although
the output of the date command consists of many words, it is not required to quote to
command substitution text.
Korn Shell Syntax - A Reference Guide
Redirection - Summary:
The three data-flow channels which exist, as standard, in all shells are called standard input,
standard output and standard error output. These are more commonly know as stdin, stdout and
stderr, respectively.
Each data-flow channel has a file descriptor number. These are 0, 1 and 2 for stdin, stdout and
stderr, respectively. The redirection control can be preceded by the file descriptor number to
define which data-flow channel is to be redirected.
By default, the shell attaches these data-flow channels to the current device file (associated with
your terminal). A redirection control, therefore, is merely instructing the shell to associate the
data-flow channel with an alternative filename. The rule, therefore, is always to follow a
redirection control with a file pathname.
The shell saves the output of command in file_name. It creates file_name if it does not exist.
Otherwise, it overwrites (replaces/clobbers) the current contents of the file.
This creates file_name if it does not exist. Otherwise, the output from command is appended to
the end of file_name.
Shell Pipelines
Data output can be transferred to become another command’s input. This is managed by the
shell using a facility know as a pipeline. To create a pipeline, each command is separated by the
pipe (vertical-bar) character.
Command lines that employ this facility are referred to as a pipeline commands (or pipes):
$ command1 | command2 | command3 | ... | commandn
Redirect stderr to the NULL device. This is common practice within shell scripts to dispense
with any error output which would, otherwise, be displayed on the screen.
$ command 2> /dev/null
To prevent the possible overwriting (clobbering) of an existing file's data, you can turn on the
Korn shell's noclobber attribute.
$ set -o noclobber
If noclobber is set, you can override it using the following syntax. The first example shows the
use of the override feature with stdout. The second example shows the use of the override
feature with stderr. The character which follows the > symbol is the pipe (|) character.
$ command >| /dev/null
$ command 2>| /dev/null
To turn off the noclobber attribute, you should give the command:
$ set +o noclobber
Each file descriptor may be associated with a file. Once that file is opened, all output which is
redirected using that file descriptor number will be appended to the associated file. All input,
using that file descriptor number, will be read in from the associated file.
To open a filename associated with a file descriptor, use one of the following commands:
exec fd_num< file_pathname #(open for input)
exec fd_num> file_pathname #(open for output)
exec fd_num< file_pathname #(open for input and output)
Where fd_num would be a number between 3 and 9 and file_pathname would be the
name of the file to be associated with that file-descriptor.
To close a filename associated with a file descriptor, use one of the following commands:
exec fd_num<&- #(closes input file)
exec fd_num>&- #(closes output file)
For example:
exec 3> store_file #(open file for output)
ls -l >&3 #(store output)
date >&3 #(store output)
exec 3>&- #(close file)
Korn Shell Syntax - A Reference Guide
Conditional Expressions:
A conditional expression would be used to test attributes of files and to compare strings. The
test command can be used to test the result of the conditional expression.
A conditional expression is used with the [[ compound command to test attributes of files and
to compare strings. Word splitting and filename generation are not performed on the words
between [[ and ]]. See "The Test Statement" for details of [[.
Each expression can be constructed from one or more of the following unary or binary
expressions:
Table 6: Conditional Expressions
Condition Description
1. Because the single [ and double [[ are alternatives to a command word, they must be followed by a
minimum of one whitspace character.
2. Similarly, the closing ] or ]] must be preceded by a minimum of one whitespace character.
Korn Shell Syntax - A Reference Guide
Condition Description
Compound Expressions
A compound expression can be constructed from these primitives by using any of the following,
listed in decreasing order of precedence.
Table 8:
Note that the characters, [ and [[ , represents to command word test and must be followed
by at least one white-space character. The closing square bracket(s) must be preceded by at least
one white-space character.
The AND and OR statements differ between the Bourne and the Korn shell syntax.
The -a and -o clauses can not be used inside the Korn shell-specific, [[ ... ]], test
statement.
Korn Shell Syntax - A Reference Guide
Condition Description
If numeric variables are used within the test condition, the variable name do not to be preceded
by a dollar ($) character. The following two test conditions would be treated as being identical:
(($var==3)) Test if content of variable, var, is equal to 3.
((var==3)) Test if content of variable, var, is equal to 3.
Korn Shell Syntax - A Reference Guide
The simplest form of locical branching is performed using the if conditional statement.
Note: The word, then, is a reserved word and must be treated as a command in its own right.
If it is to appear on the same command line as the if statement, it must be preceded by a semi-
colon to separate the two commands.
if condition_statement
then
commands to be executed
fi
is the same as
if condition_statement ; then
commands to be executed
fi
For readability, it is probably better to have the word, then, on a line of its own.
if statements can be nested, as shown below. Each if must be closed by the command word fi
(the word if reversed).
if condition_statement1
then
command line 1
command line 2
if condition_statement2
then
commands_for_nested_if
fi
command line 3
command line 4
fi
Korn Shell Syntax - A Reference Guide
if condition_statement
then
commands to be executed if condition is TRUE
else
commands to be executed if condition is FALSE
fi
if condition_statement1
then
commands to be executed if condition1 is TRUE
elif condition_statement2
then
commands to be executed if condition2 is TRUE
elif condition_statement3
then
commands to be executed if condition3 is TRUE
else
commands executed if preceding conditions are FALSE
fi
NOTE: Each elif is followed by the separated command word, then. The entire statement is
closed down by just one occurrence of the word fi.
Any of the conditional statements, described on the preceding pages could be used where the
words, condition_statement, appear in the examples shown above.
Korn Shell Syntax - A Reference Guide
case value in
pattern1) commands to execute
when value matches
pattern1
;;
pattern2) commands to execute
when value matches
pattern2
;;
...
patternn) commands to execute
when value matches
patternn ;;
esac
Once value matches one of the patterns, no further attempt will be made to match any
remaining patterns within the case statement except when special syntax is used to force (not
covered within these notes).
The pattern can be any shell pattern matching structure. Multiple, alternative patterns can be
used when separated by the pipe (|) character.
Korn Shell Syntax - A Reference Guide
Pattern Description
The last pattern, *, would normally be used as the last, catch-all, pattern match.
For example:
case "$var" in
a|b) echo "contents of var contains the letter a or b"
;;
[AB]) echo "contents of var contains the letter A or B"
;;
*) echo "contents of var do not match any pattern above"
;;
esac
Korn Shell Syntax - A Reference Guide
while condition
do
command1
...
commandn
done
Where condition can be any valid test condition. The loop will continue to be executed
while the condition passes as TRUE.
until condition
do
command1
...
commandn
done
Where condition can be any valid test condition. The loop will continue to be executed until
condition is TRUE.
Two, special, conditional statements are available with while and until loops. These
conditional statements are the words: true and false.
In effect, they are commands that return either a zero (true) or non-zero (false) value,
respectively. So long as the state is either true or false, the loop will continue to be executed.
For example, in a while true loop, the commands, within the loop, will be executed until a false
state is encountered by the shell. This, false, state could be an interrupt (Ctrl-C) or a command's
return status which shows the command failed.
Korn Shell Syntax - A Reference Guide
Loops (for):
A for loop will continue to execute for as many times as there are arguments in the argument
list. As mentioned on the previous page, loops can be nested.
For example, the following loop will execute 5 times because there are 5 arguments in the
argument_list.
If loops have been nested, it is possible to break out of the current and any previous, nested,
loops by following the word break with the number of loops to break out of.
while true
do
command1
for variable in one two three four five
do
echo "$variable"
if [[ "$variable" = "four" ]]
then
break
fi
done
commandn # <<<<<break to this line <<<<
done
another_command_line
while true
do
command1
for variable in one two three four five
do
echo "$variable"
if [[ "$variable" = "four" ]]
then
break 2
fi
done
commandn
done
another_command_line # <<<<break to this line <<<<
Korn Shell Syntax - A Reference Guide
As with the break statement, it is possible to continue at the start of a previous level of nested
loop.
When the continue is executed, no remaining commands within the current loop, with the
current argument, will be executed.
Korn Shell Syntax - A Reference Guide
As with break and continue, you can follow the word exit with a number. However, the
number is used as the return status value of the shell script.
For example:
$ script_name
...
... script runs and contains the following line
exit 0
...
...
$ echo $?
0
The value, zero (0), is passed back to the parent process (the shell from which the script was
invoked) and stored in the shell's Return Status variable ($?)
The value passed back to the parent process by the exit statement can, therefore, be used as
an error-reporting value.
The default value of the return status will be the return status value of the last command line
executed by the script unless an exit statement is supplied with a given value.
The script, shown below, provides an interactive menu from which a user can select an option.
Because the menu script uses loops, the user can make as many selections from the menu
display as they wish, up to the point where they select the Quit option.
#! /bin/ksh
# Korn shell script
# Script author: Context-Switch Limited, Egham, Surrey, UK
# A menu program
##############################
## DISCLAIMER DISCLAIMER ##
##############################
# This script is provided for educational purposed only.
# Context-Switch does not guarantee that this script will work
# It is given on the understanding that the author will not be held
# responsible for any inaccuracies, disruption or loss of data
# incurred through use of this script.
# Use of this script implies acceptance of the above disclaimer.
#
##############################
## DISCLAIMER DISCLAIMER ##
##############################
# Use the trap facility to prevent the use of HUP, QUIT or INT
trap "echo 'Control-C cannot be used' ; sleep 1 ; clear ; continue" 1 2 3
# clear the screen ready for either a command action or a message display
clear
Korn Shell Syntax - A Reference Guide
esac
echo ""
echo "PRESS RETURN FOR THE MENU"
read waitvar
done