Linux Bash Shell学习(十七):I/O重定向

  本文也即《Learning the bash Shell》3rd Edition的第七章Input/Output and Command-Line Processing之读书笔记之一。我们曾经学习过shell的基本IO重定向操作:>、<和|。基本上能满足95%的情况,但是我们需要知道bash支持的重定向操作。

    • cmd1 |cmd2 : pipe,将cmd1 的标准输出作为cmd2 的标准输入
    • >file :将标准输出重定向到file
    • <file :将file作为标准输入
    • >>file :将标准输出重定向到file,如果file存在,append到文件中,即附加到文件的后面,而不是覆盖文件

      当cat不带参数的时候,表示使用标准输入作为输入,这允许在标准输入中键入相关的内容,下面将alias加入.bashrc作为最后一行
      $ cat >> .bashrc
        alias cdmnt='mount -t iso9660 /dev/sbpcd /cdrom'
        ^D

    • >|file :强制将标准输出重定向到file,即使noclobber设置。当设置环境变量set –o noclobber,将禁止重定向到一个已经存在的文件中,避免文件被覆盖。
    • n >|file :强制将文件描述符n重定向到file,即使noclobber打开
    • <>file :将file作为标准输入和标准输出。通常用于设备文件(/dev下面的文件),底层系统程序员可用之测试设备驱动,其他的很少用。
    • n <>file :将file作为文件描述符n的输入和输出
  • <<label :Here-document; see text 。将shell的标准输入作为命令的输入,直到行中之包含label。这中间的输入成为here-document。下面是一个例子。我们让人使用cat >> file的方式,通过标准输入在文件中附加内容。

    $ cat >> msgfile << . #这里<<.表明以.为结束。因此无需使用^D,而改用.
      > this is the text of
      > our message.
      > .   #这里表示结束。则msgfile中增加了两行this is…和our message.
    MACHINE="i586"
    OS="linux-gnu"
    CC="gcc"
    cat > $file <
    Machine: $MACHINE
    OS: $OS
    Compiler: $CC
    EOF
    查看:cat $file,这里给出正常结构
    Machine: i586
    OS: linux-gnu
    Compiler: gcc
    如果在EOF加上单引号,或者双引号如下
    cat > $file <<'EOF'
    则不解析$CC的内容,文件内容如下
    Machine: $MACHINE
    OS: $OS
    Compiler: $CC
    如果使用<<-,如下,则删除所有行中前面打头的> tab<这样在脚本的书写上会比较适合阅读 ,例如上面的例子可以写为:
    cat > $file <<-EOF
        Machine: $MACHINE
        OS: $OS
        Compiler: $CC
        EOF

  • n>file :将文件描述符n重定向到file
  • n :将file作为文件描述符的输入
  • n>>file :将文件描述符n的输出重定向到file,如果file存在,将输出append到文件后面
  • n>& :将标准输出复制到文件描述符n(Duplicate standard output to file descriptor n)
  • n<& :从文件描述符n复制标准输入(Duplicate standard input from file descriptor n)
  • n>&m :文件描述字n将一个copy至文件描述字m(File descriptor n is made to be a copy of the output file descriptor)
  • n<&m :文件描述字n作为文件描述字m中的一个拷贝(File descriptor n is made to be a copy of the input file descriptor)
  • &>file : 将标准输出和标准错误输出定向至文件file
  • <&- : 关闭标准输入
  • >&- : 关闭标准输出
  • n>&- : 关闭文件描述字作为输出(Close the output from file descriptor n)
  • n<&- :关闭文件描述字作输入(Close the input from file descriptor n)
  • n>&word: If n is not specified, the standard output (file descriptor 1) is used. If the digits in word do not specify a file descriptor open for output, a redirection error occurs. As a special case, if n is omitted, and word does not expand to one or more digits, the standard output and standard error are redirected as described previously.
  • n<&word : If word expands to one or more digits, the file descriptor denoted by n is made to be a copy of that file descriptor. If the digits in word do not specify a file descriptor open for input, a redirection error occurs. If word evaluates to -, file descriptor n is closed. If n is not specified, the standard input (file descriptor 0) is used.
  • n>&digit- : Moves the file descriptor digit to file descriptor n, or the standard output (file descriptor 1) if n is not specified.
  • n<&digit- : Moves the file descriptor digit to file descriptor n, or the standard input (file descriptor 0) if n is not specified. digit is closed after being duplicated to n.
文件描述符

  文件描述符在bash中比较少用,从0开始用户表示进行的数据流,0表示标准输入,1表示标准输出,2表示标注错误输出,其他从3开始。最为常用的场景是将错误消息输出到某个文件,可以加上2>file 到我们的命令中。

  我们来看下面一个脚本的例子:

command   > logfile 2>&1 &

  >logfile,表示command的标准输出重定向至文件logfile中,2>&1,匹配n>&m,表示文件描述字2(command的标准错误输出)将copy一份采用文件描述字1(即标准输出),由于标准输出已经重定向logfile,这份copy也见将重定向至文件lofgile。我们可以用“abcd > logfile 2>&1 &”来验证这个效果。最后&表示后台运行的方式。这样命令表示在后台运行command,而它的标准输出和错误输出均重定向到logfile文件中。下面可达到类似的效果:

command 2>&1 | tee logfile &

  错误输出同样适用标准输出,通过pipe方式,见他们作为输入执行tee logfile。tee命令将它的标准输入copy至他的标准标准输出以及参数所带的文件中。和上面的命令不一眼这里即会在stdout 和logfile中同时输出。

其他文件描述字的重定向,例如<&n,通常用于从多个文件中读入或者写出。

<&- ,表示强制关闭标准输入
>&- ,表示强制关闭标准输出
1> ,等同于>
0< ,等同于<

  相关链接: 我的Linux操作相关文章

  • 0
    点赞
  • 9
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值