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

}

output=$(java Test200)
echo $output

我得到了 David Peter 的两个值。假设我只想在 shell 脚本中返回“David”?有什么线索吗?谢谢。

4

2 回答 2

4

您不返回"David",并且"Peter"将它们打印到STDOUT. 因此,如果您只想打印其中一个,只需删除另一个println调用。

您只能将整数值返回给 shell。这是由System.exit(status).

于 2013-04-10T06:59:23.837 回答
0

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.

于 2013-04-10T08:14:50.597 回答