gpt4 book ai didi

linux - sed 行删除是原子的吗?

转载 作者:塔克拉玛干 更新时间:2023-11-03 00:16:01 30 4
gpt4 key购买 nike

假设我有一个文件“队列”。据我了解,使用“>>”附加到它是 POSIX 定义的原子。但是,如果我附加了多个进程,在不丢失数据的情况下执行以下操作是否安全?

sed -e '1d' -i 队列

我在 Linux 上有几个不同的脚本服务,它们可能想要在它们之间传递信息。每个都有一个“队列”,新数据被推到底部,下一个项目从顶部弹出。

我对 sed 的使用是否保证不会错过另一个进程在将文件读取到缓冲区和将其写回磁盘之间的追加操作?

谢谢。

最佳答案

我会遵从任何提出更强主张的人,但我认为使用这种方式并不安全。 -i 通过制作文件的临时副本然后将其移回原位来工作,这将替换中间的任何其他更改。

来源:http://www.gnu.org/software/sed/manual/sed.html

-i[SUFFIX] --in-place[=SUFFIX] This option specifies that files are to be edited in-place. GNU sed does this by creating a temporary file and sending output to this file rather than to the standard output.1. This option implies -s.

When the end of the file is reached, the temporary file is renamed to the output file's original name. The extension, if supplied, is used to modify the name of the old file before renaming the temporary file, thereby making a backup copy2).

This rule is followed: if the extension doesn't contain a *, then it is appended to the end of the current filename as a suffix; if the extension does contain one or more * characters, then each asterisk is replaced with the current filename. This allows you to add a prefix to the backup file, instead of (or in addition to) a suffix, or even to place backup copies of the original files into another directory (provided the directory already exists).

If no extension is supplied, the original file is overwritten without making a backup.

关于linux - sed 行删除是原子的吗?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/29986363/

30 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com