|
Co-processes - Part III - Syntax and Simple Example
|
Adding
the operator |& (pipe ampersand) after
a command or program will run it as a
co-process in the background. It may
be easier for you to remember this syntax
if you think about what these characters
represent individually. | creates a
pipe, and & starts jobs in the
background.
Using the -p option with the print and
read commands will write to and from a
co-process. This simple script named
to_upper will
be ran as a co-process from the command
prompt to illustrate the concept and
syntax:
|
#!/bin/ksh
typeset -u arg
while [ 1 ]
do
read arg
print $arg
done
|
As
you can see, this script is an infinite loop
that will convert the value read into the
variable arg to uppercase (see typeset
tip), and then print it to standard
output (the parent program when it's ran as
a co-process). First, the script is
started in the background as a co-process:
|
$ ./to_upper |&
[1] 30879
$ jobs
[1] + Running ./to_upper
$
|
The
next sequence of commands will write to and
read from the co-process, and then display
the returned value to stdout:
|
$ print -p hello
$ read -p line
$ echo $line
HELLO
$
|
The
string hello was passed to the co-process (to_upper)
with the print statement, and then read -p
was used to capture the returned value in
the variable line. Examing the value
of line shows that the co-process converted
"hello" to "HELLO" as
expected.
|
|
|
|
|
Learn
more...
If you are new to the UNIX or Linux
operating system and would like to learn
more, you may want to consider
registering for LiveFire Labs' UNIX
and Linux Operating System Fundamentals
online training course.
If you already have a solid grasp of the
fundamentals but would like to learn more
about the Korn shell and basic and
advanced shell scripting, taking our Korn
Shell Scripting course will be
beneficial to you.
Our
innovative hands-on training model allows
you to learn
UNIX by completing hands-on
exercises on real servers in our Internet
Lab.
More
Tips...
· Popular
UNIX Tips from the Past
|
|
|
|
 |
 |
|
Receive
the UNIX Tip, Trick, or Shell Script of the
Week by Email
|
|
|