Bash提供了I/O重定向工具,有3个缺省的文件(标准输出流):
stdin - 用来获取输入,好比键盘、文件重定向
stdout - 输出数据,缺省打印到屏幕
stderr - 输出错误信息,缺省打印到屏幕bash
理解I/O(标准输入/输出流):
工具
句柄 | 名字 | 描述 |
---|---|---|
0 | stdin | 标准输入 |
1 | stdout | 标准输出 |
2 | stderr | 标准错误输出 |
[root@ns_10.2.1.242 test]$ cat1 2> error.log [root@ns_10.2.1.242 test]$ cat error.log -bash: cat1: command not found [root@ns_10.2.1.242 test]$ echo 1 2> error.log 1 [root@ns_10.2.1.242 test]$ cat error.log [root@ns_10.2.1.242 test]$
$ command-name &>file or $ command > file-name 2>&1 [root@ns_10.2.1.242 test]$ echo 1 &>error.log [root@ns_10.2.1.242 test]$ cat error.log 1 [root@ns_10.2.1.242 test]$ cat1 &>error.log [root@ns_10.2.1.242 test]$ cat error.log -bash: cat1: command not found
使用下面的命令:code
$ command-name 2>&1 #注意下面两个例子的区别 # 第一个命令没法更改标准错误输出的内容, 第二个命令由于把stderr 重定向到stdin, 因此 cat1 被替换成了 test [root@ns_10.2.1.242 test]$ cat1 |sed 's/cat1/test/' -bash: cat1: command not found [root@ns_10.2.1.242 test]$ cat1 2>&1|sed 's/cat1/test/' -bash: test: command not found