As many of us are surely aware, it's always a good idea to make your program accept stdin input. Very many programs do allow that *nix environments. This lets us do cool things like piping
echo "foo" | less
. Quite often one can find that cat barfile | baz
is logically equivalent to baz barfile
as behind the scenes it's just reading of strings anyway.
Nowadays, there are a lot more programs that one can not pipe to by default. Some programs have a flag that still allows for the behaviour mentioned above, but many don't.
Now, my question is, does a temporary file pipe exist?
Now, with my literally non-existent ability to write Bash and about 5 minutes on Google, I came up with this
#!/bin/bash
if [ $# -ne 2 ]; then exit 1; fi
f=$(mktemp)
($1) > $f
if [ $? -eq 0 ]; then ($2 $f); else exit $?; fi
rm $f
Calling this fpipe, we can do things like fpipe 'wget -O- www.example.com' baz
where baz is a program that we can't pipe into but can do baz file
.
My question is how can we do better. I suspect that with more Bash knowledge, re-writing the above script to take any amount of arguments would be fairly trivial (so that we can do things like fpipe 'foo x' bar baz
where with piping we could do something like foo x | bar | baz
. Bah, we could probably mix the two and end up with things such as fpipe 'wget -O- www.example.org | rev' baz
.
Is there an existing construct that achieves this? I believe I saw a construct of sort foo x > bar < baz
or something along these lines. I would have thought that this is a fairly common issue but my searches aren't bringing anything up. This means that I'm either not searching hard enough or that I'm missing something fairly obvious.
If there isn't a Proper Way(TM) to achieve this, is it possible to define a convenience syntax? Say, foo x |> baz | rev
where |>
pretty much directly translates to my script.
PS: I'm aware that my /script/ is very fragile and naive (such as quitting on non 0); feel free to post a better one.