zoukankan      html  css  js  c++  java
  • 永久重定向

    如果在脚本中重定向许多数据,那么重定向每个echo语句就不太方便了,这种情况下,可以使用exec命令通知shell脚本执行期间重定向特定的文件描述符

    文件名为: sh12.sh 

    #!/bin/bash
    # redirecting all output to a file
    exec 1>testout
    
    echo "This is a test of redirecting all output"
    echo "from a script to another file."
    echo "without haviing to redirect every individual line"

    exec命令启动一个新的shell,并将STDOUT文件描述重定向到一个文件,所有定向到STDOUT的脚本输出都将重定向到这个文件

    运行 sh sh12.sh ,你会发现没输出任何东西,然后查看testout文件, cat testout 

    输出:

    This is a test of redirecting all output
    from a script to another file.
    without haviing to redirect every individual line

    然后将错误信息和正确信息分别重定向不同的文件

    文件名 sh13.sh 

    #!/bin/bash
    # redirecting output to different locations
    
    exec 2>testerror
    
    echo "This is the start of the script"
    echo "now reidirecting all output to another location"
    
    exec 1>testout
    
    echo "This output should go to the testout testout file"
    echo "but this should go to the testerror file" >&2

    运行 sh sh13.sh 

    输出:

    This is the start of the script
    now reidirecting all output to another location

    查看testout文件的内容 cat testout 

    输出: This output should go to the testout testout file 

    查看testerror文件的内容 cat testrror 

    输出: but this should go to the testerror file 

  • 相关阅读:
    FZU2056 最大正方形(二分答案)
    2014暑期集训个人赛
    2014 acm鞍山现场赛总结
    hdu1853 Cyclic Tour (二分图匹配KM)
    zoj3826 Hierarchical Notation (字符串模拟)
    hdu3047 Zjnu Stadium (并查集)
    hdu4886 TIANKENG’s restaurant(Ⅱ) (trie树或者模拟进制)
    CodeForces 287B Pipeline (水题)
    wpf窗体项目 生成dll类库文件
    sqlserver 汉字转全拼函数
  • 原文地址:https://www.cnblogs.com/jacson/p/4791398.html
Copyright © 2011-2022 走看看