Hook all command output within bash

主宰稳场 提交于 2019-12-06 04:41:09

This is one way:

exec > >(exec tee >(exec xargs -n 1 -d '\n' espeak -- &>/dev/null))

If you want to be able to restore back to original output stream:

exec 3>&1  ## Store original stdout to fd 3.
exec 4> >(exec tee >(exec xargs -n 1 -d '\n' espeak -- &>/dev/null))  ## Open "espeak" as fd 4.
exec >&4  ## Redirect stdout to "espeak".
exec >&3  ## Redirect back to normal.
  • I use xargs -n 1 because espeak doesn't do anything until EOF is reached so we summon an instance of it per line. This can be customized of course, but there's your answer. And of course a while read loop can also be an option for this.
  • I also use exec's in process substitution to make sure we get rid of unnecessary subshells.

Seems like it's way easier than that - I just tested this and it works:

$ echo "these are not the droids you are looking for" | espeak --stdin 

The --stdin flag is the key. From espeak's man page:

   --stdin
          Read text input from stdin instead of a file

And if what you want to hear is a very long output, I guess you can use xargs if you run into Argument list too long errors...

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!