标准的文件描述符
文件描述符 缩写 描述
0 STDIN 标准输入
1 STDOUT 标准输出
2 STDEER 标准错误
STDOUT文件的标准输出 在终端页面上,标准输出是终端显示器
输出重定向
[zhidaobu@localhost test]$ ls -l > 1.txt
[zhidaobu@localhost test]$ cat 1.txt
总用量 4
-rw-rw-r--. 1 zhidaobu zhidaobu 0 10月 29 21:37 1.txt
-rw-rw-r--. 1 zhidaobu zhidaobu 75 10月 9 19:21 2.txt
-rw-rw-r--. 1 zhidaobu zhidaobu 0 9月 26 11:22 text3
[zhidaobu@localhost test]$
STDEER
重定向错误
2>
[zhidaobu@localhost test]$ ls -al fwfwf 2> 1.txt
[zhidaobu@localhost test]$ cat 1.txt
ls: 无法访问'fwfwf': 没有那个文件或目录
1>正常输出重定向数据
全部重定向,将STDERR STDOUT的输出重定向到一个文件中
&>
在脚本中重定向输出
临时重定向
[zhidaobu@localhost test]$ ls
1.txt 2.txt text3
[zhidaobu@localhost test]$ vim 1.txt
[zhidaobu@localhost test]$ bash 1.txt
this is a error
this is a normal output
[zhidaobu@localhost test]$ bash 1.txt 2>2.txt
this is a normal output
[zhidaobu@localhost test]$ cat 1.txt
#!/bin/bash
echo "this is a error" >&2
echo "this is a normal output"
[zhidaobu@localhost test]$ cat 2.txt
this is a error
[zhidaobu@localhost test]$
永久重定向
如果使用大量的数据需要重定向,那重定向每个echo语句就会很繁琐
可以使用exec命令告诉shell在脚本执行期间重定向某个特定文件描述符