linux脚本之>/dev/null 2>&1,以及2>1 VS 2>&1
2011-06-07 14:11
507 查看
1. 标准输入stdin文件描述符为0,标准输出stdout文件描述符为1,标准错误stderr文件描述符为2
2. /dev/null 空设备,相当于垃圾桶
3. 重定向符号:>
3. 2>1 与 2>&1 的区别
2>1, 把标准错误stderr重定向到文件1中
2>&1,把标准错误stderr重定向到标准输出stdout
4. 举例:
假设有脚本test.sh,内容如下,t是一个不存在的命令,执行脚本进行下面测试。
# cat test.sh
t
date
标准输出重定向到log,错误信息输出到终端上,如下:
# ./test.sh > log
./test.sh: line 1: t: command not found
# cat log
Thu Oct 23 22:53:02 CST 2008
删除log文件,重新执行,这次是把标准输出定向到log,错误信息定向到文件1
# ./test.sh > log 2>1
#
# cat log
Thu Oct 23 22:56:20 CST 2008
# cat 1
./test.sh: line 1: t: command not found
#
把标准输出重定向到log文件,把标准错误重定向到标准输出
# ./test.sh > log 2>&1
#
# cat log
./test.sh: line 1: t: command not found
Thu Oct 23 22:58:54 CST 2008
#
把错误信息重定向到空设备
# ./test.sh 2>/dev/null
Thu Oct 23 23:01:07 CST 2008
#
把标准输出重定向到空设备
# ./test.sh >/dev/null
./test.sh: line 1: t: command not found
把标准输出和标准错误全重定向到空设备
#./test.sh >/dev/null 2>&1
#
把标准输出和标准错误全重定向到空设备
#./test.sh >/dev/null 2>&1
2. /dev/null 空设备,相当于垃圾桶
3. 重定向符号:>
3. 2>1 与 2>&1 的区别
2>1, 把标准错误stderr重定向到文件1中
2>&1,把标准错误stderr重定向到标准输出stdout
4. 举例:
假设有脚本test.sh,内容如下,t是一个不存在的命令,执行脚本进行下面测试。
# cat test.sh
t
date
标准输出重定向到log,错误信息输出到终端上,如下:
# ./test.sh > log
./test.sh: line 1: t: command not found
# cat log
Thu Oct 23 22:53:02 CST 2008
删除log文件,重新执行,这次是把标准输出定向到log,错误信息定向到文件1
# ./test.sh > log 2>1
#
# cat log
Thu Oct 23 22:56:20 CST 2008
# cat 1
./test.sh: line 1: t: command not found
#
把标准输出重定向到log文件,把标准错误重定向到标准输出
# ./test.sh > log 2>&1
#
# cat log
./test.sh: line 1: t: command not found
Thu Oct 23 22:58:54 CST 2008
#
把错误信息重定向到空设备
# ./test.sh 2>/dev/null
Thu Oct 23 23:01:07 CST 2008
#
把标准输出重定向到空设备
# ./test.sh >/dev/null
./test.sh: line 1: t: command not found
把标准输出和标准错误全重定向到空设备
#./test.sh >/dev/null 2>&1
#
把标准输出和标准错误全重定向到空设备
#./test.sh >/dev/null 2>&1
相关文章推荐
- linux脚本之>/dev/null 2>&1,以及2>1 VS 2>&1
- Linux脚本之>/dev/null 2>&1,以及2>1 VS 2>&1
- Linux:shell脚本命令: >/dev/null 2>&1 的理解
- Linux Shell脚本入门--(linux空设备文件和重定向)>/dev/null 2>&1
- Linux下Shell输出重定向到空设备文件1>/dev/null 2>&1
- Linux Shell >/dev/null 2>&1分析解释
- Shell脚本———— /dev/null 2>&1详解
- Linux下" >/dev/null 2>&1 "相关知识说明
- Linux下">/dev/null 2>&1 "相关知识说明
- Linux下" >/dev/null 2>&1 "相关知识说明
- Shell脚本中的:>/dev/null 2>&1 的作用
- 脚本中/dev/null 2>&1的用法
- linux中常见的>/dev/null 2>&1的作用
- Linux下" >/dev/null 2>&1 "相关知识说明
- 【Linux】/dev/null 2>&1 详解
- [应用]Linux下" >/dev/null 2>&1 "
- Linux下" >/dev/null 2>&1 "相关知识说明
- Linux下" >/dev/null 2>&1 "相关知识说明
- linux命令后面常见的>/dev/null 和 2>&1 的含义
- linux上 nohup command >/dev/null 2>&1 &的含义