jozxyqk
jozxyqk

Reputation: 17266

Hook all command output within bash

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?

Upvotes: 3

Views: 1007

Answers (2)

konsolebox
konsolebox

Reputation: 75478

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.

Upvotes: 3

jimm-cl
jimm-cl

Reputation: 5412

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...

Upvotes: 0

Related Questions