Creating a temporary file from standard input

The name of the pictureThe name of the pictureThe name of the pictureClash Royale CLAN TAG#URR8PPP












0















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.










share|improve this question
























  • The answers you have provide all the pieces to come up with a function: function magic () tmpfile=$(mktemp); cat - >"$tmpfile"; printf '%sn' "$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...

    – fra-san
    Jan 18 at 17:15











  • @fra-san Yeah, it’s a function not a pipeline of bash or zsh commands. : ( I’ve added further clarification. I was very precise in what I’ve written, but it obviously hasn’t been clear at all that I really am only interested in that and nothing else.

    – k.stm
    Jan 18 at 21:38











  • With your edits you add more and more restrictions to rule out all proposed solutions but you don't explain the reason for your strange requirements.

    – Bodo
    Jan 21 at 15:27











  • @Bodo I don’t add restrictions. I can leave out all the edits and the question would still be the same. The edits are merely clarifications because people kept interpreting my question loosely as “I want to do roughly this.”, whereas I meant “I want to do exactly this”. I’ll add a small background for an explanation for these requirements.

    – k.stm
    Jan 22 at 13:50











  • And what's the reason for the restriction "nothing self-written"? It would be simple to create a shell script that writes the data from stdin to a temporary file, runs imageviewer (or a command specified as cmdline arg) and removes the temporary file when imageviewer has terminated.

    – Bodo
    Jan 22 at 14:06















0















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.










share|improve this question
























  • The answers you have provide all the pieces to come up with a function: function magic () tmpfile=$(mktemp); cat - >"$tmpfile"; printf '%sn' "$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...

    – fra-san
    Jan 18 at 17:15











  • @fra-san Yeah, it’s a function not a pipeline of bash or zsh commands. : ( I’ve added further clarification. I was very precise in what I’ve written, but it obviously hasn’t been clear at all that I really am only interested in that and nothing else.

    – k.stm
    Jan 18 at 21:38











  • With your edits you add more and more restrictions to rule out all proposed solutions but you don't explain the reason for your strange requirements.

    – Bodo
    Jan 21 at 15:27











  • @Bodo I don’t add restrictions. I can leave out all the edits and the question would still be the same. The edits are merely clarifications because people kept interpreting my question loosely as “I want to do roughly this.”, whereas I meant “I want to do exactly this”. I’ll add a small background for an explanation for these requirements.

    – k.stm
    Jan 22 at 13:50











  • And what's the reason for the restriction "nothing self-written"? It would be simple to create a shell script that writes the data from stdin to a temporary file, runs imageviewer (or a command specified as cmdline arg) and removes the temporary file when imageviewer has terminated.

    – Bodo
    Jan 22 at 14:06













0












0








0








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.










share|improve this question
















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.







bash shell zsh pipe






share|improve this question















share|improve this question













share|improve this question




share|improve this question








edited Jan 22 at 14:13







k.stm

















asked Jan 18 at 10:41









k.stmk.stm

273417




273417












  • The answers you have provide all the pieces to come up with a function: function magic () tmpfile=$(mktemp); cat - >"$tmpfile"; printf '%sn' "$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...

    – fra-san
    Jan 18 at 17:15











  • @fra-san Yeah, it’s a function not a pipeline of bash or zsh commands. : ( I’ve added further clarification. I was very precise in what I’ve written, but it obviously hasn’t been clear at all that I really am only interested in that and nothing else.

    – k.stm
    Jan 18 at 21:38











  • With your edits you add more and more restrictions to rule out all proposed solutions but you don't explain the reason for your strange requirements.

    – Bodo
    Jan 21 at 15:27











  • @Bodo I don’t add restrictions. I can leave out all the edits and the question would still be the same. The edits are merely clarifications because people kept interpreting my question loosely as “I want to do roughly this.”, whereas I meant “I want to do exactly this”. I’ll add a small background for an explanation for these requirements.

    – k.stm
    Jan 22 at 13:50











  • And what's the reason for the restriction "nothing self-written"? It would be simple to create a shell script that writes the data from stdin to a temporary file, runs imageviewer (or a command specified as cmdline arg) and removes the temporary file when imageviewer has terminated.

    – Bodo
    Jan 22 at 14:06

















  • The answers you have provide all the pieces to come up with a function: function magic () tmpfile=$(mktemp); cat - >"$tmpfile"; printf '%sn' "$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...

    – fra-san
    Jan 18 at 17:15











  • @fra-san Yeah, it’s a function not a pipeline of bash or zsh commands. : ( I’ve added further clarification. I was very precise in what I’ve written, but it obviously hasn’t been clear at all that I really am only interested in that and nothing else.

    – k.stm
    Jan 18 at 21:38











  • With your edits you add more and more restrictions to rule out all proposed solutions but you don't explain the reason for your strange requirements.

    – Bodo
    Jan 21 at 15:27











  • @Bodo I don’t add restrictions. I can leave out all the edits and the question would still be the same. The edits are merely clarifications because people kept interpreting my question loosely as “I want to do roughly this.”, whereas I meant “I want to do exactly this”. I’ll add a small background for an explanation for these requirements.

    – k.stm
    Jan 22 at 13:50











  • And what's the reason for the restriction "nothing self-written"? It would be simple to create a shell script that writes the data from stdin to a temporary file, runs imageviewer (or a command specified as cmdline arg) and removes the temporary file when imageviewer has terminated.

    – Bodo
    Jan 22 at 14:06
















The answers you have provide all the pieces to come up with a function: function magic () tmpfile=$(mktemp); cat - >"$tmpfile"; printf '%sn' "$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...

– fra-san
Jan 18 at 17:15





The answers you have provide all the pieces to come up with a function: function magic () tmpfile=$(mktemp); cat - >"$tmpfile"; printf '%sn' "$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...

– fra-san
Jan 18 at 17:15













@fra-san Yeah, it’s a function not a pipeline of bash or zsh commands. : ( I’ve added further clarification. I was very precise in what I’ve written, but it obviously hasn’t been clear at all that I really am only interested in that and nothing else.

– k.stm
Jan 18 at 21:38





@fra-san Yeah, it’s a function not a pipeline of bash or zsh commands. : ( I’ve added further clarification. I was very precise in what I’ve written, but it obviously hasn’t been clear at all that I really am only interested in that and nothing else.

– k.stm
Jan 18 at 21:38













With your edits you add more and more restrictions to rule out all proposed solutions but you don't explain the reason for your strange requirements.

– Bodo
Jan 21 at 15:27





With your edits you add more and more restrictions to rule out all proposed solutions but you don't explain the reason for your strange requirements.

– Bodo
Jan 21 at 15:27













@Bodo I don’t add restrictions. I can leave out all the edits and the question would still be the same. The edits are merely clarifications because people kept interpreting my question loosely as “I want to do roughly this.”, whereas I meant “I want to do exactly this”. I’ll add a small background for an explanation for these requirements.

– k.stm
Jan 22 at 13:50





@Bodo I don’t add restrictions. I can leave out all the edits and the question would still be the same. The edits are merely clarifications because people kept interpreting my question loosely as “I want to do roughly this.”, whereas I meant “I want to do exactly this”. I’ll add a small background for an explanation for these requirements.

– k.stm
Jan 22 at 13:50













And what's the reason for the restriction "nothing self-written"? It would be simple to create a shell script that writes the data from stdin to a temporary file, runs imageviewer (or a command specified as cmdline arg) and removes the temporary file when imageviewer has terminated.

– Bodo
Jan 22 at 14:06





And what's the reason for the restriction "nothing self-written"? It would be simple to create a shell script that writes the data from stdin to a temporary file, runs imageviewer (or a command specified as cmdline arg) and removes the temporary file when imageviewer has terminated.

– Bodo
Jan 22 at 14:06










3 Answers
3






active

oldest

votes


















1














To meet the exact requirements of your question, you can do:



cat mouse.jpg | tf=$(mktemp); cat >"$tf"; echo "$tf"; | xargs imageviewer





share|improve this answer























  • Voilà. Many thanks.

    – k.stm
    Jan 26 at 8:28


















5














In bash at least, with the use of a process substitution:



utility <( some_other_utility )


This would present utility with a file name. When utility opens the file and reads from it, it will read the standard output of some_other_utility.




Your example:



cat mouse.jpg | submagic | xargs imageviewer


I'm assuming that this is meant to send the file name mouse.jpg to imageviewer, as it doesn't make sense to send the contents of a JPEG file through xargs.



That could be done through just



echo 'mouse.jpg' | xargs imageviewer


or



xargs imageviewer <<<'mouse.jpg'


in bash.



Sending the contents of mouse.jpg to imageviewer can be done with



cat mouse.jpg | imageviewer /dev/stdin


or, using the process substitution, with



imageviewer <( cat mouse.jpg )


Using a temporary file:



tmpfile=$(mktemp)
cat mouse.jpg >"$tmpfile"

imageviewer "$tmpfile"

rm "$tmpfile"


Possibly,



tmpfile=$(mktemp)
cat mouse.jpg | cat >"$tmpfile"; echo "$tmpfile"; | xargs imageviewer
rm "$tmpfile"


This pipeline relies on the fact that xargs would wait with executing imageviewer until it had read the filename from the middle part of the pipeline. The filename would not be outputted by the middle command until the temporary file had been created.






share|improve this answer

























  • There might be an issue if imageviewer wants to seek the file

    – Torin
    Jan 18 at 10:48












  • Thanks – that doesn’t answer the question, though. That I start with a standard input stream of some process is a restriction. In your answer, the output from some_other_utility isn’t handled as a standard input stream. I specifically need some command that I can put in lieu of magic or submagic. Maybe think of the question as: “Write an alias alias magic=… such that the above works”.

    – k.stm
    Jan 18 at 10:49












  • @k.stm Your example does not make much sense to me. Could you clarify please?

    – Kusalananda
    Jan 18 at 10:50











  • @Torin This is a restriction set out by the question. If the data is a stream, then it's likely not seekable to start with.

    – Kusalananda
    Jan 18 at 10:52






  • 1





    @k.stm Since all parts of a pipeline runs concurrently, you submagic command would possibly start after imageviewer, which means the file that it created would not be available for imageviewer. This is regardless of whether it's implemented in the shell or in C. You would have to first create the file, then invoke imageviewer.

    – Kusalananda
    Jan 18 at 10:57


















3














After a clarification of submagic in the question I edited the answer.



In zsh you can create a temporary file from the output of a command with =( ... ). In contrast to <( ... ) which may use a device file like /dev/fd... or a named pipe the form =( ... ) creates a seekable temporary file. See http://zsh.sourceforge.net/Doc/Release/Expansion.html#Process-Substitution



Assuming that cat mouse.jpg is only an example for any command that sends image data to stdout, you can use



imageviewer =(cat mouse.jpg)


If you need the temporary file created by zsh to have a particular extension, you can set the TMPSUFFIX special variable):



(TMPSUFFIX=.jpg; imageviewer =(cat mouse.jpg))


second update



If you insist on the syntax



cat mouse.jpg | submagic | xargs imageviewer


it would be possible to create a script submagic similar to the commands already proposed in Kusalananda's answer.



#! /bin/sh

# A fixed name allows multiple runs without creating lots of files and
# allows a simple script to remove the file afterwards.
TMPFILE=/tmp/submagic.tmp.jpg
rm -f "$TMPFILE" && cat > "$TMPFILE" && echo "$TMPFILE"


In contrast to shell mechanisms like =( cat mouse.jpg ) this would not remove $TMPFILE after running imageviewer.



If you can run a second script after running imageviewer this could remove the file if it knows the name.



Script submagic-cleanup



#! /bin/sh

# A fixed name allows a simple script for cleaning up.
TMPFILE=/tmp/submagic.tmp.jpg
rm -f "$TMPFILE"


Then you could run



cat mouse.jpg | submagic | xargs imageviewer
submagic-cleanup





share|improve this answer

























  • Thanks, that still doesn’t answer my question. I need an alias for submagic such that exactly cat mouse.jpg | submagic | xargs imageviewer works.

    – k.stm
    Jan 18 at 15:28











  • @k.stm Why do you have this requirement? This looks like hou have a specific (difficult) solution in mind while there might be other solutions for your real problem.

    – Bodo
    Jan 21 at 12:24











Your Answer








StackExchange.ready(function()
var channelOptions =
tags: "".split(" "),
id: "106"
;
initTagRenderer("".split(" "), "".split(" "), channelOptions);

StackExchange.using("externalEditor", function()
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled)
StackExchange.using("snippets", function()
createEditor();
);

else
createEditor();

);

function createEditor()
StackExchange.prepareEditor(
heartbeatType: 'answer',
autoActivateHeartbeat: false,
convertImagesToLinks: false,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: null,
bindNavPrevention: true,
postfix: "",
imageUploader:
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
,
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
);



);













draft saved

draft discarded


















StackExchange.ready(
function ()
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2funix.stackexchange.com%2fquestions%2f495254%2fcreating-a-temporary-file-from-standard-input%23new-answer', 'question_page');

);

Post as a guest















Required, but never shown

























3 Answers
3






active

oldest

votes








3 Answers
3






active

oldest

votes









active

oldest

votes






active

oldest

votes









1














To meet the exact requirements of your question, you can do:



cat mouse.jpg | tf=$(mktemp); cat >"$tf"; echo "$tf"; | xargs imageviewer





share|improve this answer























  • Voilà. Many thanks.

    – k.stm
    Jan 26 at 8:28















1














To meet the exact requirements of your question, you can do:



cat mouse.jpg | tf=$(mktemp); cat >"$tf"; echo "$tf"; | xargs imageviewer





share|improve this answer























  • Voilà. Many thanks.

    – k.stm
    Jan 26 at 8:28













1












1








1







To meet the exact requirements of your question, you can do:



cat mouse.jpg | tf=$(mktemp); cat >"$tf"; echo "$tf"; | xargs imageviewer





share|improve this answer













To meet the exact requirements of your question, you can do:



cat mouse.jpg | tf=$(mktemp); cat >"$tf"; echo "$tf"; | xargs imageviewer






share|improve this answer












share|improve this answer



share|improve this answer










answered Jan 26 at 5:17









IsaacIsaac

11.8k11752




11.8k11752












  • Voilà. Many thanks.

    – k.stm
    Jan 26 at 8:28

















  • Voilà. Many thanks.

    – k.stm
    Jan 26 at 8:28
















Voilà. Many thanks.

– k.stm
Jan 26 at 8:28





Voilà. Many thanks.

– k.stm
Jan 26 at 8:28













5














In bash at least, with the use of a process substitution:



utility <( some_other_utility )


This would present utility with a file name. When utility opens the file and reads from it, it will read the standard output of some_other_utility.




Your example:



cat mouse.jpg | submagic | xargs imageviewer


I'm assuming that this is meant to send the file name mouse.jpg to imageviewer, as it doesn't make sense to send the contents of a JPEG file through xargs.



That could be done through just



echo 'mouse.jpg' | xargs imageviewer


or



xargs imageviewer <<<'mouse.jpg'


in bash.



Sending the contents of mouse.jpg to imageviewer can be done with



cat mouse.jpg | imageviewer /dev/stdin


or, using the process substitution, with



imageviewer <( cat mouse.jpg )


Using a temporary file:



tmpfile=$(mktemp)
cat mouse.jpg >"$tmpfile"

imageviewer "$tmpfile"

rm "$tmpfile"


Possibly,



tmpfile=$(mktemp)
cat mouse.jpg | cat >"$tmpfile"; echo "$tmpfile"; | xargs imageviewer
rm "$tmpfile"


This pipeline relies on the fact that xargs would wait with executing imageviewer until it had read the filename from the middle part of the pipeline. The filename would not be outputted by the middle command until the temporary file had been created.






share|improve this answer

























  • There might be an issue if imageviewer wants to seek the file

    – Torin
    Jan 18 at 10:48












  • Thanks – that doesn’t answer the question, though. That I start with a standard input stream of some process is a restriction. In your answer, the output from some_other_utility isn’t handled as a standard input stream. I specifically need some command that I can put in lieu of magic or submagic. Maybe think of the question as: “Write an alias alias magic=… such that the above works”.

    – k.stm
    Jan 18 at 10:49












  • @k.stm Your example does not make much sense to me. Could you clarify please?

    – Kusalananda
    Jan 18 at 10:50











  • @Torin This is a restriction set out by the question. If the data is a stream, then it's likely not seekable to start with.

    – Kusalananda
    Jan 18 at 10:52






  • 1





    @k.stm Since all parts of a pipeline runs concurrently, you submagic command would possibly start after imageviewer, which means the file that it created would not be available for imageviewer. This is regardless of whether it's implemented in the shell or in C. You would have to first create the file, then invoke imageviewer.

    – Kusalananda
    Jan 18 at 10:57















5














In bash at least, with the use of a process substitution:



utility <( some_other_utility )


This would present utility with a file name. When utility opens the file and reads from it, it will read the standard output of some_other_utility.




Your example:



cat mouse.jpg | submagic | xargs imageviewer


I'm assuming that this is meant to send the file name mouse.jpg to imageviewer, as it doesn't make sense to send the contents of a JPEG file through xargs.



That could be done through just



echo 'mouse.jpg' | xargs imageviewer


or



xargs imageviewer <<<'mouse.jpg'


in bash.



Sending the contents of mouse.jpg to imageviewer can be done with



cat mouse.jpg | imageviewer /dev/stdin


or, using the process substitution, with



imageviewer <( cat mouse.jpg )


Using a temporary file:



tmpfile=$(mktemp)
cat mouse.jpg >"$tmpfile"

imageviewer "$tmpfile"

rm "$tmpfile"


Possibly,



tmpfile=$(mktemp)
cat mouse.jpg | cat >"$tmpfile"; echo "$tmpfile"; | xargs imageviewer
rm "$tmpfile"


This pipeline relies on the fact that xargs would wait with executing imageviewer until it had read the filename from the middle part of the pipeline. The filename would not be outputted by the middle command until the temporary file had been created.






share|improve this answer

























  • There might be an issue if imageviewer wants to seek the file

    – Torin
    Jan 18 at 10:48












  • Thanks – that doesn’t answer the question, though. That I start with a standard input stream of some process is a restriction. In your answer, the output from some_other_utility isn’t handled as a standard input stream. I specifically need some command that I can put in lieu of magic or submagic. Maybe think of the question as: “Write an alias alias magic=… such that the above works”.

    – k.stm
    Jan 18 at 10:49












  • @k.stm Your example does not make much sense to me. Could you clarify please?

    – Kusalananda
    Jan 18 at 10:50











  • @Torin This is a restriction set out by the question. If the data is a stream, then it's likely not seekable to start with.

    – Kusalananda
    Jan 18 at 10:52






  • 1





    @k.stm Since all parts of a pipeline runs concurrently, you submagic command would possibly start after imageviewer, which means the file that it created would not be available for imageviewer. This is regardless of whether it's implemented in the shell or in C. You would have to first create the file, then invoke imageviewer.

    – Kusalananda
    Jan 18 at 10:57













5












5








5







In bash at least, with the use of a process substitution:



utility <( some_other_utility )


This would present utility with a file name. When utility opens the file and reads from it, it will read the standard output of some_other_utility.




Your example:



cat mouse.jpg | submagic | xargs imageviewer


I'm assuming that this is meant to send the file name mouse.jpg to imageviewer, as it doesn't make sense to send the contents of a JPEG file through xargs.



That could be done through just



echo 'mouse.jpg' | xargs imageviewer


or



xargs imageviewer <<<'mouse.jpg'


in bash.



Sending the contents of mouse.jpg to imageviewer can be done with



cat mouse.jpg | imageviewer /dev/stdin


or, using the process substitution, with



imageviewer <( cat mouse.jpg )


Using a temporary file:



tmpfile=$(mktemp)
cat mouse.jpg >"$tmpfile"

imageviewer "$tmpfile"

rm "$tmpfile"


Possibly,



tmpfile=$(mktemp)
cat mouse.jpg | cat >"$tmpfile"; echo "$tmpfile"; | xargs imageviewer
rm "$tmpfile"


This pipeline relies on the fact that xargs would wait with executing imageviewer until it had read the filename from the middle part of the pipeline. The filename would not be outputted by the middle command until the temporary file had been created.






share|improve this answer















In bash at least, with the use of a process substitution:



utility <( some_other_utility )


This would present utility with a file name. When utility opens the file and reads from it, it will read the standard output of some_other_utility.




Your example:



cat mouse.jpg | submagic | xargs imageviewer


I'm assuming that this is meant to send the file name mouse.jpg to imageviewer, as it doesn't make sense to send the contents of a JPEG file through xargs.



That could be done through just



echo 'mouse.jpg' | xargs imageviewer


or



xargs imageviewer <<<'mouse.jpg'


in bash.



Sending the contents of mouse.jpg to imageviewer can be done with



cat mouse.jpg | imageviewer /dev/stdin


or, using the process substitution, with



imageviewer <( cat mouse.jpg )


Using a temporary file:



tmpfile=$(mktemp)
cat mouse.jpg >"$tmpfile"

imageviewer "$tmpfile"

rm "$tmpfile"


Possibly,



tmpfile=$(mktemp)
cat mouse.jpg | cat >"$tmpfile"; echo "$tmpfile"; | xargs imageviewer
rm "$tmpfile"


This pipeline relies on the fact that xargs would wait with executing imageviewer until it had read the filename from the middle part of the pipeline. The filename would not be outputted by the middle command until the temporary file had been created.







share|improve this answer














share|improve this answer



share|improve this answer








edited Jan 18 at 11:02

























answered Jan 18 at 10:44









KusalanandaKusalananda

128k16241398




128k16241398












  • There might be an issue if imageviewer wants to seek the file

    – Torin
    Jan 18 at 10:48












  • Thanks – that doesn’t answer the question, though. That I start with a standard input stream of some process is a restriction. In your answer, the output from some_other_utility isn’t handled as a standard input stream. I specifically need some command that I can put in lieu of magic or submagic. Maybe think of the question as: “Write an alias alias magic=… such that the above works”.

    – k.stm
    Jan 18 at 10:49












  • @k.stm Your example does not make much sense to me. Could you clarify please?

    – Kusalananda
    Jan 18 at 10:50











  • @Torin This is a restriction set out by the question. If the data is a stream, then it's likely not seekable to start with.

    – Kusalananda
    Jan 18 at 10:52






  • 1





    @k.stm Since all parts of a pipeline runs concurrently, you submagic command would possibly start after imageviewer, which means the file that it created would not be available for imageviewer. This is regardless of whether it's implemented in the shell or in C. You would have to first create the file, then invoke imageviewer.

    – Kusalananda
    Jan 18 at 10:57

















  • There might be an issue if imageviewer wants to seek the file

    – Torin
    Jan 18 at 10:48












  • Thanks – that doesn’t answer the question, though. That I start with a standard input stream of some process is a restriction. In your answer, the output from some_other_utility isn’t handled as a standard input stream. I specifically need some command that I can put in lieu of magic or submagic. Maybe think of the question as: “Write an alias alias magic=… such that the above works”.

    – k.stm
    Jan 18 at 10:49












  • @k.stm Your example does not make much sense to me. Could you clarify please?

    – Kusalananda
    Jan 18 at 10:50











  • @Torin This is a restriction set out by the question. If the data is a stream, then it's likely not seekable to start with.

    – Kusalananda
    Jan 18 at 10:52






  • 1





    @k.stm Since all parts of a pipeline runs concurrently, you submagic command would possibly start after imageviewer, which means the file that it created would not be available for imageviewer. This is regardless of whether it's implemented in the shell or in C. You would have to first create the file, then invoke imageviewer.

    – Kusalananda
    Jan 18 at 10:57
















There might be an issue if imageviewer wants to seek the file

– Torin
Jan 18 at 10:48






There might be an issue if imageviewer wants to seek the file

– Torin
Jan 18 at 10:48














Thanks – that doesn’t answer the question, though. That I start with a standard input stream of some process is a restriction. In your answer, the output from some_other_utility isn’t handled as a standard input stream. I specifically need some command that I can put in lieu of magic or submagic. Maybe think of the question as: “Write an alias alias magic=… such that the above works”.

– k.stm
Jan 18 at 10:49






Thanks – that doesn’t answer the question, though. That I start with a standard input stream of some process is a restriction. In your answer, the output from some_other_utility isn’t handled as a standard input stream. I specifically need some command that I can put in lieu of magic or submagic. Maybe think of the question as: “Write an alias alias magic=… such that the above works”.

– k.stm
Jan 18 at 10:49














@k.stm Your example does not make much sense to me. Could you clarify please?

– Kusalananda
Jan 18 at 10:50





@k.stm Your example does not make much sense to me. Could you clarify please?

– Kusalananda
Jan 18 at 10:50













@Torin This is a restriction set out by the question. If the data is a stream, then it's likely not seekable to start with.

– Kusalananda
Jan 18 at 10:52





@Torin This is a restriction set out by the question. If the data is a stream, then it's likely not seekable to start with.

– Kusalananda
Jan 18 at 10:52




1




1





@k.stm Since all parts of a pipeline runs concurrently, you submagic command would possibly start after imageviewer, which means the file that it created would not be available for imageviewer. This is regardless of whether it's implemented in the shell or in C. You would have to first create the file, then invoke imageviewer.

– Kusalananda
Jan 18 at 10:57





@k.stm Since all parts of a pipeline runs concurrently, you submagic command would possibly start after imageviewer, which means the file that it created would not be available for imageviewer. This is regardless of whether it's implemented in the shell or in C. You would have to first create the file, then invoke imageviewer.

– Kusalananda
Jan 18 at 10:57











3














After a clarification of submagic in the question I edited the answer.



In zsh you can create a temporary file from the output of a command with =( ... ). In contrast to <( ... ) which may use a device file like /dev/fd... or a named pipe the form =( ... ) creates a seekable temporary file. See http://zsh.sourceforge.net/Doc/Release/Expansion.html#Process-Substitution



Assuming that cat mouse.jpg is only an example for any command that sends image data to stdout, you can use



imageviewer =(cat mouse.jpg)


If you need the temporary file created by zsh to have a particular extension, you can set the TMPSUFFIX special variable):



(TMPSUFFIX=.jpg; imageviewer =(cat mouse.jpg))


second update



If you insist on the syntax



cat mouse.jpg | submagic | xargs imageviewer


it would be possible to create a script submagic similar to the commands already proposed in Kusalananda's answer.



#! /bin/sh

# A fixed name allows multiple runs without creating lots of files and
# allows a simple script to remove the file afterwards.
TMPFILE=/tmp/submagic.tmp.jpg
rm -f "$TMPFILE" && cat > "$TMPFILE" && echo "$TMPFILE"


In contrast to shell mechanisms like =( cat mouse.jpg ) this would not remove $TMPFILE after running imageviewer.



If you can run a second script after running imageviewer this could remove the file if it knows the name.



Script submagic-cleanup



#! /bin/sh

# A fixed name allows a simple script for cleaning up.
TMPFILE=/tmp/submagic.tmp.jpg
rm -f "$TMPFILE"


Then you could run



cat mouse.jpg | submagic | xargs imageviewer
submagic-cleanup





share|improve this answer

























  • Thanks, that still doesn’t answer my question. I need an alias for submagic such that exactly cat mouse.jpg | submagic | xargs imageviewer works.

    – k.stm
    Jan 18 at 15:28











  • @k.stm Why do you have this requirement? This looks like hou have a specific (difficult) solution in mind while there might be other solutions for your real problem.

    – Bodo
    Jan 21 at 12:24
















3














After a clarification of submagic in the question I edited the answer.



In zsh you can create a temporary file from the output of a command with =( ... ). In contrast to <( ... ) which may use a device file like /dev/fd... or a named pipe the form =( ... ) creates a seekable temporary file. See http://zsh.sourceforge.net/Doc/Release/Expansion.html#Process-Substitution



Assuming that cat mouse.jpg is only an example for any command that sends image data to stdout, you can use



imageviewer =(cat mouse.jpg)


If you need the temporary file created by zsh to have a particular extension, you can set the TMPSUFFIX special variable):



(TMPSUFFIX=.jpg; imageviewer =(cat mouse.jpg))


second update



If you insist on the syntax



cat mouse.jpg | submagic | xargs imageviewer


it would be possible to create a script submagic similar to the commands already proposed in Kusalananda's answer.



#! /bin/sh

# A fixed name allows multiple runs without creating lots of files and
# allows a simple script to remove the file afterwards.
TMPFILE=/tmp/submagic.tmp.jpg
rm -f "$TMPFILE" && cat > "$TMPFILE" && echo "$TMPFILE"


In contrast to shell mechanisms like =( cat mouse.jpg ) this would not remove $TMPFILE after running imageviewer.



If you can run a second script after running imageviewer this could remove the file if it knows the name.



Script submagic-cleanup



#! /bin/sh

# A fixed name allows a simple script for cleaning up.
TMPFILE=/tmp/submagic.tmp.jpg
rm -f "$TMPFILE"


Then you could run



cat mouse.jpg | submagic | xargs imageviewer
submagic-cleanup





share|improve this answer

























  • Thanks, that still doesn’t answer my question. I need an alias for submagic such that exactly cat mouse.jpg | submagic | xargs imageviewer works.

    – k.stm
    Jan 18 at 15:28











  • @k.stm Why do you have this requirement? This looks like hou have a specific (difficult) solution in mind while there might be other solutions for your real problem.

    – Bodo
    Jan 21 at 12:24














3












3








3







After a clarification of submagic in the question I edited the answer.



In zsh you can create a temporary file from the output of a command with =( ... ). In contrast to <( ... ) which may use a device file like /dev/fd... or a named pipe the form =( ... ) creates a seekable temporary file. See http://zsh.sourceforge.net/Doc/Release/Expansion.html#Process-Substitution



Assuming that cat mouse.jpg is only an example for any command that sends image data to stdout, you can use



imageviewer =(cat mouse.jpg)


If you need the temporary file created by zsh to have a particular extension, you can set the TMPSUFFIX special variable):



(TMPSUFFIX=.jpg; imageviewer =(cat mouse.jpg))


second update



If you insist on the syntax



cat mouse.jpg | submagic | xargs imageviewer


it would be possible to create a script submagic similar to the commands already proposed in Kusalananda's answer.



#! /bin/sh

# A fixed name allows multiple runs without creating lots of files and
# allows a simple script to remove the file afterwards.
TMPFILE=/tmp/submagic.tmp.jpg
rm -f "$TMPFILE" && cat > "$TMPFILE" && echo "$TMPFILE"


In contrast to shell mechanisms like =( cat mouse.jpg ) this would not remove $TMPFILE after running imageviewer.



If you can run a second script after running imageviewer this could remove the file if it knows the name.



Script submagic-cleanup



#! /bin/sh

# A fixed name allows a simple script for cleaning up.
TMPFILE=/tmp/submagic.tmp.jpg
rm -f "$TMPFILE"


Then you could run



cat mouse.jpg | submagic | xargs imageviewer
submagic-cleanup





share|improve this answer















After a clarification of submagic in the question I edited the answer.



In zsh you can create a temporary file from the output of a command with =( ... ). In contrast to <( ... ) which may use a device file like /dev/fd... or a named pipe the form =( ... ) creates a seekable temporary file. See http://zsh.sourceforge.net/Doc/Release/Expansion.html#Process-Substitution



Assuming that cat mouse.jpg is only an example for any command that sends image data to stdout, you can use



imageviewer =(cat mouse.jpg)


If you need the temporary file created by zsh to have a particular extension, you can set the TMPSUFFIX special variable):



(TMPSUFFIX=.jpg; imageviewer =(cat mouse.jpg))


second update



If you insist on the syntax



cat mouse.jpg | submagic | xargs imageviewer


it would be possible to create a script submagic similar to the commands already proposed in Kusalananda's answer.



#! /bin/sh

# A fixed name allows multiple runs without creating lots of files and
# allows a simple script to remove the file afterwards.
TMPFILE=/tmp/submagic.tmp.jpg
rm -f "$TMPFILE" && cat > "$TMPFILE" && echo "$TMPFILE"


In contrast to shell mechanisms like =( cat mouse.jpg ) this would not remove $TMPFILE after running imageviewer.



If you can run a second script after running imageviewer this could remove the file if it knows the name.



Script submagic-cleanup



#! /bin/sh

# A fixed name allows a simple script for cleaning up.
TMPFILE=/tmp/submagic.tmp.jpg
rm -f "$TMPFILE"


Then you could run



cat mouse.jpg | submagic | xargs imageviewer
submagic-cleanup






share|improve this answer














share|improve this answer



share|improve this answer








edited Jan 21 at 12:20

























answered Jan 18 at 11:09









BodoBodo

75317




75317












  • Thanks, that still doesn’t answer my question. I need an alias for submagic such that exactly cat mouse.jpg | submagic | xargs imageviewer works.

    – k.stm
    Jan 18 at 15:28











  • @k.stm Why do you have this requirement? This looks like hou have a specific (difficult) solution in mind while there might be other solutions for your real problem.

    – Bodo
    Jan 21 at 12:24


















  • Thanks, that still doesn’t answer my question. I need an alias for submagic such that exactly cat mouse.jpg | submagic | xargs imageviewer works.

    – k.stm
    Jan 18 at 15:28











  • @k.stm Why do you have this requirement? This looks like hou have a specific (difficult) solution in mind while there might be other solutions for your real problem.

    – Bodo
    Jan 21 at 12:24

















Thanks, that still doesn’t answer my question. I need an alias for submagic such that exactly cat mouse.jpg | submagic | xargs imageviewer works.

– k.stm
Jan 18 at 15:28





Thanks, that still doesn’t answer my question. I need an alias for submagic such that exactly cat mouse.jpg | submagic | xargs imageviewer works.

– k.stm
Jan 18 at 15:28













@k.stm Why do you have this requirement? This looks like hou have a specific (difficult) solution in mind while there might be other solutions for your real problem.

– Bodo
Jan 21 at 12:24






@k.stm Why do you have this requirement? This looks like hou have a specific (difficult) solution in mind while there might be other solutions for your real problem.

– Bodo
Jan 21 at 12:24


















draft saved

draft discarded
















































Thanks for contributing an answer to Unix & Linux Stack Exchange!


  • Please be sure to answer the question. Provide details and share your research!

But avoid


  • Asking for help, clarification, or responding to other answers.

  • Making statements based on opinion; back them up with references or personal experience.

To learn more, see our tips on writing great answers.




draft saved


draft discarded














StackExchange.ready(
function ()
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2funix.stackexchange.com%2fquestions%2f495254%2fcreating-a-temporary-file-from-standard-input%23new-answer', 'question_page');

);

Post as a guest















Required, but never shown





















































Required, but never shown














Required, but never shown












Required, but never shown







Required, but never shown

































Required, but never shown














Required, but never shown












Required, but never shown







Required, but never shown






Popular posts from this blog

How to check contact read email or not when send email to Individual?

Bahrain

Postfix configuration issue with fips on centos 7; mailgun relay