Pipe executed command into the same output file of the command












3














I've had a look at various tips (ss64.com Command Redirection) and tricks but couldn't find an answer to my question:



Question



Is it possible to pipe the command I am going to execute into the same redirected output I will be creating?



Example with netstat



Input command



C:Usersmemyselfandi> netstat -obna >C:tempnetstat_with_programs.txt


The actual command would be: netstat -obna >C:tempnetstat_with_programs.txt



Output (The text file netstat_with_programs.txt)



This is the actual content of the netstat_with_programs.txt file. (The command is basically documenting itself in the output file.)



netstat -obna >C:tempnetstat_with_programs.txt
Active connections

Proto Local Address Remoteaddress State PID
TCP 0.0.0.0:135 0.0.0.0:0 LISTENING 888
RpcSs
[svchost.exe]
TCP 0.0.0.0:2382 0.0.0.0:0 LISTENING 1396
[sqlbrowser.exe]
TCP 0.0.0.0:3389 0.0.0.0:0 LISTENING 376
TermService
[svchost.exe]
....


Example with arp



Input command



C:usersmemyselfandi> arp -a >C:temparp_output.txt


The actual command is: arp -a >C:temparp_output.txt



Output (The contents of arp_output.txt)



This is the actual content of the arp_output.txt file. (The command is basically documenting itself in the output file.)



arp -a >C:temparp_output.txt
Interface: 10.57.209.191 --- 0x5
Internet Address Physical Address Type
10.57.209.2 80-e0-1d-58-8a-50 dynamic
10.57.209.3 80-e0-1d-58-8b-88 dynamic
10.57.209.9 00-50-56-8d-91-fe dynamic
10.57.209.10 00-50-56-8d-91-fe dynamic
10.57.209.175 00-50-56-b5-44-16 dynamic
10.57.209.255 ff-ff-ff-ff-ff-ff static
224.0.0.22 01-00-5e-00-00-16 static
224.0.0.252 01-00-5e-00-00-fc static
230.0.0.1 01-00-5e-00-00-01 static
239.255.255.250 01-00-5e-7f-ff-fa static


So basically I would be documenting the command I am executing in the output I am creating.





Based on the possible solutions provided by @barlop in the comments, I went ahead and executed both commands:



With ECHO



echo netstat -obna >C:tempnetstat_with_programs.txt && netstat -obna >>C:tempnetstat_with_programs.txt


...this produced the following first line in the output file, which doesn't fully satisfy the requirements:



netstat -obna  
....


With %aaa% variable



set aaa=netstat -obna
echo (%aaa%>C:tempnetstat_with_programs.txt) && (echo %aaa%|cmd)>>C:tempnetstat_with_programs.txt


...this produces the same output, which doesn't fully meet the requirements:



netstat -obna  
...









share|improve this question
























  • Your English is completely ambiguous and thus not clear what you are asking. You should provide examples showing exactly what you mean. Show the contents of C:tempnetstat_with_programs.txt before and after. Demonstrating exactly what you mean rather than trying to use your highly ambigious words.
    – barlop
    Dec 4 at 15:13












  • Also your title gives no indication of exactly what you are talking about
    – barlop
    Dec 4 at 15:16










  • I'd be interested to know which part of my question is ambiguous? The input is clearly stated in my question. netstat -obna >C:tempnetstat_with_programs.txt. The output of my command is a text file which contains the contents outlined in the Output section of my question, which includes the command I executed. I don't think that my question is either ambiguous, nor syntactically, orthographically or typographically incorrect. I would appreciate if you could exactly explain which part of the question you are unable to grasp.
    – hot2use
    Dec 4 at 15:22










  • Because it is a > the file is previously non-existent. I am not adding (>>) to an existing file.
    – hot2use
    Dec 4 at 15:26










  • You have now corrected the unclear part of your question
    – barlop
    Dec 4 at 15:36
















3














I've had a look at various tips (ss64.com Command Redirection) and tricks but couldn't find an answer to my question:



Question



Is it possible to pipe the command I am going to execute into the same redirected output I will be creating?



Example with netstat



Input command



C:Usersmemyselfandi> netstat -obna >C:tempnetstat_with_programs.txt


The actual command would be: netstat -obna >C:tempnetstat_with_programs.txt



Output (The text file netstat_with_programs.txt)



This is the actual content of the netstat_with_programs.txt file. (The command is basically documenting itself in the output file.)



netstat -obna >C:tempnetstat_with_programs.txt
Active connections

Proto Local Address Remoteaddress State PID
TCP 0.0.0.0:135 0.0.0.0:0 LISTENING 888
RpcSs
[svchost.exe]
TCP 0.0.0.0:2382 0.0.0.0:0 LISTENING 1396
[sqlbrowser.exe]
TCP 0.0.0.0:3389 0.0.0.0:0 LISTENING 376
TermService
[svchost.exe]
....


Example with arp



Input command



C:usersmemyselfandi> arp -a >C:temparp_output.txt


The actual command is: arp -a >C:temparp_output.txt



Output (The contents of arp_output.txt)



This is the actual content of the arp_output.txt file. (The command is basically documenting itself in the output file.)



arp -a >C:temparp_output.txt
Interface: 10.57.209.191 --- 0x5
Internet Address Physical Address Type
10.57.209.2 80-e0-1d-58-8a-50 dynamic
10.57.209.3 80-e0-1d-58-8b-88 dynamic
10.57.209.9 00-50-56-8d-91-fe dynamic
10.57.209.10 00-50-56-8d-91-fe dynamic
10.57.209.175 00-50-56-b5-44-16 dynamic
10.57.209.255 ff-ff-ff-ff-ff-ff static
224.0.0.22 01-00-5e-00-00-16 static
224.0.0.252 01-00-5e-00-00-fc static
230.0.0.1 01-00-5e-00-00-01 static
239.255.255.250 01-00-5e-7f-ff-fa static


So basically I would be documenting the command I am executing in the output I am creating.





Based on the possible solutions provided by @barlop in the comments, I went ahead and executed both commands:



With ECHO



echo netstat -obna >C:tempnetstat_with_programs.txt && netstat -obna >>C:tempnetstat_with_programs.txt


...this produced the following first line in the output file, which doesn't fully satisfy the requirements:



netstat -obna  
....


With %aaa% variable



set aaa=netstat -obna
echo (%aaa%>C:tempnetstat_with_programs.txt) && (echo %aaa%|cmd)>>C:tempnetstat_with_programs.txt


...this produces the same output, which doesn't fully meet the requirements:



netstat -obna  
...









share|improve this question
























  • Your English is completely ambiguous and thus not clear what you are asking. You should provide examples showing exactly what you mean. Show the contents of C:tempnetstat_with_programs.txt before and after. Demonstrating exactly what you mean rather than trying to use your highly ambigious words.
    – barlop
    Dec 4 at 15:13












  • Also your title gives no indication of exactly what you are talking about
    – barlop
    Dec 4 at 15:16










  • I'd be interested to know which part of my question is ambiguous? The input is clearly stated in my question. netstat -obna >C:tempnetstat_with_programs.txt. The output of my command is a text file which contains the contents outlined in the Output section of my question, which includes the command I executed. I don't think that my question is either ambiguous, nor syntactically, orthographically or typographically incorrect. I would appreciate if you could exactly explain which part of the question you are unable to grasp.
    – hot2use
    Dec 4 at 15:22










  • Because it is a > the file is previously non-existent. I am not adding (>>) to an existing file.
    – hot2use
    Dec 4 at 15:26










  • You have now corrected the unclear part of your question
    – barlop
    Dec 4 at 15:36














3












3








3







I've had a look at various tips (ss64.com Command Redirection) and tricks but couldn't find an answer to my question:



Question



Is it possible to pipe the command I am going to execute into the same redirected output I will be creating?



Example with netstat



Input command



C:Usersmemyselfandi> netstat -obna >C:tempnetstat_with_programs.txt


The actual command would be: netstat -obna >C:tempnetstat_with_programs.txt



Output (The text file netstat_with_programs.txt)



This is the actual content of the netstat_with_programs.txt file. (The command is basically documenting itself in the output file.)



netstat -obna >C:tempnetstat_with_programs.txt
Active connections

Proto Local Address Remoteaddress State PID
TCP 0.0.0.0:135 0.0.0.0:0 LISTENING 888
RpcSs
[svchost.exe]
TCP 0.0.0.0:2382 0.0.0.0:0 LISTENING 1396
[sqlbrowser.exe]
TCP 0.0.0.0:3389 0.0.0.0:0 LISTENING 376
TermService
[svchost.exe]
....


Example with arp



Input command



C:usersmemyselfandi> arp -a >C:temparp_output.txt


The actual command is: arp -a >C:temparp_output.txt



Output (The contents of arp_output.txt)



This is the actual content of the arp_output.txt file. (The command is basically documenting itself in the output file.)



arp -a >C:temparp_output.txt
Interface: 10.57.209.191 --- 0x5
Internet Address Physical Address Type
10.57.209.2 80-e0-1d-58-8a-50 dynamic
10.57.209.3 80-e0-1d-58-8b-88 dynamic
10.57.209.9 00-50-56-8d-91-fe dynamic
10.57.209.10 00-50-56-8d-91-fe dynamic
10.57.209.175 00-50-56-b5-44-16 dynamic
10.57.209.255 ff-ff-ff-ff-ff-ff static
224.0.0.22 01-00-5e-00-00-16 static
224.0.0.252 01-00-5e-00-00-fc static
230.0.0.1 01-00-5e-00-00-01 static
239.255.255.250 01-00-5e-7f-ff-fa static


So basically I would be documenting the command I am executing in the output I am creating.





Based on the possible solutions provided by @barlop in the comments, I went ahead and executed both commands:



With ECHO



echo netstat -obna >C:tempnetstat_with_programs.txt && netstat -obna >>C:tempnetstat_with_programs.txt


...this produced the following first line in the output file, which doesn't fully satisfy the requirements:



netstat -obna  
....


With %aaa% variable



set aaa=netstat -obna
echo (%aaa%>C:tempnetstat_with_programs.txt) && (echo %aaa%|cmd)>>C:tempnetstat_with_programs.txt


...this produces the same output, which doesn't fully meet the requirements:



netstat -obna  
...









share|improve this question















I've had a look at various tips (ss64.com Command Redirection) and tricks but couldn't find an answer to my question:



Question



Is it possible to pipe the command I am going to execute into the same redirected output I will be creating?



Example with netstat



Input command



C:Usersmemyselfandi> netstat -obna >C:tempnetstat_with_programs.txt


The actual command would be: netstat -obna >C:tempnetstat_with_programs.txt



Output (The text file netstat_with_programs.txt)



This is the actual content of the netstat_with_programs.txt file. (The command is basically documenting itself in the output file.)



netstat -obna >C:tempnetstat_with_programs.txt
Active connections

Proto Local Address Remoteaddress State PID
TCP 0.0.0.0:135 0.0.0.0:0 LISTENING 888
RpcSs
[svchost.exe]
TCP 0.0.0.0:2382 0.0.0.0:0 LISTENING 1396
[sqlbrowser.exe]
TCP 0.0.0.0:3389 0.0.0.0:0 LISTENING 376
TermService
[svchost.exe]
....


Example with arp



Input command



C:usersmemyselfandi> arp -a >C:temparp_output.txt


The actual command is: arp -a >C:temparp_output.txt



Output (The contents of arp_output.txt)



This is the actual content of the arp_output.txt file. (The command is basically documenting itself in the output file.)



arp -a >C:temparp_output.txt
Interface: 10.57.209.191 --- 0x5
Internet Address Physical Address Type
10.57.209.2 80-e0-1d-58-8a-50 dynamic
10.57.209.3 80-e0-1d-58-8b-88 dynamic
10.57.209.9 00-50-56-8d-91-fe dynamic
10.57.209.10 00-50-56-8d-91-fe dynamic
10.57.209.175 00-50-56-b5-44-16 dynamic
10.57.209.255 ff-ff-ff-ff-ff-ff static
224.0.0.22 01-00-5e-00-00-16 static
224.0.0.252 01-00-5e-00-00-fc static
230.0.0.1 01-00-5e-00-00-01 static
239.255.255.250 01-00-5e-7f-ff-fa static


So basically I would be documenting the command I am executing in the output I am creating.





Based on the possible solutions provided by @barlop in the comments, I went ahead and executed both commands:



With ECHO



echo netstat -obna >C:tempnetstat_with_programs.txt && netstat -obna >>C:tempnetstat_with_programs.txt


...this produced the following first line in the output file, which doesn't fully satisfy the requirements:



netstat -obna  
....


With %aaa% variable



set aaa=netstat -obna
echo (%aaa%>C:tempnetstat_with_programs.txt) && (echo %aaa%|cmd)>>C:tempnetstat_with_programs.txt


...this produces the same output, which doesn't fully meet the requirements:



netstat -obna  
...






windows command-line cmd.exe






share|improve this question















share|improve this question













share|improve this question




share|improve this question








edited Dec 4 at 16:06

























asked Dec 4 at 12:49









hot2use

335212




335212












  • Your English is completely ambiguous and thus not clear what you are asking. You should provide examples showing exactly what you mean. Show the contents of C:tempnetstat_with_programs.txt before and after. Demonstrating exactly what you mean rather than trying to use your highly ambigious words.
    – barlop
    Dec 4 at 15:13












  • Also your title gives no indication of exactly what you are talking about
    – barlop
    Dec 4 at 15:16










  • I'd be interested to know which part of my question is ambiguous? The input is clearly stated in my question. netstat -obna >C:tempnetstat_with_programs.txt. The output of my command is a text file which contains the contents outlined in the Output section of my question, which includes the command I executed. I don't think that my question is either ambiguous, nor syntactically, orthographically or typographically incorrect. I would appreciate if you could exactly explain which part of the question you are unable to grasp.
    – hot2use
    Dec 4 at 15:22










  • Because it is a > the file is previously non-existent. I am not adding (>>) to an existing file.
    – hot2use
    Dec 4 at 15:26










  • You have now corrected the unclear part of your question
    – barlop
    Dec 4 at 15:36


















  • Your English is completely ambiguous and thus not clear what you are asking. You should provide examples showing exactly what you mean. Show the contents of C:tempnetstat_with_programs.txt before and after. Demonstrating exactly what you mean rather than trying to use your highly ambigious words.
    – barlop
    Dec 4 at 15:13












  • Also your title gives no indication of exactly what you are talking about
    – barlop
    Dec 4 at 15:16










  • I'd be interested to know which part of my question is ambiguous? The input is clearly stated in my question. netstat -obna >C:tempnetstat_with_programs.txt. The output of my command is a text file which contains the contents outlined in the Output section of my question, which includes the command I executed. I don't think that my question is either ambiguous, nor syntactically, orthographically or typographically incorrect. I would appreciate if you could exactly explain which part of the question you are unable to grasp.
    – hot2use
    Dec 4 at 15:22










  • Because it is a > the file is previously non-existent. I am not adding (>>) to an existing file.
    – hot2use
    Dec 4 at 15:26










  • You have now corrected the unclear part of your question
    – barlop
    Dec 4 at 15:36
















Your English is completely ambiguous and thus not clear what you are asking. You should provide examples showing exactly what you mean. Show the contents of C:tempnetstat_with_programs.txt before and after. Demonstrating exactly what you mean rather than trying to use your highly ambigious words.
– barlop
Dec 4 at 15:13






Your English is completely ambiguous and thus not clear what you are asking. You should provide examples showing exactly what you mean. Show the contents of C:tempnetstat_with_programs.txt before and after. Demonstrating exactly what you mean rather than trying to use your highly ambigious words.
– barlop
Dec 4 at 15:13














Also your title gives no indication of exactly what you are talking about
– barlop
Dec 4 at 15:16




Also your title gives no indication of exactly what you are talking about
– barlop
Dec 4 at 15:16












I'd be interested to know which part of my question is ambiguous? The input is clearly stated in my question. netstat -obna >C:tempnetstat_with_programs.txt. The output of my command is a text file which contains the contents outlined in the Output section of my question, which includes the command I executed. I don't think that my question is either ambiguous, nor syntactically, orthographically or typographically incorrect. I would appreciate if you could exactly explain which part of the question you are unable to grasp.
– hot2use
Dec 4 at 15:22




I'd be interested to know which part of my question is ambiguous? The input is clearly stated in my question. netstat -obna >C:tempnetstat_with_programs.txt. The output of my command is a text file which contains the contents outlined in the Output section of my question, which includes the command I executed. I don't think that my question is either ambiguous, nor syntactically, orthographically or typographically incorrect. I would appreciate if you could exactly explain which part of the question you are unable to grasp.
– hot2use
Dec 4 at 15:22












Because it is a > the file is previously non-existent. I am not adding (>>) to an existing file.
– hot2use
Dec 4 at 15:26




Because it is a > the file is previously non-existent. I am not adding (>>) to an existing file.
– hot2use
Dec 4 at 15:26












You have now corrected the unclear part of your question
– barlop
Dec 4 at 15:36




You have now corrected the unclear part of your question
– barlop
Dec 4 at 15:36










2 Answers
2






active

oldest

votes


















1














This is impossible because the shell, which takes the command and executes it, has no idea of what the arguments do that it is passing to the command.



For example in our theoretical rot13write C:foobar.txt, is P:sbbone.gkg



rot13write -qevir "P" -svyr "sbbone" rkg ".gkg"


That may tell rot13write to write to the drive P, the file foobar, and the extension txt. Or it may all be a joke and that path could have already been hard coded into the executable. You don't know, and neither does a shell.



So the shell can not echo to the file that a program is mysteriously writing to, because the shell doesn't know of that sufficiently; and, the program which does know how it is invoked is under no obligation to do anything with that data (like print the invocation command to the file it's outputting to). What you can do is




  1. Have the shell echo all its commands. Most shells support this.

  2. Have the program that you execute write to standard output (as it normally does) which the program will inherit from the shell that spawned it (this is how programs you call and the shell write to the same place -- the psuedo terminal)

  3. Redirect the shells standard output to destination file.


This will do everything you want except show the flag and output location in the command.



This looks like this in command prompt (I think)



cmd /c "netstat" > myOutput.txt | type myOutput.txt


And it looks like this in PowerShell,



powershell -command "Set-PSDebug -Trace 1; netstat" | tee myOutput.txt





share|improve this answer





























    1














    If the problem is to display (or redirect to a file) the redirection symbol (>) with the ECHO command, then you just need to escape it. The escape symbol in this case would be ^:



    ECHO netstat -obna ^>C:tempnetstat_with_programs.txt >C:tempnetstat_with_programs.txt
    netstat -obna >>C:tempnetstat_with_programs.txt


    The second command in the above snippet is using >> instead of > because you want to add the output to the same file rather than to overwrite it.






    share|improve this answer





















    • I was just starting to wonder why it didn't work, until I realised that your solution is to execute two commands. Good solution, but it breaks the flow somewhat.
      – hot2use
      Dec 5 at 10:10











    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%2f1380687%2fpipe-executed-command-into-the-same-output-file-of-the-command%23new-answer', 'question_page');
    }
    );

    Post as a guest















    Required, but never shown

























    2 Answers
    2






    active

    oldest

    votes








    2 Answers
    2






    active

    oldest

    votes









    active

    oldest

    votes






    active

    oldest

    votes









    1














    This is impossible because the shell, which takes the command and executes it, has no idea of what the arguments do that it is passing to the command.



    For example in our theoretical rot13write C:foobar.txt, is P:sbbone.gkg



    rot13write -qevir "P" -svyr "sbbone" rkg ".gkg"


    That may tell rot13write to write to the drive P, the file foobar, and the extension txt. Or it may all be a joke and that path could have already been hard coded into the executable. You don't know, and neither does a shell.



    So the shell can not echo to the file that a program is mysteriously writing to, because the shell doesn't know of that sufficiently; and, the program which does know how it is invoked is under no obligation to do anything with that data (like print the invocation command to the file it's outputting to). What you can do is




    1. Have the shell echo all its commands. Most shells support this.

    2. Have the program that you execute write to standard output (as it normally does) which the program will inherit from the shell that spawned it (this is how programs you call and the shell write to the same place -- the psuedo terminal)

    3. Redirect the shells standard output to destination file.


    This will do everything you want except show the flag and output location in the command.



    This looks like this in command prompt (I think)



    cmd /c "netstat" > myOutput.txt | type myOutput.txt


    And it looks like this in PowerShell,



    powershell -command "Set-PSDebug -Trace 1; netstat" | tee myOutput.txt





    share|improve this answer


























      1














      This is impossible because the shell, which takes the command and executes it, has no idea of what the arguments do that it is passing to the command.



      For example in our theoretical rot13write C:foobar.txt, is P:sbbone.gkg



      rot13write -qevir "P" -svyr "sbbone" rkg ".gkg"


      That may tell rot13write to write to the drive P, the file foobar, and the extension txt. Or it may all be a joke and that path could have already been hard coded into the executable. You don't know, and neither does a shell.



      So the shell can not echo to the file that a program is mysteriously writing to, because the shell doesn't know of that sufficiently; and, the program which does know how it is invoked is under no obligation to do anything with that data (like print the invocation command to the file it's outputting to). What you can do is




      1. Have the shell echo all its commands. Most shells support this.

      2. Have the program that you execute write to standard output (as it normally does) which the program will inherit from the shell that spawned it (this is how programs you call and the shell write to the same place -- the psuedo terminal)

      3. Redirect the shells standard output to destination file.


      This will do everything you want except show the flag and output location in the command.



      This looks like this in command prompt (I think)



      cmd /c "netstat" > myOutput.txt | type myOutput.txt


      And it looks like this in PowerShell,



      powershell -command "Set-PSDebug -Trace 1; netstat" | tee myOutput.txt





      share|improve this answer
























        1












        1








        1






        This is impossible because the shell, which takes the command and executes it, has no idea of what the arguments do that it is passing to the command.



        For example in our theoretical rot13write C:foobar.txt, is P:sbbone.gkg



        rot13write -qevir "P" -svyr "sbbone" rkg ".gkg"


        That may tell rot13write to write to the drive P, the file foobar, and the extension txt. Or it may all be a joke and that path could have already been hard coded into the executable. You don't know, and neither does a shell.



        So the shell can not echo to the file that a program is mysteriously writing to, because the shell doesn't know of that sufficiently; and, the program which does know how it is invoked is under no obligation to do anything with that data (like print the invocation command to the file it's outputting to). What you can do is




        1. Have the shell echo all its commands. Most shells support this.

        2. Have the program that you execute write to standard output (as it normally does) which the program will inherit from the shell that spawned it (this is how programs you call and the shell write to the same place -- the psuedo terminal)

        3. Redirect the shells standard output to destination file.


        This will do everything you want except show the flag and output location in the command.



        This looks like this in command prompt (I think)



        cmd /c "netstat" > myOutput.txt | type myOutput.txt


        And it looks like this in PowerShell,



        powershell -command "Set-PSDebug -Trace 1; netstat" | tee myOutput.txt





        share|improve this answer












        This is impossible because the shell, which takes the command and executes it, has no idea of what the arguments do that it is passing to the command.



        For example in our theoretical rot13write C:foobar.txt, is P:sbbone.gkg



        rot13write -qevir "P" -svyr "sbbone" rkg ".gkg"


        That may tell rot13write to write to the drive P, the file foobar, and the extension txt. Or it may all be a joke and that path could have already been hard coded into the executable. You don't know, and neither does a shell.



        So the shell can not echo to the file that a program is mysteriously writing to, because the shell doesn't know of that sufficiently; and, the program which does know how it is invoked is under no obligation to do anything with that data (like print the invocation command to the file it's outputting to). What you can do is




        1. Have the shell echo all its commands. Most shells support this.

        2. Have the program that you execute write to standard output (as it normally does) which the program will inherit from the shell that spawned it (this is how programs you call and the shell write to the same place -- the psuedo terminal)

        3. Redirect the shells standard output to destination file.


        This will do everything you want except show the flag and output location in the command.



        This looks like this in command prompt (I think)



        cmd /c "netstat" > myOutput.txt | type myOutput.txt


        And it looks like this in PowerShell,



        powershell -command "Set-PSDebug -Trace 1; netstat" | tee myOutput.txt






        share|improve this answer












        share|improve this answer



        share|improve this answer










        answered Dec 4 at 16:40









        Evan Carroll

        4,231852101




        4,231852101

























            1














            If the problem is to display (or redirect to a file) the redirection symbol (>) with the ECHO command, then you just need to escape it. The escape symbol in this case would be ^:



            ECHO netstat -obna ^>C:tempnetstat_with_programs.txt >C:tempnetstat_with_programs.txt
            netstat -obna >>C:tempnetstat_with_programs.txt


            The second command in the above snippet is using >> instead of > because you want to add the output to the same file rather than to overwrite it.






            share|improve this answer





















            • I was just starting to wonder why it didn't work, until I realised that your solution is to execute two commands. Good solution, but it breaks the flow somewhat.
              – hot2use
              Dec 5 at 10:10
















            1














            If the problem is to display (or redirect to a file) the redirection symbol (>) with the ECHO command, then you just need to escape it. The escape symbol in this case would be ^:



            ECHO netstat -obna ^>C:tempnetstat_with_programs.txt >C:tempnetstat_with_programs.txt
            netstat -obna >>C:tempnetstat_with_programs.txt


            The second command in the above snippet is using >> instead of > because you want to add the output to the same file rather than to overwrite it.






            share|improve this answer





















            • I was just starting to wonder why it didn't work, until I realised that your solution is to execute two commands. Good solution, but it breaks the flow somewhat.
              – hot2use
              Dec 5 at 10:10














            1












            1








            1






            If the problem is to display (or redirect to a file) the redirection symbol (>) with the ECHO command, then you just need to escape it. The escape symbol in this case would be ^:



            ECHO netstat -obna ^>C:tempnetstat_with_programs.txt >C:tempnetstat_with_programs.txt
            netstat -obna >>C:tempnetstat_with_programs.txt


            The second command in the above snippet is using >> instead of > because you want to add the output to the same file rather than to overwrite it.






            share|improve this answer












            If the problem is to display (or redirect to a file) the redirection symbol (>) with the ECHO command, then you just need to escape it. The escape symbol in this case would be ^:



            ECHO netstat -obna ^>C:tempnetstat_with_programs.txt >C:tempnetstat_with_programs.txt
            netstat -obna >>C:tempnetstat_with_programs.txt


            The second command in the above snippet is using >> instead of > because you want to add the output to the same file rather than to overwrite it.







            share|improve this answer












            share|improve this answer



            share|improve this answer










            answered Dec 4 at 20:16









            Andriy M

            230310




            230310












            • I was just starting to wonder why it didn't work, until I realised that your solution is to execute two commands. Good solution, but it breaks the flow somewhat.
              – hot2use
              Dec 5 at 10:10


















            • I was just starting to wonder why it didn't work, until I realised that your solution is to execute two commands. Good solution, but it breaks the flow somewhat.
              – hot2use
              Dec 5 at 10:10
















            I was just starting to wonder why it didn't work, until I realised that your solution is to execute two commands. Good solution, but it breaks the flow somewhat.
            – hot2use
            Dec 5 at 10:10




            I was just starting to wonder why it didn't work, until I realised that your solution is to execute two commands. Good solution, but it breaks the flow somewhat.
            – hot2use
            Dec 5 at 10:10


















            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.





            Some of your past answers have not been well-received, and you're in danger of being blocked from answering.


            Please pay close attention to the following guidance:


            • 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%2f1380687%2fpipe-executed-command-into-the-same-output-file-of-the-command%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

            Plaza Victoria

            In PowerPoint, is there a keyboard shortcut for bulleted / numbered list?

            How to put 3 figures in Latex with 2 figures side by side and 1 below these side by side images but in...