Chinaunix首页 | 论坛 | 博客
  • 博客访问: 421518
  • 博文数量: 136
  • 博客积分: 5351
  • 博客等级: 少校
  • 技术积分: 1446
  • 用 户 组: 普通用户
  • 注册时间: 2011-04-29 15:46
文章存档

2013年(2)

2012年(18)

2011年(116)

分类: Python/Ruby

2011-09-28 10:35:24

0.回顾重定向   标准输入输出   报告错误  丢弃数据  创建日志文件  以上主要内容

1.文件描述符:每个进程可以拥有九个描述符0--9其中 0--STDIN--- 标准输入 1--STDOUT  2--STDERR

2.临时重定向,讲输出重定向到STDERR     重定向到某个文件描述符,在对应数字前面添加 &
   形式:echo  "this is a error text "  >&2  //   >&2   三个字符之间一定不要有空格
   另外此处没有体现出了 >  可以创造一个文件。此外>创建文件的时候>与文件名之间有无空格不影响

 Q:   无论是重定向到STDIN  STDOUT  STDERR的哪一个,数据到底去哪儿了?

 A:显示在屏幕上  并且实质原因是STDOUT  STDERR  ;echo 可以实现 把数据重定向到文件里面
       比如:   #echo  $name    >   a    你就可以在a文件里面,看到对应数据.

3. #vim   test0
        #!/bin/bash
          echo   "this  is  an  error message test "  >&2    //决定了这句话的去向,其本质.
          echo    "this  is  a  normal  test"

    # ./test0
 [root@acer today_bash]# ./test0
 this is  an  error message test
 this is  normal  output

    # ./test0   2  >  a   ;  cat   a   
   [root@acer today_bash]# ./test0   2  >   b  ;  cat b
   this is  an  error message test            //# ./test0   2  >   b     输出结果
   this is  normal  output                        //该句是  文件b 的内容

4.永久重定向 :为解决echo针对大量数据的不灵活性
    利用exec 命令  通知脚本执行期间重定向特定文件描述符。

   Q:exec  的作用范围  也就是说它结束重定向的标识 ???
   A:

   Q:永久重定向中永久的含义,以及数据被导向的输出规则???
   A:临时重定向中程序段里面没有涉及到生成文件,而是在命令行加入生成文件的操作;
      因此数据主要有两个去向:屏幕   文件
      就  exec 2>testerr     exec 1>testout   以及程序段里面的 >&2   2 1  >&2  影响生成文件
      而对于屏幕输出   则由  2 1  >&2   还有   exec 2>testerr     exec 1>testout  在程序段里面的位置决定
   Q:什么情况下同一语句既有屏幕输出又被写入生成文件???
   A:


程序段一:

#vim   test01

#!/bin/bash

exec 2>testerr
echo  "this  is  the commence of shell scrip "
echo  "the work is moving the output to another location"

exec 1>testout
echo  "this output is a test for  STDOUT"
echo  "but  this  output is a test for STDERR  ,locate in testout"  >&2

重点分析一下  脚本程序  test1  以及生成的文件 testout   testerr   的关系和内容

[root@acer today_bash]# ./test1
this  is  the commence of shell scrip
the work is moving the output to another location  
[root@acer today_bash]# cat   testerr
but  this  output is a test for STDERR  ,locate in testout
[root@acer today_bash]# cat  testout
this output is a test for  STDOUT


程序段二:

#vim  test2

#!/bin/bash
#to  understand  the  exec  command

exec   1>testout1
echo "this is an  output for STDOUT"
echo  "but  this is an  output for STDERR ,  locate  in testout1"  >&2

exec  2>testerr1
echo  "this is the end  of this shell script which I thought"
echo  "I wanto get  across the using of exec command"

[root@acer today_bash]# ./test2
but  this is an  output for STDERR ,  locate  in testout1
[root@acer today_bash]# cat   testerr1
[root@acer today_bash]# cat   testout1
this is an  output for STDOUT
this is the end  of this shell script which I thought
I wanto get  across the using of exec command     //没有定义>&2  则写入了  testout1


程序段三 :

#vim  test3

#!/bin/bash

echo   "at first I  can not understand why ./test1 output the content of testerr"

exec 2>testerr
echo  "this  is  the commence of shell scrip "
echo  "the work is moving the output to another location"  >&2

exec 1>testout
echo  "this output is a test for  STDOUT"
echo  "but  this  output is a test for STDERR  ,locate in testout"  >&2


[root@acer today_bash]# ./test15 
at first I  can not understand why ./test1 output the content of testerr
this  is  the commence of shell scrip
[root@acer today_bash]# cat   testerr
the work is moving the output to another location
but  this  output is a test for STDERR  ,locate in testout
[root@acer today_bash]# cat   testout
this output is a test for  STDOUT

程序段四:

#vim   test4

#!/bin/bash
#to  understand  the  exec  command

exec   1>testout1
echo "this is an  output for STDOUT"
echo  "but  this is an  output for STDERR ,  locate  in testout1"

exec  2>testerr1
echo  "this is the end  of this shell script which I thought"
echo  "I wanto get  across the using of exec command"
echo  "this  sentence  is added later to test for  error"   >&2

[root@acer today_bash]# ./test4
[root@acer today_bash]# cat  testerr1
this  sentence  is added later to test for  error
[root@acer today_bash]# cat   testout1
this is an  output for STDOUT
but  this is an  output for STDERR ,  locate  in testout1
this is the end  of this shell script which I thought      //说明数据导向时只要在exec作用域则与位置无关
I wanto get  across the using of exec command   //而取决于待导向的数据性质 error  还是 normal out


由上可见:
1.exec 在程序中已经重定向的error 不输出而导入文件
2.exec程序段的输出与第一段exec 涉及的程序有关    但不影响生成文件的数据及其性质
3.error生成文件及输出还与>&2  有关    
4.exec 1>testout    位于起始:
   a. exec 1>testout  定义的程序段里面没有   >&2   则执行脚本时  无任何屏幕输出语句
   b.程序段里面有 >&2   则输出  本段程序里面所有的>&2对应语句
5.exec  2>testerr   位于起始
   a.exec  2>testerr 程序段里面没有 >&2 则屏幕输出该段程序输出语句    且生成空的testerr文件
      说明错误生成文件由 >&2   来决定(错误的属性)
   b.程序段里面含有 >&2  则输出没被其定义的语句   被其定义的语句则导入生成文件
6.exec  2>testerr   位于非起始位置
    程序段里面没有被 >&2 定义的语句会  写入  exec 1>testout   生成文件里面  且不屏幕输出。

最后由此领悟到此处的数据显示对错误信息更敏感!!

阅读(1115) | 评论(0) | 转发(0) |
给主人留下些什么吧!~~