1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720 721 722 723 724 725 726 727 728 729 730 731 732 733 734 735 736 737 738 739 740 741 742 743 744 745 746 747 748 749 750 751 752 753 754 755 756 757 758 759 760 761 762 763 764 765 766 767 768 769 770 771 772 773 774 775 776 777 778 779 780 781 782 783 784 785 786 787 788 789 790 791 792 793 794 795 796 797 798 799 800 801 802 803 804 805 806 807 808 809 810 811 812 813 814 815 816 817 818 819 820 821 822 823 824 825 826 827 828 829 830 831 832 833 834 835 836 837 838 839 840 841 842 843 844 845 846 847 848 849 850 851 852 853 854 855 856 857 858 859 860 861 862
|
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<HTML
><HEAD
><TITLE
>Process Substitution</TITLE
><META
NAME="GENERATOR"
CONTENT="Modular DocBook HTML Stylesheet Version 1.76b+
"><LINK
REL="HOME"
TITLE="Advanced Bash-Scripting Guide"
HREF="index.html"><LINK
REL="UP"
TITLE="Advanced Topics"
HREF="part5.html"><LINK
REL="PREVIOUS"
TITLE="Restricted Shells"
HREF="restricted-sh.html"><LINK
REL="NEXT"
TITLE="Functions"
HREF="functions.html"><META
HTTP-EQUIV="Content-Style-Type"
CONTENT="text/css"><LINK
REL="stylesheet"
HREF="common/kde-common.css"
TYPE="text/css"><META
HTTP-EQUIV="Content-Type"
CONTENT="text/html; charset=iso-8859-1"><META
HTTP-EQUIV="Content-Language"
CONTENT="en"><LINK
REL="stylesheet"
HREF="common/kde-localised.css"
TYPE="text/css"
TITLE="KDE-English"><LINK
REL="stylesheet"
HREF="common/kde-default.css"
TYPE="text/css"
TITLE="KDE-Default"></HEAD
><BODY
CLASS="CHAPTER"
BGCOLOR="#FFFFFF"
TEXT="#000000"
LINK="#AA0000"
VLINK="#AA0055"
ALINK="#AA0000"
STYLE="font-family: sans-serif;"
><DIV
CLASS="NAVHEADER"
><TABLE
SUMMARY="Header navigation table"
WIDTH="100%"
BORDER="0"
CELLPADDING="0"
CELLSPACING="0"
><TR
><TH
COLSPAN="3"
ALIGN="center"
>Advanced Bash-Scripting Guide: An in-depth exploration of the art of shell scripting</TH
></TR
><TR
><TD
WIDTH="10%"
ALIGN="left"
VALIGN="bottom"
><A
HREF="restricted-sh.html"
ACCESSKEY="P"
>Prev</A
></TD
><TD
WIDTH="80%"
ALIGN="center"
VALIGN="bottom"
></TD
><TD
WIDTH="10%"
ALIGN="right"
VALIGN="bottom"
><A
HREF="functions.html"
ACCESSKEY="N"
>Next</A
></TD
></TR
></TABLE
><HR
ALIGN="LEFT"
WIDTH="100%"></DIV
><DIV
CLASS="CHAPTER"
><H1
><A
NAME="PROCESS-SUB"
></A
>Chapter 23. Process Substitution</H1
><P
><A
NAME="PROCESSSUBREF"
></A
><A
HREF="special-chars.html#PIPEREF"
>Piping</A
> the <TT
CLASS="FILENAME"
>stdout</TT
>
of a command into the <TT
CLASS="FILENAME"
>stdin</TT
> of another
is a powerful technique. But, what if you need to pipe the
<TT
CLASS="FILENAME"
>stdout</TT
> of <SPAN
CLASS="emphasis"
><I
CLASS="EMPHASIS"
>multiple</I
></SPAN
>
commands? This is where <TT
CLASS="REPLACEABLE"
><I
>process
substitution</I
></TT
> comes in.</P
><P
><I
CLASS="FIRSTTERM"
>Process substitution</I
> feeds the
output of a <A
HREF="special-chars.html#PROCESSREF"
>process</A
> (or
processes) into the <TT
CLASS="FILENAME"
>stdin</TT
> of another
process.</P
><DIV
CLASS="VARIABLELIST"
><P
><B
><A
NAME="COMMANDSPARENS1"
></A
>Template</B
></P
><DL
><DT
>Command list enclosed within parentheses</DT
><DD
><P
><B
CLASS="COMMAND"
>>(command_list)</B
></P
><P
><B
CLASS="COMMAND"
><(command_list)</B
></P
><P
>Process substitution uses
<TT
CLASS="FILENAME"
>/dev/fd/<n></TT
> files to send the
results of the process(es) within parentheses to another process.
<A
NAME="AEN18244"
HREF="#FTN.AEN18244"
>[1]</A
>
</P
><DIV
CLASS="CAUTION"
><TABLE
CLASS="CAUTION"
WIDTH="90%"
BORDER="0"
><TR
><TD
WIDTH="25"
ALIGN="CENTER"
VALIGN="TOP"
><IMG
SRC="common/caution.png"
HSPACE="5"
ALT="Caution"></TD
><TD
ALIGN="LEFT"
VALIGN="TOP"
><P
>There is <SPAN
CLASS="emphasis"
><I
CLASS="EMPHASIS"
>no</I
></SPAN
> space between the
the <SPAN
CLASS="QUOTE"
>"<"</SPAN
> or <SPAN
CLASS="QUOTE"
>">"</SPAN
> and the parentheses.
Space there would give an error message.</P
></TD
></TR
></TABLE
></DIV
></DD
></DL
></DIV
><P
> <TABLE
BORDER="0"
BGCOLOR="#E0E0E0"
WIDTH="100%"
><TR
><TD
><PRE
CLASS="SCREEN"
> <TT
CLASS="PROMPT"
>bash$ </TT
><TT
CLASS="USERINPUT"
><B
>echo >(true)</B
></TT
>
<TT
CLASS="COMPUTEROUTPUT"
>/dev/fd/63</TT
>
<TT
CLASS="PROMPT"
>bash$ </TT
><TT
CLASS="USERINPUT"
><B
>echo <(true)</B
></TT
>
<TT
CLASS="COMPUTEROUTPUT"
>/dev/fd/63</TT
>
<TT
CLASS="PROMPT"
>bash$ </TT
><TT
CLASS="USERINPUT"
><B
>echo >(true) <(true)</B
></TT
>
<TT
CLASS="COMPUTEROUTPUT"
>/dev/fd/63 /dev/fd/62</TT
>
<TT
CLASS="PROMPT"
>bash$ </TT
><TT
CLASS="USERINPUT"
><B
>wc <(cat /usr/share/dict/linux.words)</B
></TT
>
<TT
CLASS="COMPUTEROUTPUT"
> 483523 483523 4992010 /dev/fd/63</TT
>
<TT
CLASS="PROMPT"
>bash$ </TT
><TT
CLASS="USERINPUT"
><B
>grep script /usr/share/dict/linux.words | wc</B
></TT
>
<TT
CLASS="COMPUTEROUTPUT"
> 262 262 3601</TT
>
<TT
CLASS="PROMPT"
>bash$ </TT
><TT
CLASS="USERINPUT"
><B
>wc <(grep script /usr/share/dict/linux.words)</B
></TT
>
<TT
CLASS="COMPUTEROUTPUT"
> 262 262 3601 /dev/fd/63</TT
>
</PRE
></TD
></TR
></TABLE
>
</P
><DIV
CLASS="NOTE"
><TABLE
CLASS="NOTE"
WIDTH="100%"
BORDER="0"
><TR
><TD
WIDTH="25"
ALIGN="CENTER"
VALIGN="TOP"
><IMG
SRC="common/note.png"
HSPACE="5"
ALT="Note"></TD
><TD
ALIGN="LEFT"
VALIGN="TOP"
><P
> Bash creates a pipe with two <A
HREF="io-redirection.html#FDREF"
>file
descriptors</A
>, <TT
CLASS="FILENAME"
>--fIn</TT
> and
<TT
CLASS="FILENAME"
>fOut--</TT
>. The <TT
CLASS="FILENAME"
>stdin</TT
>
of <A
HREF="internal.html#TRUEREF"
>true</A
> connects
to <TT
CLASS="FILENAME"
>fOut</TT
> (dup2(fOut, 0)),
then Bash passes a <TT
CLASS="FILENAME"
>/dev/fd/fIn</TT
>
argument to <B
CLASS="COMMAND"
>echo</B
>. On systems lacking
<TT
CLASS="FILENAME"
>/dev/fd/<n></TT
> files, Bash may use
temporary files. (Thanks, S.C.)
</P
></TD
></TR
></TABLE
></DIV
><P
>Process substitution can compare the output of two
different commands, or even the output of different options
to the same command.</P
><TABLE
BORDER="0"
BGCOLOR="#E0E0E0"
WIDTH="100%"
><TR
><TD
><PRE
CLASS="SCREEN"
> <TT
CLASS="PROMPT"
>bash$ </TT
><TT
CLASS="USERINPUT"
><B
>comm <(ls -l) <(ls -al)</B
></TT
>
<TT
CLASS="COMPUTEROUTPUT"
>total 12
-rw-rw-r-- 1 bozo bozo 78 Mar 10 12:58 File0
-rw-rw-r-- 1 bozo bozo 42 Mar 10 12:58 File2
-rw-rw-r-- 1 bozo bozo 103 Mar 10 12:58 t2.sh
total 20
drwxrwxrwx 2 bozo bozo 4096 Mar 10 18:10 .
drwx------ 72 bozo bozo 4096 Mar 10 17:58 ..
-rw-rw-r-- 1 bozo bozo 78 Mar 10 12:58 File0
-rw-rw-r-- 1 bozo bozo 42 Mar 10 12:58 File2
-rw-rw-r-- 1 bozo bozo 103 Mar 10 12:58 t2.sh</TT
></PRE
></TD
></TR
></TABLE
><P
><A
NAME="PCC2DIR"
></A
></P
><P
> Process substitution can compare the contents
of two directories -- to see which filenames are in one,
but not the other.</P
><P
> <TABLE
BORDER="0"
BGCOLOR="#E0E0E0"
WIDTH="100%"
><TR
><TD
><PRE
CLASS="PROGRAMLISTING"
> 1 diff <(ls $first_directory) <(ls $second_directory)</PRE
></TD
></TR
></TABLE
>
</P
><P
>Some other usages and uses of process substitution:</P
><P
><A
NAME="PSFDSTDIN"
></A
></P
><P
><TABLE
BORDER="0"
BGCOLOR="#E0E0E0"
WIDTH="100%"
><TR
><TD
><PRE
CLASS="PROGRAMLISTING"
> 1 read -a list < <( od -Ad -w24 -t u2 /dev/urandom )
2 # Read a list of random numbers from /dev/urandom,
3 #+ process with "od"
4 #+ and feed into stdin of "read" . . .
5
6 # From "insertion-sort.bash" example script.
7 # Courtesy of JuanJo Ciarlante.</PRE
></TD
></TR
></TABLE
></P
><P
><A
NAME="NETCATEXAMPLE"
></A
></P
><P
><TABLE
BORDER="0"
BGCOLOR="#E0E0E0"
WIDTH="100%"
><TR
><TD
><PRE
CLASS="PROGRAMLISTING"
> 1 PORT=6881 # bittorrent
2
3 # Scan the port to make sure nothing nefarious is going on.
4 netcat -l $PORT | tee>(md5sum ->mydata-orig.md5) |
5 gzip | tee>(md5sum - | sed 's/-$/mydata.lz2/'>mydata-gz.md5)>mydata.gz
6
7 # Check the decompression:
8 gzip -d<mydata.gz | md5sum -c mydata-orig.md5)
9 # The MD5sum of the original checks stdin and detects compression issues.
10
11 # Bill Davidsen contributed this example
12 #+ (with light edits by the ABS Guide author).</PRE
></TD
></TR
></TABLE
></P
><P
><TABLE
BORDER="0"
BGCOLOR="#E0E0E0"
WIDTH="100%"
><TR
><TD
><PRE
CLASS="PROGRAMLISTING"
> 1 cat <(ls -l)
2 # Same as ls -l | cat
3
4 sort -k 9 <(ls -l /bin) <(ls -l /usr/bin) <(ls -l /usr/X11R6/bin)
5 # Lists all the files in the 3 main 'bin' directories, and sorts by filename.
6 # Note that three (count 'em) distinct commands are fed to 'sort'.
7
8
9 diff <(command1) <(command2) # Gives difference in command output.
10
11 tar cf >(bzip2 -c > file.tar.bz2) $directory_name
12 # Calls "tar cf /dev/fd/?? $directory_name", and "bzip2 -c > file.tar.bz2".
13 #
14 # Because of the /dev/fd/<n> system feature,
15 # the pipe between both commands does not need to be named.
16 #
17 # This can be emulated.
18 #
19 bzip2 -c < pipe > file.tar.bz2&
20 tar cf pipe $directory_name
21 rm pipe
22 # or
23 exec 3>&1
24 tar cf /dev/fd/4 $directory_name 4>&1 >&3 3>&- | bzip2 -c > file.tar.bz2 3>&-
25 exec 3>&-
26
27
28 # Thanks, Stphane Chazelas</PRE
></TD
></TR
></TABLE
></P
><P
><A
NAME="GOODREAD0"
></A
>Here is a method of circumventing the
problem of an <A
HREF="gotchas.html#BADREAD0"
><I
CLASS="FIRSTTERM"
>echo</I
>
piped to a <I
CLASS="FIRSTTERM"
>while-read loop</I
></A
> running
in a subshell.</P
><DIV
CLASS="EXAMPLE"
><HR><A
NAME="WRPS"
></A
><P
><B
>Example 23-1. Code block redirection without forking</B
></P
><TABLE
BORDER="0"
BGCOLOR="#E0E0E0"
WIDTH="100%"
><TR
><TD
><PRE
CLASS="PROGRAMLISTING"
> 1 #!/bin/bash
2 # wr-ps.bash: while-read loop with process substitution.
3
4 # This example contributed by Tomas Pospisek.
5 # (Heavily edited by the ABS Guide author.)
6
7 echo
8
9 echo "random input" | while read i
10 do
11 global=3D": Not available outside the loop."
12 # ... because it runs in a subshell.
13 done
14
15 echo "\$global (from outside the subprocess) = $global"
16 # $global (from outside the subprocess) =
17
18 echo; echo "--"; echo
19
20 while read i
21 do
22 echo $i
23 global=3D": Available outside the loop."
24 # ... because it does NOT run in a subshell.
25 done < <( echo "random input" )
26 # ^ ^
27
28 echo "\$global (using process substitution) = $global"
29 # Random input
30 # $global (using process substitution) = 3D: Available outside the loop.
31
32
33 echo; echo "##########"; echo
34
35
36
37 # And likewise . . .
38
39 declare -a inloop
40 index=0
41 cat $0 | while read line
42 do
43 inloop[$index]="$line"
44 ((index++))
45 # It runs in a subshell, so ...
46 done
47 echo "OUTPUT = "
48 echo ${inloop[*]} # ... nothing echoes.
49
50
51 echo; echo "--"; echo
52
53
54 declare -a outloop
55 index=0
56 while read line
57 do
58 outloop[$index]="$line"
59 ((index++))
60 # It does NOT run in a subshell, so ...
61 done < <( cat $0 )
62 echo "OUTPUT = "
63 echo ${outloop[*]} # ... the entire script echoes.
64
65 exit $?</PRE
></TD
></TR
></TABLE
><HR></DIV
><P
><A
NAME="PSUBPIPING"
></A
>This is a similar example.</P
><DIV
CLASS="EXAMPLE"
><HR><A
NAME="PSUBP"
></A
><P
><B
>Example 23-2. Redirecting the output of <I
CLASS="FIRSTTERM"
>process
substitution</I
> into a loop.</B
></P
><TABLE
BORDER="0"
BGCOLOR="#E0E0E0"
WIDTH="100%"
><TR
><TD
><PRE
CLASS="PROGRAMLISTING"
> 1 #!/bin/bash
2 # psub.bash
3
4 # As inspired by Diego Molina (thanks!).
5
6 declare -a array0
7 while read
8 do
9 array0[${#array0[@]}]="$REPLY"
10 done < <( sed -e 's/bash/CRASH-BANG!/' $0 | grep bin | awk '{print $1}' )
11 # Sets the default 'read' variable, $REPLY, by process substitution,
12 #+ then copies it into an array.
13
14 echo "${array0[@]}"
15
16 exit $?
17
18 # ====================================== #
19
20 bash psub.bash
21
22 #!/bin/CRASH-BANG! done #!/bin/CRASH-BANG!</PRE
></TD
></TR
></TABLE
><HR></DIV
><P
>A reader sent in the following interesting example of process
substitution.</P
><P
><TABLE
BORDER="0"
BGCOLOR="#E0E0E0"
WIDTH="100%"
><TR
><TD
><PRE
CLASS="PROGRAMLISTING"
> 1 # Script fragment taken from SuSE distribution:
2
3 # --------------------------------------------------------------#
4 while read des what mask iface; do
5 # Some commands ...
6 done < <(route -n)
7 # ^ ^ First < is redirection, second is process substitution.
8
9 # To test it, let's make it do something.
10 while read des what mask iface; do
11 echo $des $what $mask $iface
12 done < <(route -n)
13
14 # Output:
15 # Kernel IP routing table
16 # Destination Gateway Genmask Flags Metric Ref Use Iface
17 # 127.0.0.0 0.0.0.0 255.0.0.0 U 0 0 0 lo
18 # --------------------------------------------------------------#
19
20 # As Stphane Chazelas points out,
21 #+ an easier-to-understand equivalent is:
22 route -n |
23 while read des what mask iface; do # Variables set from output of pipe.
24 echo $des $what $mask $iface
25 done # This yields the same output as above.
26 # However, as Ulrich Gayer points out . . .
27 #+ this simplified equivalent uses a subshell for the while loop,
28 #+ and therefore the variables disappear when the pipe terminates.
29
30 # --------------------------------------------------------------#
31
32 # However, Filip Moritz comments that there is a subtle difference
33 #+ between the above two examples, as the following shows.
34
35 (
36 route -n | while read x; do ((y++)); done
37 echo $y # $y is still unset
38
39 while read x; do ((y++)); done < <(route -n)
40 echo $y # $y has the number of lines of output of route -n
41 )
42
43 More generally spoken
44 (
45 : | x=x
46 # seems to start a subshell like
47 : | ( x=x )
48 # while
49 x=x < <(:)
50 # does not
51 )
52
53 # This is useful, when parsing csv and the like.
54 # That is, in effect, what the original SuSE code fragment does.</PRE
></TD
></TR
></TABLE
></P
></DIV
><H3
CLASS="FOOTNOTES"
>Notes</H3
><TABLE
BORDER="0"
CLASS="FOOTNOTES"
WIDTH="100%"
><TR
><TD
ALIGN="LEFT"
VALIGN="TOP"
WIDTH="5%"
><A
NAME="FTN.AEN18244"
HREF="process-sub.html#AEN18244"
>[1]</A
></TD
><TD
ALIGN="LEFT"
VALIGN="TOP"
WIDTH="95%"
><P
>This has the same effect as a
<A
HREF="extmisc.html#NAMEDPIPEREF"
>named pipe</A
> (temp
file), and, in fact, named pipes were at one time used
in process substitution.</P
></TD
></TR
></TABLE
><DIV
CLASS="NAVFOOTER"
><HR
ALIGN="LEFT"
WIDTH="100%"><TABLE
SUMMARY="Footer navigation table"
WIDTH="100%"
BORDER="0"
CELLPADDING="0"
CELLSPACING="0"
><TR
><TD
WIDTH="33%"
ALIGN="left"
VALIGN="top"
><A
HREF="restricted-sh.html"
ACCESSKEY="P"
>Prev</A
></TD
><TD
WIDTH="34%"
ALIGN="center"
VALIGN="top"
><A
HREF="index.html"
ACCESSKEY="H"
>Home</A
></TD
><TD
WIDTH="33%"
ALIGN="right"
VALIGN="top"
><A
HREF="functions.html"
ACCESSKEY="N"
>Next</A
></TD
></TR
><TR
><TD
WIDTH="33%"
ALIGN="left"
VALIGN="top"
>Restricted Shells</TD
><TD
WIDTH="34%"
ALIGN="center"
VALIGN="top"
><A
HREF="part5.html"
ACCESSKEY="U"
>Up</A
></TD
><TD
WIDTH="33%"
ALIGN="right"
VALIGN="top"
>Functions</TD
></TR
></TABLE
></DIV
></BODY
></HTML
>
|