无法将shell脚本的echo输出到Java变量

时间:2014-03-06 09:48:14

标签: java shell echo output

我用Java调用shell脚本,在shell脚本中运行一些查询并获取运行时间(例如,当我将echo TIME的结果保存在输出文件中时,我在一列中得到例如28秒27秒34秒)但是我无法将回显TIME的输出传递给Java,在运行程序时我得到了一些奇怪的十进制数。我从shell脚本获取输出的代码是:

    import java.util.*;
    import java.io.*;

    public class Test
    {
       public static void main(String[] args)
       {
          TreeMap <String, Long> myMap = new TreeMap <String, Long>();
          String output = "";
          String line = "";

          myMap.put("key", value);
          myMap.put("key", value);

          List<String> cmdList = new ArrayList<String>();
          cmdList.add("/path/to/the/test/file/test.sh");
          for(Map.Entry entry : myMap.entrySet())
          {
             cmdList.add(entry.getKey().toString());
             cmdList.add(entry.getValue().toString());

          }
          try
          {
            Process process = Runtime.getRuntime().exec(cmdList.toArray(new String[cmdList.size()]));
            //get input stream of the process
            BufferedReader in = new BufferedReader(new InputStreamReader(process.getInputStream()));
            while ((line = in.readLine()) != null)
            {
             output += line;
            } 
          }
          catch(IOException e){ System.out.println("Caught IOException: " + e.getMessage()); }

      System.out.println(output);
     }

 }

我的shell脚本看起来像这样:

    i=0
    args=("$@")
    while [ $i -lt $# ]
    do
    START=`date +%s`
       some operation here
    END=`date +%s`
    echo $END - $START | bc
    done

来自java的奇怪输出:

    A   F   37734107.00 56586554400.73  53758257134.8700    55909065222.827692  25.522006   38273.129735    0.049985    1478493N    991417.00   1487504710.38   1413082168.0541 1469649223.194375   25.516472   38284.467761    0.050093    38854N  O

我的代码可能出错了什么?

0 个答案:

没有答案