Linux文件描述符的简单操作

大家都知道,Linux系统将每个对象当做文件来处理,包括了输入与输出。Linux使用文件描述符(file descriptor)标识每个文件对象。文件描述符是一个非负整数,可以唯一的标识会话中打开的文件。每个进程中最多可以有9个打开的文件描述符。bash shell保留了前三个文件描述符(0、1和2)来作为数据流重定向。关于数据流重定向可以参考man bash页面中关于REDIRECTION描述。


文件描述符
缩写
说明
符号
0
STDIN
标准输入
<或<<
1
STDOUT
标准输出
>或>>
2
STDERR
标准错误输出
2>或2>>

对符号的说明

  • 1>或者>:以覆盖的方式将正确的数据输出到指定的文件
  • 1>>或者>>:以累加的方式将正确的数据输出到指定的文件
  • 2>或者>:以覆盖的方式将错误的数据输出到指定的文件
  • 2>>或者>>:以累加的方式将错误的数据输出到指定的文件
  • 2>&1或者&>:以覆盖的方式同时将正确和错误的数据输出到指定的文件


例子

[root@rhel6164 test]# echo1 "this is testing" > test.txt 2>&1 #同时重定向正确和错误输出到test.txt文件
[root@rhel6164 test]# cat test.txt
-bash: echo1: command not found
[root@rhel6164 test]# echo1 "this is testing" &> test.txt #同时重定向正确和错误输出到test.txt文件
[root@rhel6164 test]# cat test.txt
-bash: echo1: command not found
[root@rhel6164 test]# rpmbuild -ba xxx.spec 2>&1 | tee log.txt #编译rpm包的时候,同时把正确和错误的log输出到屏幕和保存到log.txt文件

标准输入
对于终端接口,标准输入时为键盘,shell通过STDIN文件描述符从键盘接受输入。当我们使用输入重定向符的时候,Linux将使用重定向引用的文件替代标准的键盘输入。

[root@rhel6164 test]# cat > test.txt #cat命令来新建文件
this is testing file #用[Ctrl]+d结束输入
[root@rhel6164 test]# cat test.txt
this is testing file
用stdin来替代键盘输入

[root@rhel6164 test]# cat > test_new.txt < test.txt 
[root@rhel6164 test]# cat test_new.txt
this is testing file
可以不用[Ctrl]+d来结束输入,而是用<<来结束输入

[root@rhel6164 test]# cat > test_new1.txt << EOF #<<代表结束输入字符的意思,当输入完成的时候,就可以输入EOF,也可以定义其它字符作为输入结束字符标志
> this is testing file using EOF.
> EOF
[root@rhel6164 test]# cat test_new1.txt
this is testing file using EOF.

当我们知道错误信息会发生,但是我们不关注发生了什么错误信息,可以直接将错误信息重定向到/dev/null文件。

[root@rhel6164 test]# cat1 xxx 2> /dev/null


在脚本中重定向输出

  • 临时重定向每一行,必须要在文件描述符前加上&号
  • 在脚本中永久重定向所有的命令

临时重定向每一行

$ echo "this is testing" >&2 该行在脚本的STDERR文件描述符指向的地方而不是一般的STDOUT上显示以上文本。

[root@rhel6164 test]# cat test.sh
#!/bin/bash

echo "this is an error" >&2
echo "this is normal output"
[root@rhel6164 test]# bash test.sh #默认情况下,Linux将STDOUT和STDEER都输出到屏幕,所以可以看到两行输出
this is an error
this is normal output
[root@rhel6164 test]# bash test.sh 2> test.txt #重定向标准错误输出到文件,在屏幕上就只能看到标准的正确输出
this is normal output
[root@rhel6164 test]# cat test.txt
this is an error

永久重定向;可以对每一行进行永久重定向,如果想对脚本中所有的标准输出或者标准错误输出进行永久重定向,可以使用exec命令来完成

[root@rhel6164 test]# cat test.sh
#!/bin/bash

exec 2> testerror #重定向标准错误输出到testerror文件

echo "this is start of the script"
echo "now, redirecting all output to another file"

exec 1> testout #重定向标准输出到testout文件

echo "this output should go to the testout file"
echo "this should go to the testerr file" >&2 #标准错误输出
[root@rhel6164 test]# bash test.sh
this is start of the script
now, redirecting all output to another file
[root@rhel6164 test]# cat testerror
this should go to the testerr file
[root@rhel6164 test]# cat testout
this output should go to the testout file

创建自己的输出文件描述符

[root@rhel6164 test]# cat test.sh
#!/bin/bash

exec 3> test3out #定义文件描述符3,并把输出显示到test3out文件中

echo "This should display on the screen"
echo "this should be stored in the test3out file" >&3 #该行将不会显示在屏幕上,将会显示到文件描述符为3的地方
echo "this should be back to the sreen"
[root@rhel6164 test]# bash ./test.sh
This should display on the screen
this should be back to the sreen
[root@rhel6164 test]# cat test3out
this should be stored in the test3out file

创建输出与输入文件描述符 e.g. $exec 3<>testfile


关闭文件描述符

如果创建新的输入或输出文件描述符,shell将在脚本退出时自动关闭它们,如果想在脚本结束前就手动关闭文件描述符,可以将他重定向到特殊符号&-。e.g. $exec 3>&-


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值