start page | rating of books | rating of authors | reviews | copyrights

UNIX Power Tools

UNIX Power ToolsSearch this book
Previous: 45.22 Handling Files Line-by-Line Chapter 45
Shell Programming for the Initiated
Next: 45.24 A Shell Can Read a Script from its Standard Input, But...
 

45.23 The Ins and Outs of Redirected I/O Loops

The Bourne shell usually runs a loop with redirected input or output ( 45.22 ) in a subshell ( 38.4 ) . For the formprog script in article 45.22 , this means, among other things, that:

Greg Ubben sent me two other ways that he prefers. The first one depends on having a read that accepts redirection on its command line, which most do these days. The second works when you can put the usage in the same scope (within the curly braces ( 13.8 ) ) as the redirection:

exec 3< file   { while read line <&3       while read line do       do      var=value            var=value done       done exec 3<&-       echo "var = $var" echo "var = $var"   } < file

Putting the loop inside a function and redirecting into the function also seems to avoid the subshell problem. But don't take my (our) word for it: test it on the shell you'll be using.

- JP


Previous: 45.22 Handling Files Line-by-Line UNIX Power Tools Next: 45.24 A Shell Can Read a Script from its Standard Input, But...
45.22 Handling Files Line-by-Line Book Index 45.24 A Shell Can Read a Script from its Standard Input, But...

The UNIX CD Bookshelf Navigation The UNIX CD BookshelfUNIX Power ToolsUNIX in a NutshellLearning the vi Editorsed & awkLearning the Korn ShellLearning the UNIX Operating System