1753510 Members
5563 Online
108795 Solutions
New Discussion юеВ

Re: |& - Pipe

 
SOLVED
Go to solution
Sundar_7
Honored Contributor

|& - Pipe

I was wondering if anyone could help me understand this |& symbol.

this is from the man page of sh-posix

|& Causes asynchronous execution of the preceding command or
pipeline with a two-way pipe established to the parent
shell. The standard input and output of the spawned command
can be written to and read from by the parent shell using
the -p option of the special commands read and print.

man page doesnt make much sense to me and /sbin/init.d/swconfig seem to use that

# grep "\|\&" /sbin/init.d/swconfig
ls |&
ls |&
cat /var/adm/sw/cleanupfile |&
#

I understand |& will execute the command1 in the background and write to a pipe that can be read using the -p option of read.

I was wondering if you guys are aware of any other use to this syntax or when it can be best used ?
Learn What to do ,How to do and more importantly When to do ?
5 REPLIES 5
Stuart Browne
Honored Contributor
Solution

Re: |& - Pipe

I can see this pipe being useful for challenge-response type programs where you want to script responses or sequences.

Whilst we do this for a number of bits and pieces using the perl 'open()' pipe, it could just as easily use this.

If you look into where those |& pipes are used in /sbin/init.d/swconfig, you'll see shortly after them what use they are put too:

# If exist, cleanup alternate cmds

if [[ -d /usr/lbin/sw/bin ]]
then
(
typeset first=1
cd /usr/lbin/sw/bin
ls |&
while read -p old_cmd
do
if [[ ${first} = 1 ]]
then
msg star "Removing files from '/usr/lbin/sw/bin'"
first=0
fi
rm -f ${old_cmd}
done
)
fi

It goes through a directory listing of the current directory, and removes them, bypassing the need for shell expansion.

The second entry isn't all that more exciting (identical infact), and the third use makes it just a little testing and stuff, before moving the '/var/adm/sw/cleanupfile' out of the way.

If you had a program of which did question/answer however, the |& starts to be of more use. You send it a question with 'print -p ', then it sends you an answer which you grab using 'read -p VAR'. You can then act on the answer, do actions, then send the next question etc. etc..

Or you could use the '>&p' and '<&p' to output/input from your own custom commands into the backgrounded pipe.
One long-haired git at your service...
Mark Grant
Honored Contributor

Re: |& - Pipe



Here's another example that is quite useful.

bc |&
print -p "10 + 10"
read -p answer
echo $answer
Never preceed any demonstration with anything more predictive than "watch this"
Laurent Menase
Honored Contributor

Re: |& - Pipe

It is a coprocess. You have the possibility to have more than one coprocess, by dupping the pipes to others fds:
example to use a socket in a script:
telnet anothersystem 7 |&

exec 3<&p 4>&p

cat <&3 &
echo hello >&4
exec 4>&-

Geoff Wild
Honored Contributor

Re: |& - Pipe

You can also use && in a condition statement like this from cron:

0 0 * * * [ -d /oracle/PROD/920_64 ] && /export/APPL/spaceman/cleanarch > /dev/null 2>&1

That checks to see if the directory /oracle/PROD/920_64 exists, and if yes, then executes the cleanarch script...

Rgds...Geoff
Proverbs 3:5,6 Trust in the Lord with all your heart and lean not on your own understanding; in all your ways acknowledge him, and he will make all your paths straight.
Sundar_7
Honored Contributor

Re: |& - Pipe

Thanks to all of you.

Sorry Geoff, I think you have misread the question. I was talking about "|&" and not about "&&".
Learn What to do ,How to do and more importantly When to do ?