Re: I/O redirection problem
From: Stephane CHAZELAS (this.address_at_is.invalid)
Date: 05/26/04
- Next message: KenW: "Re: MS Word conversion to tiff"
- Previous message: Stephane CHAZELAS: "Re: I/O redirection problem"
- In reply to: Stephane CHAZELAS: "Re: I/O redirection problem"
- Messages sorted by: [ date ] [ thread ] [ subject ] [ author ]
Date: Wed, 26 May 2004 13:28:58 +0000
2004-05-26, 12:54(+00), Stephane CHAZELAS:
> 2004-05-26, 04:32(-07), Krit Kasemosoth:
>> I have data file 'test.txt'. I want to search text in that file and
>> store
>> the result back to file 'test.txt'. The command that I execute is :
>>
>> cat test.txt | grep monday > test.txt
> [...]
>
> That's a common error.
>
> "cat test.txt" and "grep monday > test.txt" are two commands run
> at the same time. The right one opens the test.txt file with
> truncation, so reduces it to an empty file. If the left one has
> not opened test.txt for reading before, it sees an empty file.
> Even if it opened it before, you may have unexpected results.
[...]
Actually, it depends on how much data "cat" was able to read
before the shell on the RHS truncate the file.
cat implementations often perform 4096 byte long reads. cat may
be able to perform several reads, but, it will be limited by the
size of the pipe buffer anyway. So, you can be sure that it
won't work for files bigger than PIPE_BUF + 4046.
You would get weirder behaviors if, instead of grep, you had
have a command that outputs more data than it reads some, and
you'd have ended up in an infinite self-fed loop.
-- Stephane
- Next message: KenW: "Re: MS Word conversion to tiff"
- Previous message: Stephane CHAZELAS: "Re: I/O redirection problem"
- In reply to: Stephane CHAZELAS: "Re: I/O redirection problem"
- Messages sorted by: [ date ] [ thread ] [ subject ] [ author ]
Relevant Pages
|