Debian Bug report logs - #610778
echo to file is not atomic

version graph

Package: bash; Maintainer for bash is Matthias Klose <doko@debian.org>; Source for bash is src:bash.

Reported by: karl156 <karl156@abwesend.de>

Date: Sat, 22 Jan 2011 11:33:01 UTC

Severity: important

Found in version bash/4.1-3

Done: Jonathan Nieder <jrnieder@gmail.com>

Bug is archived. No further changes may be made.

Toggle useless messages

View this report as an mbox folder, status mbox, maintainer mbox


Report forwarded to debian-bugs-dist@lists.debian.org, Matthias Klose <doko@debian.org>:
Bug#610778; Package bash. (Sat, 22 Jan 2011 11:33:04 GMT) Full text and rfc822 format available.

Acknowledgement sent to karl156 <karl156@abwesend.de>:
New Bug report received and forwarded. Copy sent to Matthias Klose <doko@debian.org>. (Sat, 22 Jan 2011 11:33:04 GMT) Full text and rfc822 format available.

Message #5 received at submit@bugs.debian.org (full text, mbox):

From: karl156 <karl156@abwesend.de>
To: submit@bugs.debian.org
Subject: echo to file is not atomic
Date: Sat, 22 Jan 2011 12:29:38 +0100
Package: bash
Version: 4.1-3
Severity: important
Tags: squeeze sid


When I redirect an echo output to a file the output gets mixed with
other output.

---- Demo script
#!/bin/sh
echo -ne "\na $1\nb $1\nc $1\n">>"/test"
---- Demo script end

When I call this script twice very fast (with "1" and "2" as
parameters), I get random output in the file like:

---- Actual file content


a 1
a 2
b 1
b 2
c 1
c 2
---- Actual file content end

Sometimes the output is even more mixed.
But I should get this:

---- Expected file content

a 1
b 1
c 1

a 2
b 2
c 2
---- Expected file content end

I think that bash is splitting the output before writing them to the
file, because the split always happens at the newlines.

Is there any workaround for this?




Added tag(s) wheezy. Request was from Kurt Roeckx <kurt@roeckx.be> to control@bugs.debian.org. (Wed, 16 Feb 2011 19:05:01 GMT) Full text and rfc822 format available.

Removed tag(s) squeeze, sid, and wheezy. Request was from Jonathan Nieder <jrnieder@gmail.com> to control@bugs.debian.org. (Tue, 21 Jun 2011 07:43:02 GMT) Full text and rfc822 format available.

Reply sent to Jonathan Nieder <jrnieder@gmail.com>:
You have taken responsibility. (Tue, 21 Jun 2011 07:43:05 GMT) Full text and rfc822 format available.

Notification sent to karl156 <karl156@abwesend.de>:
Bug acknowledged by developer. (Tue, 21 Jun 2011 07:43:06 GMT) Full text and rfc822 format available.

Message #14 received at 610778-done@bugs.debian.org (full text, mbox):

From: Jonathan Nieder <jrnieder@gmail.com>
To: karl156 <karl156@abwesend.de>
Cc: 610778-done@bugs.debian.org
Subject: Re: echo to file is not atomic
Date: Tue, 21 Jun 2011 02:37:17 -0500
# Please do not use these tags unless you want to override
# the automatic version tracking.
tags 610778 - sid squeeze wheezy
quit

Hi Karl,

karl156 wrote:

> When I redirect an echo output to a file the output gets mixed with
> other output.
>
> ---- Demo script
> #!/bin/sh
> echo -ne "\na $1\nb $1\nc $1\n">>"/test"
> ---- Demo script end
>
> When I call this script twice very fast (with "1" and "2" as
> parameters), I get random output in the file like:
>
> ---- Actual file content
>
> a 1
> a 2
> b 1
[...]
> I think that bash is splitting the output before writing them to the
> file, because the split always happens at the newlines.
>
> Is there any workaround for this?

Yes, I can reproduce this:

	$ strace -e write bash -c 'echo -ne "foo\nbar\nbaz\n"'
	write(1, "foo\n", 4foo
	)                    = 4
	write(1, "bar\n", 4bar
	)                    = 4
	write(1, "baz\n", 4baz
	)                    = 4

However, I don't think it is a bug.  To control buffering, your best
bet is something like

	echo ... | dd ibs=1k obs=1k >>log

or

	perl -e 'syswrite(STDOUT, ...)' >>log

Alas, I have no better suggestions.

Hope that helps,
Jonathan




Bug archived. Request was from Debbugs Internal Request <owner@bugs.debian.org> to internal_control@bugs.debian.org. (Wed, 20 Jul 2011 07:34:27 GMT) Full text and rfc822 format available.

Send a report that this bug log contains spam.


Debian bug tracking system administrator <owner@bugs.debian.org>. Last modified: Mon Apr 21 00:05:31 2014; Machine Name: buxtehude.debian.org

Debian Bug tracking system
Copyright (C) 1999 Darren O. Benham, 1997,2003 nCipher Corporation Ltd, 1994-97 Ian Jackson.