第三章 文件的描述符和重定向

第三章 文件的描述符和重定向

解释

文件描述符是和文件的输入、输出相关联的非负整数,Linux内核(kernel)利用文件描述符(file descriptor)来访问文件。打开现存文件或新建文件时,内核会返回一个文件描述符。读写文件也需要使用文件描述符来指定待读写的文件。常见的文件描述符是stdin、stdout和stderr。

系统预留文件描述符

  • 0 stdin(标准输入)

  • 1 stdout(标准输出)

  • 2 stderr(标准错误)

重定向

重定向将输入文本通过截取模式保存到文件(清空文件):

[root@ceshi ~]# echo "this is a test file" > test.txt
[root@ceshi ~]# more test.txt 
this is a test file

注意:写入文件之前,test.txt会被清空

重定向将输入文本通过追加模式保存到文件(文件不被清空):

[root@ceshi ~]# echo "this is a test file222" >> test.txt
[root@ceshi ~]# more test.txt
this is a test file
this is a test file222

标准错误输出

标准错误输出:

[root@ceshi ceshi]# ls
a  aaaa.sql  b  bb.bak  bb.io  catalog  e.ior  r.doc
[root@ceshi ceshi]# cat aaa.txt
cat: aaa.txt: No such file or directory
# 因为当前目录没有aaa.txt 所以报错没有这个文件或者目录

标准错误输出重定向

方法一:
[root@ceshi ceshi]# cat aaa.txt 2> a.txt
[root@ceshi ceshi]# cat a.txt 
cat: aaa.txt: No such file or directory

方法二:
[root@ceshi ceshi]# cat aaa.txt &> aa.txt 
[root@ceshi ceshi]# cat aa.txt 
cat: aaa.txt: No such file or directory

#这样就不会有报错提示,而报错提示被重定向到了别的文件中。

标准错误重定向到/dev/null

/dev/null 是一个特殊的设备文件,接收的所有内容都会被丢弃,通常被称为位桶、黑洞。

[root@ceshi ceshi]# cat aaa.txt 2> /dev/null

tee命令

tee命令可以将数据重定向到文件,同时还可以提供一份重定向数据的副本作为后续命令的stdin。

[root@ceshi ceshi]# ip addr| grep inet | tee out.txt | cat -n
     1      inet 127.0.0.1/8 scope host lo
     2      inet6 ::1/128 scope host 
     3      inet 103.57.56.11/29 brd 103.57.56.15 scope global ens160
     4      inet6 fe80::8747:c951:a2cf:efb0/64 scope link 
[root@ceshi ceshi]# more out.txt 
    inet 127.0.0.1/8 scope host lo
    inet6 ::1/128 scope host 
    inet 103.57.56.11/29 brd 103.57.56.15 scope global ens160
    inet6 fe80::8747:c951:a2cf:efb0/64 scope link 

重定向脚本内的文本片段到文件

[root@ceshi ceshi]# vi s.sh
#!/bin/bash
cat <<EOF>text.log
first line
second line 
three line
EOF

[root@ceshi ceshi]# /bin/bash s.sh 
[root@ceshi ceshi]# more text.log 
first line
second line 
three line

在 <<EOF>text.log与下一个EOF行之间的所有文本都会当作stdin保存到text.log文件里。

自定义文件描述符

除了0/1/2对应的stdin、stdout、stderr是系统预留描述符,还可以用exec命令创建自定义文件描述符,文件的打开模式有只读模式、截断模式、追加模式。

"<" 操作符用于从文件中读取到stdin

[root@ceshi ceshi]# exec 3<text.log  # 自定义文件描述符3打开并读取文件

手动使用描述符
[root@ceshi ceshi]# cat <&3
first line
second line 
three line

注意:这里只能读取一次自定义的描述符,如果再次使用需要重新创建文件描述符。

">" 操作符用于截断模式的文件写入(数字在文件内容被截断之后写入)

[root@ceshi ceshi]# exec 4>output.txt   # 定义文件描述符为4,截断模式,输入文件名是output.txt
[root@ceshi ceshi]# echo this is a new test line >&4    # 截断内容
[root@ceshi ceshi]# cat output.txt  # 查看截断后的文件内容
this is a new test line

截断模式 可以多次截断吗?
[root@ceshi ceshi]# cat output.txt 
111111111111111111111
222222222222222222222
[root@ceshi ceshi]# exec 4>output.txt
[root@ceshi ceshi]# echo this is a new test line >&4
[root@ceshi ceshi]# echo "截断模式能多次截断吗?" >&4             
[root@ceshi ceshi]# cat output.txt 
this is a new test line
截断模式能多次截断吗?

总结:
1、截断模式会清空文件,在写入文件
2、截断模式,可以多次截断内容,写入文件

">>" 操作符用于追加模式的文件写入(添加数据到文件中,原有数据不会丢失)

[root@ceshi ceshi]# exec 5>>output.txt
[root@ceshi ceshi]# echo "追加模式的内容" >&5 
[root@ceshi ceshi]# cat output.txt 
this is a new test line
截断模式能多次截断吗?
追加模式的内容

猜你喜欢

转载自blog.51cto.com/506554897/2114410