简体   繁体   English

如何从控制台将无限 stream (bash) 转换为 java args?

[英]How to make infinite stream (bash) into java args from console?

consider the following java program:考虑以下 java 程序:

class PrintName{
    public static void main(String[] args){
        System.out.println("Hi " + args[0]);
    }
}

Now I just compile and execute it from console (I'm on Ubuntu server):现在我只是从控制台编译并执行它(我在 Ubuntu 服务器上):

~$:javac PrintName.java
~$:java PrintName "Fernando"

I get the next output:我得到下一个 output:

Hi Fernando

I know there are commands like 'yes' in Linux, with which I can get infinite stream of data.我知道在 Linux 中有类似“yes”的命令,通过它我可以获得无限的 stream 数据。 My idea is to do something like this:我的想法是做这样的事情:

yes "Fernando" | java PrintName >> my_file.txt

I want to be able to pass "infinite" Fernando's to my program and have it run infinitely many times, then be able to manipulate the STDO to redirect to some file.我希望能够将“无限”费尔南多传递给我的程序并让它无限运行多次,然后能够操纵 STDO 重定向到某个文件。

I don't know if I explained it clearly, sorry for my poor handling of the English language.我不知道我是否解释清楚,抱歉我对英语的处理不好。 Thank you very much for your time.非常感谢您的宝贵时间。

This is where you want the command:这是您想要命令的地方:

yes Fernando | xargs java PrintName

xargs takes each line of stdin, and passes it as command line arguments to the given command. xargs 获取标准输入的每一行,并将其作为命令行 arguments 传递给给定的命令。

To redirect it to a file, you can wrap that in a grouping construct:要将其重定向到文件,您可以将其包装在分组构造中:

{ yes Fernando | xargs java PrintName; } >> my_file.txt

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

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