问题
Just for fun I want to pipe all output text in a terminal to espeak
. For example, after this is set up I should be able to type echo hi
and hear "hi" spoken, or ls
and hear my directory contents listed.
The only promising method to capture output I've found so far is from here: http://www.linuxjournal.com/content/bash-redirections-using-exec
This is what I have so far:
npipe=/tmp/$$.tmp
mknod $npipe p
tee /dev/tty <$npipe | espeak &
espeakpid=$!
exec 1>&-
exec 1>$npipe
trap "rm -f $npipe; kill $espeakpid" EXIT
It works (also printing a bunch of "Done" jobs), but creating the named pipe, removing with trap and printing the output with tee
all just seems a bit messy. Is there a simpler way?
回答1:
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
becauseespeak
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 awhile 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.
回答2:
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...
来源:https://stackoverflow.com/questions/24843014/hook-all-command-output-within-bash