网站开发技术

记点笔记、 学点技术 欢迎交流建站技术。本站关注lamp技术

您尚未登录。

#1 2015-03-20 14:52:49

admin
管理员

linux 自定义文件的输入和输出

linux 标注的输入和输出、错误是2是 0, 1 (linux 中的标准输入,输出,和错误

自己可以自定义一些。

首先要明白文件操作的三种方式

第一: 读取

第二: 追加

第三: 重写(先把文件清空,之后写入)

比如一个文件 

[root@qd ~]# cat a.txt
test
[root@qd ~]# cat < a.txt  # 读取操作
test
[root@qd ~]# echo "new test" > a.txt  #重写操作
[root@qd ~]# echo "other " >> a.txt   #追加操作
[root@qd ~]# cat a.txt
new test
other

如果要自定义一些使用命令exec 

做一个读的操作

[root@qd ~]# exec 3< a.txt
[root@qd ~]# cat <&3
new test
other

这里可以看到 没有使用文件名通过一个数 就读取了文件。

这里要注意 exec 3< 后面的文件是必须存在的。

做一个重写的操作

[root@qd ~]# exec 4>a.txt
[root@qd ~]# echo "test" >&4
[root@qd ~]# cat a.txt
test

做一个追加操作

[root@qd ~]# exec 5>>a.txt
[root@qd ~]# echo "test" >&5
[root@qd ~]# cat a.txt
test
test

这里可以看到 出现了两个test. 


ipbbs.net

离线

页脚

Powered by FluxBB