java中shell调用

java中调用shell代码片段

package com.tuxianchao;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.ArrayList;
import java.util.List;

/**
 * created by
 *
 * @author tuxianchao
 * @date 2018/6/11 上午10:43
 */
public class Test {

    public static void main(String[] args) {
        for (String line : callShell("ls -la /")) {
            System.out.println(line);
        }
        /**
         * console output
         *total 94
         * drwxr-xr-x  35 root  wheel   1258 May 12 19:21 .
         * drwxr-xr-x  35 root  wheel   1258 May 12 19:21 ..
         * -rw-rw-r--   1 root  admin   6148 Feb  4  2017 .DS_Store
         * d--x--x--x   9 root  wheel    306 Jun  6 13:17 .DocumentRevisions-V100
         * drwxr-xr-x   2 root  wheel     68 Nov 13  2014 .PKInstallSandboxManager
         * drwxr-xr-x@  2 root  wheel     68 Jun  6 21:43 .PKInstallSandboxManager-SystemSoftware
         * drwx------   5 root  wheel    170 Nov 13  2014 .Spotlight-V100
         * d-wx-wx-wt   2 root  wheel     68 May  8 13:04 .Trashes
         * -rw-------   1 root  wheel  12288 Oct 25  2016 .bash_profile.swp
         * ----------   1 root  admin      0 Feb 21  2017 .file
         * drwx------  97 root  wheel   3298 Jun 11 03:30 .fseventsd
         * drwxr-xr-x@  2 root  wheel     68 Oct 24  2016 .vol
         * drwxrwxr-x+ 61 root  admin   2074 Jun  6 11:34 Applications
         * drwxr-xr-x+ 63 root  wheel   2142 Nov  9  2017 Library
         * drwxr-xr-x@  2 root  wheel     68 Oct 24  2016 Network
         * drwxr-xr-x@  4 root  wheel    136 May 12 19:23 System
         * drwxr-xr-x   7 root  admin    238 May  8 13:04 Users
         * drwxr-xr-x@  4 root  wheel    136 Jun  8 13:47 Volumes
         * drwxr-xr-x@ 38 root  wheel   1292 May 12 19:23 bin
         * drwxrwxr-t@  2 root  admin     68 Oct 24  2016 cores
         * dr-xr-xr-x   3 root  wheel   4614 Jun  6 13:17 dev
         * lrwxr-xr-x@  1 root  wheel     11 Oct 24  2016 etc -> private/etc
         * dr-xr-xr-x   2 root  wheel      1 Jun  6 13:32 home
         * -rw-r--r--@  1 root  wheel    313 Jul 31  2016 installer.failurerequests
         * dr-xr-xr-x   2 root  wheel      1 Jun  6 13:32 net
         * drwxr-xr-x@  3 root  wheel    102 Oct 22  2016 opt
         * drwxr-xr-x@  6 root  wheel    204 Oct 24  2016 private
         * drwxr-xr-x@ 63 root  wheel   2142 May 12 19:21 sbin
         * lrwxr-xr-x@  1 root  wheel     11 Oct 24  2016 tmp -> private/tmp
         * drwxr-xr-x@ 10 root  wheel    340 Sep 20  2017 usr
         * lrwxr-xr-x@  1 root  wheel     11 Oct 24  2016 var -> private/var
         * drwxr-xr-x  15 502   staff    510 Aug 26  2016 原来的文件
         * lrwxr-xr-x   1 root  wheel     49 Mar 23  2015 用户信息 -> /Library/Documentation/User Information.localized
         *
         */
    }

    public static List callShell(String shellString) {
        List result = new ArrayList();
        BufferedReader bufferedReader = null;
        try {
            Process process = Runtime.getRuntime().exec(shellString);
            int exitValue = process.waitFor();
            if (0 != exitValue) {
                System.out.println("call shell failed. error code is :" + exitValue);
            }
            //正常结束,Process的waitFor()方法返回0
            bufferedReader = new BufferedReader(new InputStreamReader(process.getInputStream()));
            String line;
            while ((line = bufferedReader.readLine()) != null) {
                result.add(line);
            }
        } catch (Throwable e) {
            System.out.println("call shell failed. " + e);
        } finally {
            try {
                bufferedReader.close();
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
        return result;
    }


}

你可能感兴趣的:(java中shell调用)