• 2>&1


    http://www.cnblogs.com/caolisong/archive/2007/04/25/726896.html

    nohup /mnt/Nand3/H2000G  >/dev/null  2>&1  &
          对于& 1 更准确的说应该是文件描述符 1,而1 一般代表的就是STDOUT_FILENO,

         实际上这个操作就是一个dup2(2)调用.他标准输出到all_result ,然后复制标准输出到文件描述符2(STDERR_FILENO),其后果就是文件描述符1和2指向同一个文件表项,也可以说错误的输出被合并了.其中0表示键盘输入 1表示屏幕输出 2表示错误输出.把标准出错重定向到标准输出,然后扔到/DEV/NULL下面去。通俗的说,就是把所有标准输出和标准出错都扔到垃圾桶里面。

    为何2>&1要写在后面?      

          command > file 2>&1 
          首先是command > file将标准输出重定向到file中, 2>&1 是标准错误拷贝了标准输出的行为,也就是同样被重定向到file中,最终结果就是标准输出和错误都被重定向到file中。 
          command 2>&1 >file 
          2>&1 标准错误拷贝了标准输出的行为,但此时标准输出还是在终端。>file 后输出才被重定向到file,但标准错误仍然保持在终端。

    https://en.wikipedia.org/wiki/Redirection_(computing)

    0 stdin    Standard input

    1 stdout  Standard output

    2 stderr  Standard error

    linux shell中"2>&1"含义

    在计划任务中经常可以看到。例如我们公司的计划任务举例:

    */2 * * * * root cd /opt/xxxx/test_S1/html/xxxx/admin; php index.php task testOne >/dev/null 2>&1
    */2 * * * * root cd /opt/xxxx/test_S1/html/xxxx/admin; php index.php task testTwo >/dev/null 2>&1

    对于& 1 更准确的说应该是文件描述符 1,而1标识标准输出,stdout。
    对于2 ,表示标准错误,stderr。
    2>&1 的意思就是将标准错误重定向到标准输出。这里标准输出已经重定向到了 /dev/null。那么标准错误也会输出到/dev/null

    可以把/dev/null 可以看作"黑洞". 它等价于一个只写文件. 所有写入它的内容都会永远丢失. 而尝试从它那儿读取内容则什么也读不到.

    偶尔也可以把 & 在命令的最后加上,表示让程序后台执行。

    为何2>&1要写在后面?

    index.php task testOne >/dev/null 2>&1

    我们可以理解为,左边是标准输出,好,现在标准输出直接输入到 /dev/null 中,而2>&1是将标准错误重定向到标准输出,所以当程序产生错误的时候,相当于错误流向左边,而左边依旧是输入到/dev/null中。

    可以理解为,如果写在中间,那会把隔断标准输出指定输出的文件

    你可以用

    ls 2>1测试一下,不会报没有2文件的错误,但会输出一个空的文件1;
    ls xxx 2>1测试,没有xxx这个文件的错误输出到了1中;
    ls xxx 2>&1测试,不会生成1这个文件了,不过错误跑到标准输出了;
    ls xxx >out.txt 2>&1, 实际上可换成 ls xxx 1>out.txt 2>&1;重定向符号>默认是1,错误和输出都传到out.txt了。

    About Redirection

    Short description

    Explains how to redirect output from PowerShell to text files.

    Long description

    By default, PowerShell sends its command output to the PowerShell console. However, you can direct the output to a text file, and you can redirect error output to the regular output stream.

    You can use the following methods to redirect output:  3种方式

    • Use the Out-File cmdlet, which sends command output to a text file. Typically, you use the Out-File cmdlet when you need to use its parameters, such as the Encoding, Force, Width, or NoClobber parameters.

    • Use the Tee-Object cmdlet, which sends command output to a text file and then sends it to the pipeline.

    • Use the PowerShell redirection operators.

    PowerShell redirection operators  这里单独说明了第三种方式

    The redirection operators enable you to send streams of data to a file or the Success output stream.

    The PowerShell redirection operators use the following numbers to represent the available output streams:

    Stream #DescriptionIntroduced in
    1 Success Stream PowerShell 2.0
    2 Error Stream PowerShell 2.0
    3 Warning Stream PowerShell 3.0
    4 Verbose Stream PowerShell 3.0
    5 Debug Stream PowerShell 3.0
    6 Information Stream PowerShell 5.0
    * All Streams PowerShell 3.0

    The PowerShell redirection operators are as follows, where n represents the stream number. The Success stream ( 1 ) is the default if no stream is specified.

    OperatorDescriptionSyntax
    > Send specified stream to a file. n>
    >> Append specified stream to a file. n>>
    >&1 Redirects the specified stream to the Success stream. n>&1
  • 相关阅读:
    【pycharm 密钥】pycharm 2017 密钥
    【jenkins 忘记密码】忘记Jenkins管理员密码的解决办法
    【git 报错】Could not read from remote repository.Please make sure you have the correct access rights.
    【pycharm 警告】unittest RuntimeWarning: Parent module ” not found while handling absolute import
    【python接口自动化测试教程】00---00章节就代表开篇吧
    【python-strip】Python strip()方法
    认识map-reduce
    subprocess实用手册
    k8s学习路线
    nginx小知识
  • 原文地址:https://www.cnblogs.com/chucklu/p/7603052.html
Copyright © 2020-2023  润新知