# sh & fifo related question



## graudeejs (Jun 17, 2009)

This is pure sh question.

Is it possible to start app and redirect redirect output on demand?

For example I want to start app, redirect output to /dev/null untill I need to read it.
Then redirect it to fifo, read it, and redirect it back to /dev/null until next time I want to read output.

The app must be running all the time?

An alternative:
Is it possible to redirect app output to fifo, and keep fifo clean untill i want to read it, while at the same time app keeps spamming to fifo?

Perhaps it's possible with some special file? (reading mknod(8))

I hope it makes sense. If anything of this is possible, I would do miracles.


----------



## blah (Jun 18, 2009)

killasmurf86 said:
			
		

> For example I want to start app, redirect output to /dev/null untill I need to read it.
> Then redirect it to fifo, read it, and redirect it back to /dev/null until next time I want to read output.


Just redirect output to named pipe and change app recieving on another end. Typical drainage of unneeded data can be [cmd=]cat my.pipe >/dev/null[/cmd]. When the need arises you just kill(1) that [cmd=]cat[/cmd] instance and redirect it to smth else, eg. file - [cmd=]cat my.pipe >my.file[/cmd].

Here is an example for mplayer. It starts mplayer in slave mode. While playing it throws away all the spam from different message modules. Then at certain point it retrieves some (meta)data to a file named `m.blah'
	
	



```
cleanup() rm -f $in $out $err

# usage: consume destination from1 from2 ... fromN
consume() {
    sleep 1 # no need to be rash
    dest=$1; shift

    kill $conp 2>&-
    cat $@ >$dest &
    conp=$!
}

conp= # PID of current consumer
in=/tmp/m.in   # pipe
out=/tmp/m.out # pipe
err=/tmp/m.err # regular file
testfile=/tmp/m.blah

#trap cleanup exit
cleanup; mkfifo $in $out

# drain output
consume /dev/null $out

# begin enslavement
2>$err                       \
>$out                         \
mplayer -idle                  \
        -slave                  \
        -msglevel statusline=-1  \
        -input file=$in &

# initial feed
>$in echo loadfile /d/muz/ly/openbsd.4.4.trial_of_the_bsd_knights.ogg

# substitute drain with file
consume $testfile $out

# feed smth else
>$in echo get_meta_title
>$in echo get_audio_codec

# change back to drain
consume /dev/null $out
```
`$ foo.sh`
`$ cat /tmp/m.blah
ANS_META_TITLE='Trial of the BSD Knights'
ANS_AUDIO_CODEC='ffvorbis'`

Perhaps, there is a better way.


----------

