简体   繁体   English

如何从C#写入cygwin管道

[英]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". 我想创建可在带有“ tail -f”的cygwin管道中使用的ac#应用程序。

ie

tail -f SomeFile | 尾-f SomeFile | MyCSharpApp MyCSharpApp

I can see (from debugging) that I am able to read stdinput correctly, but nothing is written back to cygwin terminal windows. 我可以从调试中看到,我能够正确读取stdinput,但是什么都没有写回cygwin终端窗口。

However, 然而,

tail -n10 SomeFile | 尾-n10 SomeFile | MyCSharpApp 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. 尚不清楚SomeFile发生了什么。 tail -f SomeFile opens the SomeFile file descriptor and monitors it. tail -f SomeFile打开SomeFile文件描述符并对其进行监视。 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 . 我建议尝试使用tail -F SomeFile -F reopens a file descriptor. -F重新打开文件描述符。

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 . 也就是说,它是为尝试myApp > tail -f而不是tail -f file.txt > myApp而编写的。

tail -f works on a file and not a pipe. tail -f适用于文件而不是管道。 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. 我可以得到它来输出另一个命令的输出的唯一方法是,如果我使用tail -F <(ls -lthr) ,其中ls -lthr可以是任何显示输出的命令。

Let's think about why this is. 让我们考虑一下这是为什么。 tail prints the contents of a file (particularly the the lines at the end). tail打印文件的内容(尤其是末尾的行)。 If you have the ability to print to the console or stdout then you do not need tail . 如果您能够打印到控制台或标准输出,则不需要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. 附加:此外,如果您决定需要输出到文件中,则可以简单地使用> file.txt将控制台输出放入文件中。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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