Say I have a standard input stream of file contents and a command that expects a file name as an argument and I want to run that command on a file made up of the standard input stream’s file contents.
For instance, instead of the command
imageviewer mouse.jpg
I want some magical line magic
that makes this equivalent to
cat mouse.jpg | magic
What would this magic
look like in zsh
or bash
?
Preferably, I would like it to look like
cat mouse.jpg | submagic | xargs imageviewer
that is, I would like submagic
to create a temporary file from the standard input stream and output the created file’s file name.
The lines magic
or submagic
should be shell pipelines using only bash or zsh commands, their builtins, GNU coreutils and the likes.
This is the entire question. Below is merely clarification and background because people kept misinterpreting what I meant.
Okay, so I thought I made it clear, but it seems like I didn’t: I really want to have a pipeline doing the equivalent of
imageviewer mouse.jpg
that begins exactly with cat mouse.jpg | …
. That is, I really want exactly a line magic
or submagic
such that exactly the pipelines above work – I meant my question literally.
This means in particular that the following suggested solutions won’t do:
- beginning the pipeline with
tmpfile = $(mktemp); cat mouse.jpg | …
- rewriting it as
imageviewer =(cat mouse.jpg)
- rewriting it as
echo mouse.jpg | xargs imageviewer
- using a function or a binary to solve it, say by defining
function magic () { … }
It can be said that what I’m interested in is whether it’s possible to write an alias alias magic='…'
or alias submagic='…'
such that one of the above pipelines work and such that definition of these aliases contains nothing more than bash or zsh commands, GNU coreutils and the likes – nothing self-written.
Another comment. You make take imageviewer
to be feh
or sxiv
or something.
Background. This question came to me when I tried opening several image files attached to a mail using my mail client neomutt
. Neomutt offers to tag attached files and then to pipe the file contents to a command line you may type in. So here, I can only give a command line that performs something on a given standard input stream. That’s where the question came from. But I’m not interested in the original problem, but only in this very question.
function magic (){ tmpfile=$(mktemp); cat - >"$tmpfile"; printf '%s\n' "$tmpfile"; }
. This will work exactly the way you are asking for:cat mouse.jpg | magic | xargs gwenview
(gwenview
is the image viewer I have and which I'm testing with). If this falls short of your expectations then I fear you'll have to clarify even more...imageviewer
(or a command specified as cmdline arg) and removes the temporary file whenimageviewer
has terminated.