简体   繁体   中英

How to write to cygwin pipe from c#

I want to create ac# app that can be used in a cygwin pipe with "tail -f".

ie

tail -f SomeFile | MyCSharpApp

I can see (from debugging) that I am able to read stdinput correctly, but nothing is written back to cygwin terminal windows.

However,

tail -n10 SomeFile | MyCSharpApp

Works perfectly.

class Program
{
    static void Main(string[] args)
    {

        string s;
        while ((s = Console.ReadLine()) != null)
        {
           //Potentially process s here
           Console.WriteLine(s);
        }
    }
}

It isn't clear what have been happening with SomeFile. tail -f SomeFile opens the SomeFile file descriptor and monitors it. If the descriptor somehow changed, for an example, you have opened the file in notepad and clicked "Save", nothing would have been monitored. It happens because notepad deletes the file and writes new one with the same name. I would recommend to try tail -F SomeFile . -F reopens a file descriptor.

Edit: This answer was written wrongly about the opposite of the issue. That is to say that it was written for attempting myApp > tail -f and not tail -f file.txt > myApp .

tail -f works on a file and not a pipe. The only way that I can get it to output the output of another command is if I use tail -F <(ls -lthr) where ls -lthr could be any command that prints output.

Let's think about why this is. tail prints the contents of a file (particularly the the lines at the end). If you have the ability to print to the console or stdout then you do not need tail .

Extra: and furthermore if you decided that you needed to output into a file then you could simply use > file.txt to put that console output into a file.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM