Concatenating Output Streams

Here's an update on the stuff I've been working on. Red-black tree is done, and works as far as I know. Buffer works well too. Chart of oweage script is up on the main page - it's a bit kludgey, but it had to get done. I'll be spending a bit of today getting the house funds in order before Peter leaves.

I also made a shell script helper in C called "piper". It reads from a fifo and echos what it reads. When it encounters the end of a stream, it reopens the fifo and repeats. The program terminates when it reads the termination string. It then removes the fifo and ends.

mkfifo f
./piper f "time to die"
echo -n "Oh isn't it swell" >f
echo -n " to join multiple outputs" >f
echo " to a single output?" >f
echo -n "time to die" >f


This simple example shows the basic usage... though not in a very useful case. If one were recursively grepping for dependencies of a tree of executables from the output of ldd and wanted to pipe the concatenated output to 'sort -u', this piper program might come in handy. Here is a bash version:

#!/bin/bash

f=$1
term=$2

while [ 1 ]
do
    text=`cat $f`
    if [ "$text" = "$term" ]
    then
        break
    else
        echo "$text"
    fi
done

rm $f


This version assures a new line after every closed fifo, which should be okay for a script helper. I couldn't figure out a way around it. Anyway, there you have it! ... the C version is much longer, but does run a bit faster.

EDIT: The correct way to accomplish what I was doing is to spawn a child process and pipe its output to 'sort -u'. But really, the tools are all there and are quite well constructed... in the end, nothing really fancy was necessary, just had to spend some time learning it.

find . -type f -executable -print0 | xargs -0 ldd \
| $SCRIPTDIR/lddget.awk | sort -u


Where lddget.awk is
#!/bin/awk -f

BEGIN { mem_regex="(0x.*)" }

{
    if ($2 == "=>")
    {
        if ($4 ~ mem_regex)
        {
            print $3
        }
    }
    else if ($2 ~ mem_regex)
    {
        print $1
    }
}


-- Alex

2009.08.19 - last edit >> Wed, 09 Dec 2009 23:09:59 -0500