How do I use exec 3>myfifo in a script, and not have echo foo>&3 close the pipe?

前端 未结 2 876
礼貌的吻别
礼貌的吻别 2020-12-19 21:50

Why can\'t I use exec 3>myfifo in the same manner in a bash script as I can in my terminal?

I\'m using named pipes to turn an awk filter into a simple \"se

2条回答
  •  慢半拍i
    慢半拍i (楼主)
    2020-12-19 22:31

    I think I figured it out!

    The exec commands do work, but bash itself closes all open file descriptors on exiting from the script. Adding sleep 5 to the end of the client shows that it takes 5 seconds for the server to finally shut down.

    So the solution is just to open some file descriptor to my named pipes from some other terminal, and just keep them open, e.g. in terminal 3:

    $ exec 3>to_server; exec 4

    or, in the server terminal/script itself:

    while true; do 
      # Really, this awk script BEGIN's with reading in a huge file, 
      # thus the client-server model
      awk '{sub("wrong", "correct");print;} /\0/ {fflush();}' from_server &
      AWKPID=$!
      exec 3>to_server; exec 4

提交回复
热议问题