linux 2>&1

版权声明:本文为博主原创文章,未经博主允许不得转载。 https://blog.csdn.net/nayi_224/article/details/83864347

2标准错误
1标准输出
>重定向,默认重定向的内容为标准输出
&等同

举些例子,先建立一个测试文件

vi shtest.sh
#!/bin/sh
echo "aaa"
echoo "bbb"

运行

sh shtest.sh

通常会输出这些

aaa
shtest.sh:line 3: echoo: command not found

其中,aaa 为标准输出,shtest.sh:line 3: echoo: command not found 为标准错误。

2>&1 通俗点说,就是把输出和bug信息这两条管道拧在了一起。

更多的例子
sh shtest.sh > out.txt 等同于sh shtest.sh 1> out.txt 。会在out.txt文件中(若没有,会自动生成)写入aaa,并在控制台打出shtest.sh:line 3: echoo: command not found

sh shtest.sh 2> out.txt,控制台只输出aaa,out.txt文件中写入shtest.sh:line 3: echoo: command not found

sh shtest.sh > out.txt 2>&1,控制台什么都不输出,out.txt文件写入
aaa
shtest.sh:line 3: echoo: command not found

sh shtest.sh 1> out1.txt 2> out2.txt,控制台什么都不输出,out1.txt写入aaa,out2.txt写入shtest.sh:line 3: echoo: command not found

猜你喜欢

转载自blog.csdn.net/nayi_224/article/details/83864347