Bash: read stdin from file and write stdout to file

后端 未结 2 954
陌清茗
陌清茗 2020-12-08 17:23

I\'m trying to run an app (let\'s say top) so it will read from a file for stdin and write to another file from stdout.

Currently I have



        
2条回答
  •  一个人的身影
    2020-12-08 18:09

    Is there a way I can map stdin and stdout to files and use them to control a cli app?

    It sounds like you are looking for coprocesses, added to Bash in 4.0.

    coproc cat                    # Start cat in background
    echo Hello >&${COPROC[1]}     # Say "Hello" to cat
    read LINE <&${COPROC[0]}      # Read response
    echo $LINE                    # cat replied "Hello"!
    

    Before 4.0 you had to use two named pipes to achieve this.

提交回复
热议问题