crchin
crchin

Reputation: 9683

java return specific value to shell script

public class Test200 {
    public static void main (String args []) {
        System.out.println("David");
        System.out.println("Peter");
}

}

output=$(java Test200)
echo $output

I get the both value which are David Peter. Let say I only want "David" to be returned in shell script ? Any clue ? Thanks.

Upvotes: 0

Views: 1217

Answers (2)

Kiran Jujare
Kiran Jujare

Reputation: 178

Try using grep command so that it will return the expected value alone for multiple values use egrep

output=$(java Test200)

instead use

output=$(java Test200 | grep 'David')

But i didnt tested this code, it should work.

or try this

java Test200 >> outfile

all the SYSOUT will redirect to outfile base on that you can do te operation with external script.

Upvotes: 0

user1907906
user1907906

Reputation:

You don't return "David" and "Peter", you print them to STDOUT. So if you only want to print one of these, just remove the other println call.

You can only return integer values to the shell. This is done by System.exit(status).

Upvotes: 4

Related Questions