How to reference stdin as an option in a program in a pipeline?












0















I have a program that outputs a file that I want to use as stdin for the next program. However, program2 has "-in" and "-out" options that want filenames. So if I'm piping into program2, how can I reference the stdin for the "-in" option?



program1 | program2 -in stdin -out filename









share|improve this question



























    0















    I have a program that outputs a file that I want to use as stdin for the next program. However, program2 has "-in" and "-out" options that want filenames. So if I'm piping into program2, how can I reference the stdin for the "-in" option?



    program1 | program2 -in stdin -out filename









    share|improve this question

























      0












      0








      0








      I have a program that outputs a file that I want to use as stdin for the next program. However, program2 has "-in" and "-out" options that want filenames. So if I'm piping into program2, how can I reference the stdin for the "-in" option?



      program1 | program2 -in stdin -out filename









      share|improve this question














      I have a program that outputs a file that I want to use as stdin for the next program. However, program2 has "-in" and "-out" options that want filenames. So if I'm piping into program2, how can I reference the stdin for the "-in" option?



      program1 | program2 -in stdin -out filename






      bash stdin






      share|improve this question













      share|improve this question











      share|improve this question




      share|improve this question










      asked Jan 7 at 19:01









      MichaelMichael

      1




      1






















          3 Answers
          3






          active

          oldest

          votes


















          3














          If program2 doesn't use its stdin by itself and -in is the only way to specify the input file, these are useful options:





          • /proc/self/fd/0 or /proc/fd/0 (if available; kernel feature, not required by POSIX)



            program1 | program2 -in /proc/self/fd/0 -out filename



          • Process substitution (supported in Bash and few other shells, not required by POSIX)



            program2 -in <(program1) -out filename



          • Named fifo (POSIX way)



            mkfifo foo
            program1 >foo & # in background
            program2 -in foo -out filename
            rm fifo



          Note that many programs that don't normally use their stdin, behave this way for a reason: they need the input file to be seekable. With <bar program2 you can make the file descriptor 0 of program2 point to the bar file which is seekable, but in general stdin is not seekable, so such programs don't bother using it. Each of the above methods may make your program fail if it expects the file to be seekable.






          share|improve this answer





















          • 1





            I'd recommend /dev/stdin instead of /proc/[self]/fd/0 -- it's not required by POSIX either, but it seems to be much more widely supported than /proc. See here and here.

            – Gordon Davisson
            Jan 7 at 22:42



















          1














          You can do something like



          program1 > stdin & program2 -in stdin -out filename


          This is not a pipe actually but will put all the stdout of program1 inside stdin file so then program2 can read it.



          Alternatively some programs use the shorthand - when they attempt to read/write from shell. So it may be like:



          program1 | program2 -in - -out filename





          share|improve this answer































            1














            The following syntax should work in shell by pointing input file to a special file /dev/stdin:



            program1 | program2 -in /dev/stdin -out filename





            share|improve this answer























              Your Answer








              StackExchange.ready(function() {
              var channelOptions = {
              tags: "".split(" "),
              id: "3"
              };
              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: true,
              noModals: true,
              showLowRepImageUploadWarning: true,
              reputationToPostImages: 10,
              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%2fsuperuser.com%2fquestions%2f1391610%2fhow-to-reference-stdin-as-an-option-in-a-program-in-a-pipeline%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









              3














              If program2 doesn't use its stdin by itself and -in is the only way to specify the input file, these are useful options:





              • /proc/self/fd/0 or /proc/fd/0 (if available; kernel feature, not required by POSIX)



                program1 | program2 -in /proc/self/fd/0 -out filename



              • Process substitution (supported in Bash and few other shells, not required by POSIX)



                program2 -in <(program1) -out filename



              • Named fifo (POSIX way)



                mkfifo foo
                program1 >foo & # in background
                program2 -in foo -out filename
                rm fifo



              Note that many programs that don't normally use their stdin, behave this way for a reason: they need the input file to be seekable. With <bar program2 you can make the file descriptor 0 of program2 point to the bar file which is seekable, but in general stdin is not seekable, so such programs don't bother using it. Each of the above methods may make your program fail if it expects the file to be seekable.






              share|improve this answer





















              • 1





                I'd recommend /dev/stdin instead of /proc/[self]/fd/0 -- it's not required by POSIX either, but it seems to be much more widely supported than /proc. See here and here.

                – Gordon Davisson
                Jan 7 at 22:42
















              3














              If program2 doesn't use its stdin by itself and -in is the only way to specify the input file, these are useful options:





              • /proc/self/fd/0 or /proc/fd/0 (if available; kernel feature, not required by POSIX)



                program1 | program2 -in /proc/self/fd/0 -out filename



              • Process substitution (supported in Bash and few other shells, not required by POSIX)



                program2 -in <(program1) -out filename



              • Named fifo (POSIX way)



                mkfifo foo
                program1 >foo & # in background
                program2 -in foo -out filename
                rm fifo



              Note that many programs that don't normally use their stdin, behave this way for a reason: they need the input file to be seekable. With <bar program2 you can make the file descriptor 0 of program2 point to the bar file which is seekable, but in general stdin is not seekable, so such programs don't bother using it. Each of the above methods may make your program fail if it expects the file to be seekable.






              share|improve this answer





















              • 1





                I'd recommend /dev/stdin instead of /proc/[self]/fd/0 -- it's not required by POSIX either, but it seems to be much more widely supported than /proc. See here and here.

                – Gordon Davisson
                Jan 7 at 22:42














              3












              3








              3







              If program2 doesn't use its stdin by itself and -in is the only way to specify the input file, these are useful options:





              • /proc/self/fd/0 or /proc/fd/0 (if available; kernel feature, not required by POSIX)



                program1 | program2 -in /proc/self/fd/0 -out filename



              • Process substitution (supported in Bash and few other shells, not required by POSIX)



                program2 -in <(program1) -out filename



              • Named fifo (POSIX way)



                mkfifo foo
                program1 >foo & # in background
                program2 -in foo -out filename
                rm fifo



              Note that many programs that don't normally use their stdin, behave this way for a reason: they need the input file to be seekable. With <bar program2 you can make the file descriptor 0 of program2 point to the bar file which is seekable, but in general stdin is not seekable, so such programs don't bother using it. Each of the above methods may make your program fail if it expects the file to be seekable.






              share|improve this answer















              If program2 doesn't use its stdin by itself and -in is the only way to specify the input file, these are useful options:





              • /proc/self/fd/0 or /proc/fd/0 (if available; kernel feature, not required by POSIX)



                program1 | program2 -in /proc/self/fd/0 -out filename



              • Process substitution (supported in Bash and few other shells, not required by POSIX)



                program2 -in <(program1) -out filename



              • Named fifo (POSIX way)



                mkfifo foo
                program1 >foo & # in background
                program2 -in foo -out filename
                rm fifo



              Note that many programs that don't normally use their stdin, behave this way for a reason: they need the input file to be seekable. With <bar program2 you can make the file descriptor 0 of program2 point to the bar file which is seekable, but in general stdin is not seekable, so such programs don't bother using it. Each of the above methods may make your program fail if it expects the file to be seekable.







              share|improve this answer














              share|improve this answer



              share|improve this answer








              edited Jan 7 at 20:34

























              answered Jan 7 at 20:27









              Kamil MaciorowskiKamil Maciorowski

              27.2k155982




              27.2k155982








              • 1





                I'd recommend /dev/stdin instead of /proc/[self]/fd/0 -- it's not required by POSIX either, but it seems to be much more widely supported than /proc. See here and here.

                – Gordon Davisson
                Jan 7 at 22:42














              • 1





                I'd recommend /dev/stdin instead of /proc/[self]/fd/0 -- it's not required by POSIX either, but it seems to be much more widely supported than /proc. See here and here.

                – Gordon Davisson
                Jan 7 at 22:42








              1




              1





              I'd recommend /dev/stdin instead of /proc/[self]/fd/0 -- it's not required by POSIX either, but it seems to be much more widely supported than /proc. See here and here.

              – Gordon Davisson
              Jan 7 at 22:42





              I'd recommend /dev/stdin instead of /proc/[self]/fd/0 -- it's not required by POSIX either, but it seems to be much more widely supported than /proc. See here and here.

              – Gordon Davisson
              Jan 7 at 22:42













              1














              You can do something like



              program1 > stdin & program2 -in stdin -out filename


              This is not a pipe actually but will put all the stdout of program1 inside stdin file so then program2 can read it.



              Alternatively some programs use the shorthand - when they attempt to read/write from shell. So it may be like:



              program1 | program2 -in - -out filename





              share|improve this answer




























                1














                You can do something like



                program1 > stdin & program2 -in stdin -out filename


                This is not a pipe actually but will put all the stdout of program1 inside stdin file so then program2 can read it.



                Alternatively some programs use the shorthand - when they attempt to read/write from shell. So it may be like:



                program1 | program2 -in - -out filename





                share|improve this answer


























                  1












                  1








                  1







                  You can do something like



                  program1 > stdin & program2 -in stdin -out filename


                  This is not a pipe actually but will put all the stdout of program1 inside stdin file so then program2 can read it.



                  Alternatively some programs use the shorthand - when they attempt to read/write from shell. So it may be like:



                  program1 | program2 -in - -out filename





                  share|improve this answer













                  You can do something like



                  program1 > stdin & program2 -in stdin -out filename


                  This is not a pipe actually but will put all the stdout of program1 inside stdin file so then program2 can read it.



                  Alternatively some programs use the shorthand - when they attempt to read/write from shell. So it may be like:



                  program1 | program2 -in - -out filename






                  share|improve this answer












                  share|improve this answer



                  share|improve this answer










                  answered Jan 7 at 20:04









                  user1330614user1330614

                  1334




                  1334























                      1














                      The following syntax should work in shell by pointing input file to a special file /dev/stdin:



                      program1 | program2 -in /dev/stdin -out filename





                      share|improve this answer




























                        1














                        The following syntax should work in shell by pointing input file to a special file /dev/stdin:



                        program1 | program2 -in /dev/stdin -out filename





                        share|improve this answer


























                          1












                          1








                          1







                          The following syntax should work in shell by pointing input file to a special file /dev/stdin:



                          program1 | program2 -in /dev/stdin -out filename





                          share|improve this answer













                          The following syntax should work in shell by pointing input file to a special file /dev/stdin:



                          program1 | program2 -in /dev/stdin -out filename






                          share|improve this answer












                          share|improve this answer



                          share|improve this answer










                          answered Jan 7 at 20:57









                          kenorbkenorb

                          11.1k1578115




                          11.1k1578115






























                              draft saved

                              draft discarded




















































                              Thanks for contributing an answer to Super User!


                              • 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%2fsuperuser.com%2fquestions%2f1391610%2fhow-to-reference-stdin-as-an-option-in-a-program-in-a-pipeline%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

                              "Incorrect syntax near the keyword 'ON'. (on update cascade, on delete cascade,)

                              Alcedinidae

                              RAC Tourist Trophy