Bash read/write file descriptors — seek to start of file

前端 未结 7 1803
暗喜
暗喜 2020-12-08 08:48

I tried to use the read/write file descriptor in bash so that I could delete the file that the file descriptor referred to afterward, as such:

F=$(mktemp)
ex         


        
相关标签:
7条回答
  • 2020-12-08 09:22

    I found a way to do it in bash, but it's relying on an obscure feature of exec < /dev/stdin which actually can rewind the file descriptor of stdin according to http://linux-ip.net/misc/madlug/shell-tips/tip-1.txt:

    F=$(mktemp)
    exec 3<> "$F"
    rm -f "$F"
    
    echo "Hello world" >&3
    { exec < /dev/stdin; cat; } <&3
    

    The write descriptor isn't affected by that so you can still append output to descriptor 3 before the cat.

    Sadly I only got this working under Linux not under MacOS (BSD), even with the newest bash version. So it doesn't seem very portable.

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