sed 行删除是原子的吗?
Is sed line deletion atomic?
假设我有一个文件 'queue'。据我了解,使用“>>”附加到它是原子的,如 POSIX 所定义。但是如果我附加了多个进程,在不丢失数据的情况下执行以下操作是否安全?
sed -e '1d' -i queue
我在 Linux 上有几个不同的脚本服务,可能需要在它们之间传递信息。每个都有一个 'queue',其中新数据被推到底部,下一个项目从顶部弹出。
我对 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.
假设我有一个文件 'queue'。据我了解,使用“>>”附加到它是原子的,如 POSIX 所定义。但是如果我附加了多个进程,在不丢失数据的情况下执行以下操作是否安全?
sed -e '1d' -i queue
我在 Linux 上有几个不同的脚本服务,可能需要在它们之间传递信息。每个都有一个 'queue',其中新数据被推到底部,下一个项目从顶部弹出。
我对 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.