为什么通过I/O重定向读写同一文件会导致Unix中的文件为空? [英] Why reading and writing the same file through I/O redirection results in an empty file in Unix?

查看:92
本文介绍了为什么通过I/O重定向读写同一文件会导致Unix中的文件为空?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如果我将命令的输出重定向到其读取的相同文件,则其内容将被删除.

If I redirect output of a command to same file it reads from, its contents is erased.

sed 's/abd/def/g' a.txt > a.txt

任何人都可以解释原因吗?

Can anyone explain why?

推荐答案

重定向的第一件事是打开文件进行写入,从而清除所有现有内容. sed然后尝试读取您刚刚创建的这个空文件,并且不执行任何操作.然后关闭文件,不包含任何内容.

The first thing the redirection does is to open the file for writing, thus clearing any existing contents. sed then tries to read this empty file you have just created, and does nothing. The file is then closed, containing nothing.

这篇关于为什么通过I/O重定向读写同一文件会导致Unix中的文件为空?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆