Zsh Mailing List Archive
Messages sorted by: Reverse Date, Date, Thread, Author

Re: reopening/resetting/tee'ing pipes?



On Aug 8,  9:49pm, Aaron Davies wrote:
}
} % ./bar.sh <(echo foo)
} 1
} foo
} 2
} 3
} 
} is it possible to get another "foo" between 2 & 3 there given that
} i'm reading from a pipe? (both portable and zsh-only solutions are
} welcome)

Pipes are can't be rewound/reopened (in parlance, are not "seekable")
so there is no solution that doesn't involve copying the data.  You
can do that in the calling zsh like so:

% ./bar.sh =(echo foo)

Or you can do it inside the script:

    #!/bin/zsh
    STDIN="$(cat $1)"  # Stash in memory
    echo 1
    cat <<<"$STDIN"
    echo 2
    cat <<<"$STDIN"
    echo 3

Or:

    #!/bin/zsh
    function do_bar {
	echo 1
	cat $1  # This is the function's $1, not the script's $1
	echo 2
	cat $1
	echo 3
    }
    do_bar =(cat $1)  # Stash in autoremoved temp file

There are various other tricks you can play to avoid the extra "cat"
but they all involve testing whether $1 is set.  The examples above
rely on cat reading from stdin when $1 expands to nothing.



Messages sorted by: Reverse Date, Date, Thread, Author