shell block redirect

Code block as a whole by a plurality of statements, for, while, until the cycle or if ... else, case ... in selecting structures, or by the {} of a command are called code blocks.

At the end of the block on the redirect command, redirection can be implemented for all the commands in the code block.

For Li

   

While loop continues to read the digital nums.txt, their sum is calculated.

  1. #!/bin/bash
  2. sum=0
  3. while read ndo
  4. ((I  + n ))
  5. DONE  <the nums .txt  # input redirection
  6. echo "sum=$sum"

Test.sh to save the code and run:

cat nums.txt

80 33 129 71 100 222 8

. ./test.sh

sum=643

Reading process while recording, and to redirect the output log.txt file:

  1. #!/bin/bash
  2. sum=0
  3. while read ndo
  4. ((I  + n ))
  5. echo "this number: $n"
  6. DONE  <the nums .txt  > log .txt  # simultaneously input and output redirection
  7. echo "sum=$sum"

. ./test.sh sum=643

cat log.txt

this number: 80

this number: 33

this number: 129

this number: 71

this number: 100

this number: 222

this number: 8

Of {}the code enclosed redirection.

  1. #!/bin/bash
  2. {
  3. echo "aa";
  4. echo "bb";
  5. echo "cc"
  6. > Log .txt  # output redirection
  7. {
  8. read name;
  9. read url;
  10. read age
  11. <Log .txt  # input redirection
  12. echo  "$ name has $ age years old, it is the URL $ url"

. ./Test.sh aa bb-year-old already, and it is the URL cc cat log.txt aa

bb

cc



Guess you like

Origin www.cnblogs.com/ting152/p/12554468.html