Documente Academic
Documente Profesional
Documente Cultură
STEPHEN BOURNE
FEATURES OF BOURNE SHELL
Looping Constructs
1. Until
2. While
3. For
Conditional Constructs
1. If
2. Case
To allow shell scripts to be used as filters.
$ echo $PATH
/usr/local/bin:/usr/bin:/bin
$ VAR=value
$ echo $VAR
Value
$ sh
$ echo $PATH
/usr/local/bin:/usr/bin:/bin
$ echo $VAR
$
In the example above, the PATH variable (which is marked for export by default) gets copied into the
environment of the shell that is started within the shell. But the VAR variable is not marked for
export, so the environment of the second shell doesn't get a copy.
Rationalize and generalize string quoting mechanism.
Special Characters
Below are the characters that are special to the Bourne shell Quoting
these characters turns off their special meaning
#&*?[]()=|^;<>`$"'\
How Quoting Works
' xxx ‘ Disable all special characters in xxx .
32
Environment File
Although predefined environmental variables will
always be known to subshells, the shell must be
explicitly told which other variables, aliases, and
options should be communicated to subshells
One way to do this is to put them in an environment
file
Do this by placing
export ENV=~/environmentfile in your .profile and then
putting your data in environmentfile
33
Random Numbers
The Korn shell provides a random number generator
To initialize it, you type RANDOM=$$ (or a specific
number if you want a repeatable sequence)
Each time you echo the RANDOM variable, you will
get anew random number in the range 0 -32767
34
print
In other shells, you usually use awk when you want
formatted printing
ksh provides print. It doesn't allow as much control
over printing as awk but it does provide more control
than echo
print has a variety of formatting options and
parameters
35
print Formatting Options
\a - announce, ring the bell
\b - backspace
\c - print line without newline (rest of args ignored)
\f - formfeed
\n - newline
\r - carriage return
\t - tab
\v - vertical tab
\Ox - the 8 bit ASCII character with octal value x
\\ - backslash
36
whence
Tells you where a command resides
Two options, -p and -v, control the format of the output
whence alone prints the path to the command
whence vi
/bin/ucb/vi
-v also lists the type of command
whence -v vi
vi is a tracked alias for /usr/ucb/vi
-p does a path search even if the name is a reserved word or
alias
37
Built-In Arithmetic
ksh lets you do arithmetic using either the let
command or (( ))
(( )) good for True/False comparisons; $(( )) provides
value of computation
$ let a = 6+8
$ print $a
$ 14
Note that there are no spaces in the expression. If you
want to use spaces, you must single quote the expression
38
The (( )) is useful in as a conditional because it returns
a 1 or 0
if test (($min+$max>$top))
then
print "Ooh, a big one!"
else
print "Guess not"
fi
39
Aliases
ksh has aliases very similar to the C shell
It adds the concept of tracked aliases
These translate faster than normal aliases
When used, the shell searches the path the first time
and then remembers where the command was found
Future uses of the alias go directly to the command
You can also unalias in the Korn shell
40
Job Control
Korn shell supports job control almost the same as C shell
CTRL-Z places a job in the background, as does command
&
fg brings a job back to the foreground
Referring to background jobs is also similar
%n job n
?string job whose command contains string
%+ most recently invoked background job
%% same as %+
%- second-most recently invoked bg job
41
Additional Pattern Matching
Korn shell supports additional pattern matching
metacharacters
?(pattern[|pattern]…) matches zero or one occurrence
of any of the patterns
phre?([drs]|xx) would match phred, phrer, phres, and
phrexx.
*(pattern[|pattern]…) matches zero or more
occurrences of any pattern
phre*([0-9]) would match 'phre' or 'phre' followed by
any number of digits
42
+(pattern[|pattern]…) matches one or more
occurrences of any pattern
phre+([0-9]) would match 'phre' followed by one or
more digits, +(x) would match 'x', 'xx', 'xxx' …
@(pattern[|pattern]…) matches any occurrence of any
pattern
Most useful when used as a logical OR.
phre@(ddy|ds|ak) would match phreddy or phreds or
phreak. @(x|y|z) matches 'x' or 'y' or 'z'
43
Typeset
In the Korn shell, you can assign types to variables
typeset -i count
makes count an integer, if you tried to assign a string to
it, the shell would give you an error message
You can typeset with the following attributes
-u make uppercase (forces string to uppercase)
-l make lowercase
-in require integers and store internally as integer;
n specifies the base
-r make variable readonly
-xexport the variable
44
-Ln left justify to remove leading spaces; fill with
spaces or truncate on right to n bytes
-Rn right justify to remove trailing spaces; fill with
spaces or truncate on left to n bytes
-Zn same as -R except pad with leading 0's instead
of spaces
typeset +o is used to turn the option off
45
Functions
The Korn shell's function feature is an expanded version of
that found in the Bourne shell
A function is similar to a script except it is kept in the
shell's memory. Because it is kept in memory, it runs faster
than loading a script
Functions allow you to create your own groups of
commands, which can make shell programming much
easier since they can be used analogously to subroutines
Functions are not inherited by subshells unless you define
them in the environment file with the typeset command:
typeset -fx functname
46
Syntax
function functname{
shell command
shell command
…
}
47
There are two important differences between
functions and scripts
A functions does not run as a separate process
It shares variables with the process that invoked it
The present working directory is that of the caller
If the function changes it, it will also be changed for the caller
If a function has the same name as a script or executable
program, the function takes precedence
The return statement returns the exit status of the last
command executed
Functions can be recursive but you need to be careful
48
The Korn shell's major other features include:
Integrated programming features : the functionality of
several external UNIX commands,
including test , expr , getopt , and echo , has been integrated
into the shell itself, enabling common programming tasks to
be done more cleanly and without creating extra processes.
Debugging primitives that make it possible to write tools
that help programmers debug their shell code.
Regular expressions , well known to users of UNIX
utilities like grep and awk , have been added to the standard
set of filename wildcards and to the shell variable facility.
Advanced I/O features , including the ability to do two-
way communication with concurrent processes
( coroutines ).
Increased speed of shell code execution.
Security features that help protect against "Trojan horses"
and other types of break-in schemes.
In spite of its increased size, ksh provides better
performance. You can write programs to run faster with
ksh than with either the Bourne shell or the C shell,
sometimes an order of magnitude faster.
ksh is compatible with the Bourne shell. Virtually all
programs written for the Bourne shell run with ksh.
TCSH
a Unix shell based on and compatible with the C
shell (csh).
It is essentially the C shell with programmable command
line completion, command-line editing, and a few other
features.