简体   繁体   中英

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

consider the following java program:

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):

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

I get the next output:

Hi Fernando

I know there are commands like 'yes' in Linux, with which I can get infinite stream of data. 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.

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.

To redirect it to a file, you can wrap that in a grouping construct:

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

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