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

前端 未结 7 1804
暗喜
暗喜 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:07

    #!/bin/bash
    F=$(mktemp tmp.XXXXXX)
    exec 3<> $F
    rm $F
    
    echo "Hello world" >&3
    cat /dev/fd/3
    

    As suggested in other answer, cat will rewind the file descriptor for you before reading from it since it thinks it's just a regular file.

提交回复
热议问题