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
|
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<HTML
><HEAD
><TITLE
>Redirecting Code Blocks</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="I/O Redirection"
HREF="io-redirection.html"><LINK
REL="PREVIOUS"
TITLE="I/O Redirection"
HREF="io-redirection.html"><LINK
REL="NEXT"
TITLE="Applications"
HREF="redirapps.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="SECT1"
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="io-redirection.html"
ACCESSKEY="P"
>Prev</A
></TD
><TD
WIDTH="80%"
ALIGN="center"
VALIGN="bottom"
>Chapter 20. I/O Redirection</TD
><TD
WIDTH="10%"
ALIGN="right"
VALIGN="bottom"
><A
HREF="redirapps.html"
ACCESSKEY="N"
>Next</A
></TD
></TR
></TABLE
><HR
ALIGN="LEFT"
WIDTH="100%"></DIV
><DIV
CLASS="SECT1"
><H1
CLASS="SECT1"
><A
NAME="REDIRCB"
></A
>20.2. Redirecting Code Blocks</H1
><P
><A
NAME="REDIRREF"
></A
>Blocks of code, such as <A
HREF="loops.html#WHILELOOPREF"
>while</A
>, <A
HREF="loops.html#UNTILLOOPREF"
>until</A
>, and <A
HREF="loops.html#FORLOOPREF1"
>for</A
> loops, even <A
HREF="tests.html#IFTHEN"
>if/then</A
> test blocks can also incorporate
redirection of <TT
CLASS="FILENAME"
>stdin</TT
>. Even a function may
use this form of redirection (see <A
HREF="functions.html#REALNAME"
>Example 24-11</A
>).
The <SPAN
CLASS="TOKEN"
><</SPAN
> operator at the end of the code block
accomplishes this.</P
><DIV
CLASS="EXAMPLE"
><HR><A
NAME="REDIR2"
></A
><P
><B
>Example 20-5. Redirected <I
CLASS="FIRSTTERM"
>while</I
> loop</B
></P
><TABLE
BORDER="0"
BGCOLOR="#E0E0E0"
WIDTH="100%"
><TR
><TD
><PRE
CLASS="PROGRAMLISTING"
> 1 #!/bin/bash
2 # redir2.sh
3
4 if [ -z "$1" ]
5 then
6 Filename=names.data # Default, if no filename specified.
7 else
8 Filename=$1
9 fi
10 #+ Filename=${1:-names.data}
11 # can replace the above test (parameter substitution).
12
13 count=0
14
15 echo
16
17 while [ "$name" != Smith ] # Why is variable $name in quotes?
18 do
19 read name # Reads from $Filename, rather than stdin.
20 echo $name
21 let "count += 1"
22 done <"$Filename" # Redirects stdin to file $Filename.
23 # ^^^^^^^^^^^^
24
25 echo; echo "$count names read"; echo
26
27 exit 0
28
29 # Note that in some older shell scripting languages,
30 #+ the redirected loop would run as a subshell.
31 # Therefore, $count would return 0, the initialized value outside the loop.
32 # Bash and ksh avoid starting a subshell *whenever possible*,
33 #+ so that this script, for example, runs correctly.
34 # (Thanks to Heiner Steven for pointing this out.)
35
36 # However . . .
37 # Bash *can* sometimes start a subshell in a PIPED "while-read" loop,
38 #+ as distinct from a REDIRECTED "while" loop.
39
40 abc=hi
41 echo -e "1\n2\n3" | while read l
42 do abc="$l"
43 echo $abc
44 done
45 echo $abc
46
47 # Thanks, Bruno de Oliveira Schneider, for demonstrating this
48 #+ with the above snippet of code.
49 # And, thanks, Brian Onn, for correcting an annotation error.</PRE
></TD
></TR
></TABLE
><HR></DIV
><DIV
CLASS="EXAMPLE"
><HR><A
NAME="REDIR2A"
></A
><P
><B
>Example 20-6. Alternate form of redirected <I
CLASS="FIRSTTERM"
>while</I
> loop</B
></P
><TABLE
BORDER="0"
BGCOLOR="#E0E0E0"
WIDTH="100%"
><TR
><TD
><PRE
CLASS="PROGRAMLISTING"
> 1 #!/bin/bash
2
3 # This is an alternate form of the preceding script.
4
5 # Suggested by Heiner Steven
6 #+ as a workaround in those situations when a redirect loop
7 #+ runs as a subshell, and therefore variables inside the loop
8 # +do not keep their values upon loop termination.
9
10
11 if [ -z "$1" ]
12 then
13 Filename=names.data # Default, if no filename specified.
14 else
15 Filename=$1
16 fi
17
18
19 exec 3<&0 # Save stdin to file descriptor 3.
20 exec 0<"$Filename" # Redirect standard input.
21
22 count=0
23 echo
24
25
26 while [ "$name" != Smith ]
27 do
28 read name # Reads from redirected stdin ($Filename).
29 echo $name
30 let "count += 1"
31 done # Loop reads from file $Filename
32 #+ because of line 20.
33
34 # The original version of this script terminated the "while" loop with
35 #+ done <"$Filename"
36 # Exercise:
37 # Why is this unnecessary?
38
39
40 exec 0<&3 # Restore old stdin.
41 exec 3<&- # Close temporary fd 3.
42
43 echo; echo "$count names read"; echo
44
45 exit 0</PRE
></TD
></TR
></TABLE
><HR></DIV
><DIV
CLASS="EXAMPLE"
><HR><A
NAME="REDIR3"
></A
><P
><B
>Example 20-7. Redirected <I
CLASS="FIRSTTERM"
>until</I
> loop</B
></P
><TABLE
BORDER="0"
BGCOLOR="#E0E0E0"
WIDTH="100%"
><TR
><TD
><PRE
CLASS="PROGRAMLISTING"
> 1 #!/bin/bash
2 # Same as previous example, but with "until" loop.
3
4 if [ -z "$1" ]
5 then
6 Filename=names.data # Default, if no filename specified.
7 else
8 Filename=$1
9 fi
10
11 # while [ "$name" != Smith ]
12 until [ "$name" = Smith ] # Change != to =.
13 do
14 read name # Reads from $Filename, rather than stdin.
15 echo $name
16 done <"$Filename" # Redirects stdin to file $Filename.
17 # ^^^^^^^^^^^^
18
19 # Same results as with "while" loop in previous example.
20
21 exit 0</PRE
></TD
></TR
></TABLE
><HR></DIV
><DIV
CLASS="EXAMPLE"
><HR><A
NAME="REDIR4"
></A
><P
><B
>Example 20-8. Redirected <I
CLASS="FIRSTTERM"
>for</I
> loop</B
></P
><TABLE
BORDER="0"
BGCOLOR="#E0E0E0"
WIDTH="100%"
><TR
><TD
><PRE
CLASS="PROGRAMLISTING"
> 1 #!/bin/bash
2
3 if [ -z "$1" ]
4 then
5 Filename=names.data # Default, if no filename specified.
6 else
7 Filename=$1
8 fi
9
10 line_count=`wc $Filename | awk '{ print $1 }'`
11 # Number of lines in target file.
12 #
13 # Very contrived and kludgy, nevertheless shows that
14 #+ it's possible to redirect stdin within a "for" loop...
15 #+ if you're clever enough.
16 #
17 # More concise is line_count=$(wc -l < "$Filename")
18
19
20 for name in `seq $line_count` # Recall that "seq" prints sequence of numbers.
21 # while [ "$name" != Smith ] -- more complicated than a "while" loop --
22 do
23 read name # Reads from $Filename, rather than stdin.
24 echo $name
25 if [ "$name" = Smith ] # Need all this extra baggage here.
26 then
27 break
28 fi
29 done <"$Filename" # Redirects stdin to file $Filename.
30 # ^^^^^^^^^^^^
31
32 exit 0</PRE
></TD
></TR
></TABLE
><HR></DIV
><P
>We can modify the previous example to also redirect the output of
the loop.</P
><DIV
CLASS="EXAMPLE"
><HR><A
NAME="REDIR4A"
></A
><P
><B
>Example 20-9. Redirected <I
CLASS="FIRSTTERM"
>for</I
> loop (both
<TT
CLASS="FILENAME"
>stdin</TT
> and <TT
CLASS="FILENAME"
>stdout</TT
>
redirected)</B
></P
><TABLE
BORDER="0"
BGCOLOR="#E0E0E0"
WIDTH="100%"
><TR
><TD
><PRE
CLASS="PROGRAMLISTING"
> 1 #!/bin/bash
2
3 if [ -z "$1" ]
4 then
5 Filename=names.data # Default, if no filename specified.
6 else
7 Filename=$1
8 fi
9
10 Savefile=$Filename.new # Filename to save results in.
11 FinalName=Jonah # Name to terminate "read" on.
12
13 line_count=`wc $Filename | awk '{ print $1 }'` # Number of lines in target file.
14
15
16 for name in `seq $line_count`
17 do
18 read name
19 echo "$name"
20 if [ "$name" = "$FinalName" ]
21 then
22 break
23 fi
24 done < "$Filename" > "$Savefile" # Redirects stdin to file $Filename,
25 # ^^^^^^^^^^^^^^^^^^^^^^^^^^^ and saves it to backup file.
26
27 exit 0</PRE
></TD
></TR
></TABLE
><HR></DIV
><DIV
CLASS="EXAMPLE"
><HR><A
NAME="REDIR5"
></A
><P
><B
>Example 20-10. Redirected <I
CLASS="FIRSTTERM"
>if/then</I
> test</B
></P
><TABLE
BORDER="0"
BGCOLOR="#E0E0E0"
WIDTH="100%"
><TR
><TD
><PRE
CLASS="PROGRAMLISTING"
> 1 #!/bin/bash
2
3 if [ -z "$1" ]
4 then
5 Filename=names.data # Default, if no filename specified.
6 else
7 Filename=$1
8 fi
9
10 TRUE=1
11
12 if [ "$TRUE" ] # if true and if : also work.
13 then
14 read name
15 echo $name
16 fi <"$Filename"
17 # ^^^^^^^^^^^^
18
19 # Reads only first line of file.
20 # An "if/then" test has no way of iterating unless embedded in a loop.
21
22 exit 0</PRE
></TD
></TR
></TABLE
><HR></DIV
><DIV
CLASS="EXAMPLE"
><HR><A
NAME="NAMESDATA"
></A
><P
><B
>Example 20-11. Data file <I
CLASS="FIRSTTERM"
>names.data</I
> for above
examples</B
></P
><TABLE
BORDER="0"
BGCOLOR="#E0E0E0"
WIDTH="100%"
><TR
><TD
><PRE
CLASS="PROGRAMLISTING"
> 1 Aristotle
2 Arrhenius
3 Belisarius
4 Capablanca
5 Dickens
6 Euler
7 Goethe
8 Hegel
9 Jonah
10 Laplace
11 Maroczy
12 Purcell
13 Schmidt
14 Schopenhauer
15 Semmelweiss
16 Smith
17 Steinmetz
18 Tukhashevsky
19 Turing
20 Venn
21 Warshawski
22 Znosko-Borowski
23
24 # This is a data file for
25 #+ "redir2.sh", "redir3.sh", "redir4.sh", "redir4a.sh", "redir5.sh".</PRE
></TD
></TR
></TABLE
><HR></DIV
><P
>Redirecting the <TT
CLASS="FILENAME"
>stdout</TT
> of a code
block has the effect of saving its output to a file. See <A
HREF="special-chars.html#RPMCHECK"
>Example 3-2</A
>.</P
><P
><A
HREF="here-docs.html#HEREDOCREF"
>Here documents</A
>
are a special case of redirected code blocks. That being the case,
it should be possible to feed the output of a <I
CLASS="FIRSTTERM"
>here
document</I
> into the <TT
CLASS="FILENAME"
>stdin</TT
> for a
<I
CLASS="FIRSTTERM"
>while loop</I
>.</P
><P
> <TABLE
BORDER="0"
BGCOLOR="#E0E0E0"
WIDTH="100%"
><TR
><TD
><PRE
CLASS="PROGRAMLISTING"
> 1 # This example by Albert Siersema
2 # Used with permission (thanks!).
3
4 function doesOutput()
5 # Could be an external command too, of course.
6 # Here we show you can use a function as well.
7 {
8 ls -al *.jpg | awk '{print $5,$9}'
9 }
10
11
12 nr=0 # We want the while loop to be able to manipulate these and
13 totalSize=0 #+ to be able to see the changes after the 'while' finished.
14
15 while read fileSize fileName ; do
16 echo "$fileName is $fileSize bytes"
17 let nr++
18 totalSize=$((totalSize+fileSize)) # Or: "let totalSize+=fileSize"
19 done<<EOF
20 $(doesOutput)
21 EOF
22
23 echo "$nr files totaling $totalSize bytes"</PRE
></TD
></TR
></TABLE
>
</P
></DIV
><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="io-redirection.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="redirapps.html"
ACCESSKEY="N"
>Next</A
></TD
></TR
><TR
><TD
WIDTH="33%"
ALIGN="left"
VALIGN="top"
>I/O Redirection</TD
><TD
WIDTH="34%"
ALIGN="center"
VALIGN="top"
><A
HREF="io-redirection.html"
ACCESSKEY="U"
>Up</A
></TD
><TD
WIDTH="33%"
ALIGN="right"
VALIGN="top"
>Applications</TD
></TR
></TABLE
></DIV
></BODY
></HTML
>
|