java - 如何从 Java 调用格式化的 linux shell 命令?

标签 java linux

我正在开发一个项目,我必须使用 java 代码跟踪 Linux 上的磁盘使用情况。我已经执行了以下代码

public  void checkSystemPerformance() {
    System.out.println( "------- Track Disk Usage -------" );

        BufferedReader stdInput  = null;
        BufferedReader stdError = null;
        String line = "";

        try {
            String diskUsageCommandTest = "df -h";
            //String diskUsageCommandTest = "df -h | awk '$NF==\"/\"{printf \"Disk Usage: %d/%dGB (%s)\n\", $3,$2,$5}'";I want the result of this command via java code

            Process proc = Runtime.getRuntime().exec(diskUsageCommandTest);

            stdInput  =  new BufferedReader(new InputStreamReader(proc.getInputStream()));
            stdError = new BufferedReader(new InputStreamReader(proc.getErrorStream()));

            // read the output from the command
            System.out.println("Here is the standard output of the command:\n");
            while((line = stdInput .readLine()) != null) {
                System.out.println(line);
            }

            // read any errors from the attempted command
            System.out.println("Here is the standard error of the command (if any):\n");
            while ((line = stdError.readLine()) != null) {
                System.out.println(line);
            }

            proc.waitFor();   

            } catch (Exception e) {
                e.printStackTrace();
                System.out.println(e.getMessage());
        }
    }

我得到以下输出:

------- Track Memory Usage,Disk Usage and CPU Load -------
Here is the standard output of the command:

Filesystem                   Size  Used Avail Use% Mounted on
/dev/mapper/ubuntu--vg-root   19G   16G  2.3G  88% /
udev                         987M  4.0K  987M   1% /dev
tmpfs                        200M  268K  200M   1% /run
none                         5.0M     0  5.0M   0% /run/lock
none                         998M     0  998M   0% /run/shm
/dev/sda1                    236M   33M  191M  15% /boot
Here is the standard error of the command (if any):

我已经通过java代码执行了下面的行

String diskUsageCommandTest = "df -h | awk '$NF==\"/\"{printf \"Disk Usage: %d/%dGB (%s)\n\", $3,$2,$5}'";

我得到以下输出:

------- Track Memory Usage,Disk Usage and CPU Load -------
Here is the standard output of the command:

Here is the standard error of the command (if any):

df: `|': No such file or directory
df: `awk': No such file or directory
df: `\'$NF=="/"{printf': No such file or directory
df: `"Disk': No such file or directory
df: `Usage:': No such file or directory
df: `%d/%dGB': No such file or directory
df: `(%s)': No such file or directory
df: `",': No such file or directory
df: `$3,$2,$5}\'': No such file or directory
df: no file systems processed

在 Linux 终端上运行以下命令:

df -h | awk '$NF=="/"{printf "Disk Usage: %d/%dGB (%s)\n", $3,$2,$5}'

我得到以下输出:

Disk Usage: 16/19GB (88%)

所以有人知道我可以在 java 代码中做什么来获得“磁盘使用情况:16/19GB (88%)”这样的格式的输出吗?

最佳答案

你可以看看here ,这将解决您的问题 - 如果您的情况可能的话(使用您的命令创建一个 shell 脚本并执行该脚本)

关于java - 如何从 Java 调用格式化的 linux shell 命令?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/37827762/

相关文章:

linux - 使用示例代码测试 Linsched

java - Android:自定义RecyclerView中的单行

java - Intellij maven模块继承

java - 将 onClick 添加到 RecyclerView 项目的一部分

linux - 排序命令不起作用

java - 如何在没有终端的情况下在其他linux计算机上打开java程序?

linux - elasticsearch 中的 Gradle 问题

java - 在 Apache HTTP Server 中调用 Java 程序

java - 在所有其他用户线程完成后,守护线程生成的用户线程是否继续存在?

linux - 将目录内指定扩展名的所有文件转换为pdf,对所有子目录递归