Operating System - HP-UX
1826401 Members
4276 Online
109692 Solutions
New Discussion

Re: How to use these syntax?

 
SOLVED
Go to solution
tikual
Advisor

How to use these syntax?

I have read a book of Korn Shell and got that it provides a coprocess function and the syntax is like as this |&

It shows an example like this(not remember clearly):
[program name] $1 $2 2>/dev/null 2>/dev/null |&
(Pls. focus on the last three syntaxs)

Two questions:
Why it uses double "2>/dev/null" ?
How to use "|&" ?

I really not understand the meaning from the book. Can anyone tell me how to use it or give me a link? It is desirable if you can give me an example.

Thank you very much.
4 REPLIES 4
Mark Grant
Honored Contributor

Re: How to use these syntax?

When you start a co-process in a shell you do it will something like

units |&

This will start the units program as a co-process. All this means is that your script and, in this case, units run at the same time but the exciting thing is that a two way pipe is set up between your script and the command (units in this case). Doesn't sound too exciting yet but all this means you can write to units using "print" and read from it using "read -p". So your script can go "print inches\ncentimetres" and then a read -p will give you the result of converting inches to centimeters courtesy of the units application. A. Clay Stephenson introduced me to the concept of using "bc" as a co-process to do complex math within a script. Something I had never even begun to consider!

I have never seen the double "2>/dev/null" but I imagine that it re-directs the standard error of both processes to /dev/null.
Never preceed any demonstration with anything more predictive than "watch this"
tikual
Advisor

Re: How to use these syntax?

After following your example, I can't get any result from "read -p". It froze the terminal.
Actually I am not fully understanding the meaning. Do you know any materials related to this issue from the web?

Thanks!
Mark Grant
Honored Contributor
Solution

Re: How to use these syntax?

Here is a small example for you.

#!/bin/sh

bc |&

print -p "10+10"
read -p answer

echo $answer
Never preceed any demonstration with anything more predictive than "watch this"
tikual
Advisor

Re: How to use these syntax?

I got the result this time. More clearly after watching your example.

Thanks again!