Write mplayer's output to fifo and read it

bash, pipe, shell

Solution

You could do unbuffered grep with:

$ mplayer ...  2>&1 | grep --line-buffered "ICY"

or better:

$ mplayer ...  2>&1 | sed -une 's/^.*ICY[^:]*: //p'

or even, why not (sed is very nice for grep and formatting), this will grep `ICY` lines and even split line containing `-` in a first field of 30 chars length separed by a `:` from a second field:

$ mplayer ...  2>&1 |
    sed -une "
        /ICY/{
            s/^.*ICY[^:]*:.*'\([^']*\)';/\1/;
            s/^\(.*\) - /\1                              - /;
            s/^\(.\{30\}\) *- /\1: /;
            p;
    }"

could give something like:

Artist name                  : Song title
Other artist                 : Other song
Unsplited line
Artist                       : Title

Problem

I'm trying write simple notify app in bash. I want to read output from mplayer, parse it and display through notify-send. I can get desired info from mplayer using this: ``` mplayer <url> | grep ICY ``` and then parse in using sed. I create named pipe, tell mplayer to write it and then I'm reading from it. Unfortunately, it doesn't work. Here's my script: ``` $fifo=~/.rp/fifo mkfifo $fifo mplayer <url> 2>/dev/null | grep ICY 1> $fifo & while read line < $fifo; do echo $line done wait ``` Program keeps waiting to input from $fifo. I tried following in other terminal, while this script is running: Run ``` echo "Test" > .rp/fifo ``` Terminal with running script shows "Test" Run ``` echo "ICY" | grep ICY > .rp/fifo ``` also works. Run ``` mplayer <url> | grep ICY > .rp/fifo ``` and it doesn't work. Is I said above, the combination of mplayer | grep works fine. grep > $fifo works fine. I don't understand why mplayer | grep > $fifo doesn't work.

Original source