Download The UNIX programming environment
Transcript
The UNIX programming environment
echo ${var?"No such variable"}
The first of these prints out the contents of `$var', if it is defined. If it is not defined the variable is
substituted for the string "No value set". The value of `var' is not changed by this operation. It is
only for convenience.
The second command has the same effect as the first, but here the value of `$var' is actually
changed to "Octopus" if `$var' is not set.
The third version is slightly peculiar. If `$var' is already set, its value will be forced to be "Forced
value", otherwise it is left undefined.
Finally the last instance issues an error message "No such variable" if `$var' is not defined.
Stdin, stdout and stderr
In the Bourne shell, the standard input/output files are referred to by numbers rather than by
names.
stdin
File number 0
stdout
File number 1
stderr
File number 2
The default routes for these files can be changed by redirection. The redirection commands are
more complicated than in C-shell, but they are also more flexible. Here is a comparison.
sh
command
command
command
command
command
errs
csh
> file
1> file
2> errs
1> file 2>&1
1> file 2> errs
command > file
command > file
(No analogue)
command >& file
(No analogue)
Description
Stdout
Stdout
Stderr
stdout
stdout
to file
to file
only to file errs
and stderr to file
to file, stderr to
Arithmetic in sh
Arithmetic is performed entirely `by proxy'. There are no internal arithmetic operators as in the Cshell. To evaluate an expression we call the `expr' command or the `bc' precision calculator.
Here are some examples of `expr'
a=`expr $a+1`
a=`expr 4 + 10 \* 5`
check = `expr $a \> $b`
# increment a
# 4+10*5
# true=1, false=0. True if $a > $b
file:///C|/Dokumente und Einstellungen/The UNIX programming environment.htm (63 von 198)20.11.2004 11:29:42