Using the same file for stdin and stdout with redirection

后端 未结 3 605
误落风尘
误落风尘 2020-12-30 04:56

I\'m writing a application that acts like a filter: it reads input from a file (stdin), processes, and write output to another file (stdout). The input file is completely re

相关标签:
3条回答
  • 2020-12-30 05:24

    For a solution of a purely academic nature:

    $ ( unlink file.txt && ./myprog >file.txt ) <file.txt
    

    Possibly problematic side-effects are:

    • If ./myprog fails, you destroy your input. (Naturally...)
    • ./myprog runs from a subshell (Use { ... ; } instead of ( ... ) to avoid.)
    • file.txt becomes a new file with a new inode and file permissions.
    • You need +w permission on the directory housing file.txt.
    0 讨论(0)
  • 2020-12-30 05:26

    The shell is what clobbers your output file, as it's preparing the output filehandles before executing your program. There's no way to make your program read the input before the shell clobbers the file in a single shell command line.

    You need to use two commands, either moving or copying the file before reading it:

    mv file.txt filecopy.txt
    ./myprog < filecopy.txt > file.txt
    

    Or else outputting to a copy and then replacing the original:

    ./myprog < file.txt > filecopy.txt
    mv filecopy.txt file.txt
    

    If you can't do that, then you need to pass the filename to your program, which opens the file in read/write mode, and handles all the I/O internally.

    ./myprog file.txt                 # reads and writes according to its own rules
    
    0 讨论(0)
  • 2020-12-30 05:48

    There's a sponge utility in moreutils package:

    ./myprog < file.txt | sponge file.txt
    

    To quote the manual:

    Sponge reads standard input and writes it out to the specified file. Unlike a shell redirect, sponge soaks up all its input before opening the output file. This allows constructing pipelines that read from and write to the same file.

    0 讨论(0)
提交回复
热议问题