java如何调用linux命令

package com.huateng.util;

import java.io.*;
import java.util.ArrayList;
import java.util.List;

public class LinuxUtil {

    public static void main(String[] args) {
        String cmd = "sort -t,  -k2n,2  -k1,1   aa";
        LinuxUtil.exec(cmd);

        List commands = new ArrayList<>();
        commands.add("cd /home/whj/tmp/hello");
        commands.add("sort -t,  -k2n,2  -k1,1   aa");
        commands.add("sort -t,  -k2n,2  -k1,1   aa > vv");
        LinuxUtil.execFlow(commands);
    }


    public static Object execFlow(List commands) {
        try {
            String cmds = "";
            for (String cmd : commands) {
                cmds += cmd + ";";
            }
            String[] cmdA = {"/bin/sh", "-c", cmds};
            Process process = Runtime.getRuntime().exec(cmdA);
            LineNumberReader br = new LineNumberReader(new InputStreamReader(
                    process.getInputStream()));
            StringBuffer sb = new StringBuffer();
            String line;
            while ((line = br.readLine()) != null) {
                System.out.println(line);
                sb.append(line).append("\n");
            }
            return sb.toString();
        } catch (Exception e) {
            e.printStackTrace();
        }
        return null;
    }

    public static Object exec(String cmd) {
        try {
            String[] cmdA = {"/bin/sh", "-c", cmd};
            Process process = Runtime.getRuntime().exec(cmdA);
            LineNumberReader br = new LineNumberReader(new InputStreamReader(
                    process.getInputStream()));
            StringBuffer sb = new StringBuffer();
            String line;
            while ((line = br.readLine()) != null) {
                System.out.println(line);
                sb.append(line).append("\n");
            }
            return sb.toString();
        } catch (Exception e) {
            e.printStackTrace();
        }
        return null;
    }


}

 

你可能感兴趣的:(linux)