Linux shell : Command 21

之前看到如下Linux hell 命令,一头雾水:

ls temp >list.txt 2>&1ls temp >/dev/null 2>&1查阅之后,明白此语句含义,特此记录.ls temp >list.txt 2>&1 (ps:temp是个不存在的目录)ls temp >list.txt :把标准输出重定向到list.txt文件中,在这里,就是把temp目录下所有的文件,列出来,,然后输出到list.txt文件如果没有这个重定向,那么标准输出默认是控制台标准输出的句柄应该是1 ,所以,完整的写法应该是:ls temp 1>list.txt

2>&1 :在此处 ,表示把标准错误输出写入到list.txt文件经过这个重定向,标准输出和标准错误输出都重定向到了list.txt中结果如下:root:ls temp >list.txt 2>&1root:/opt/tmp # more list.txtls: cannot access temp: No such file or directory //error 信息在文件里ls temp >/dev/null 2>&1这个表示把标准输出,以及标准错误输出都舍弃了,不显示也不保存如果换一下顺序,如何?ls temp 2>&1 >list.txt 标准错误输出定向到控制台,标准内容输出定向到list.txt结果如下:root:/opt/tmp # ls temp 2>&1 >out.txtls: cannot access temp: No such file or directory //error信息直接输出到控制台总结:ls temp :表示标准内容输出和标准错误都输出到控制台 等同于:ls temp>&1ls temp >list :表示标准内容输出到list文件中,标准错误输出还是在控制台 等同于:ls temp 1>listls temp >list 2>&1 :表示标准内容输出到list文件中,标准错误输出也在list文件中 等同于:ls temp >list 2>list ls temp 1>list 2>list ls temp 1>list 2>&1ls temp 2>& >list :标准错误输出也在控制台,表示标准内容输出到list文件中 等同于:ls temp 2>& 1>list

因为在路上你就已经收获了自由自在的好心情。

Linux shell : Command 21

相关文章:

你感兴趣的文章:

标签云: