使用java运行terminal命令以清除终端历史记录

使用java运行terminal命令以清除终端历史记录,java,Java,我想了解终端历史 所以我做了这个 Runtime rt = Runtime.getRuntime(); pr = rt.exec("/bin/bash -c \"history -c\""); pr.waitFor(); rt.exec("/usr/bin/xterm"); 但是pr=rt.exec/bin/bash-c\history-c\;,存在问题,它没有清除xterm以前的历史记录,也没有清除我的正常终端 此外,当我试图打印历史记录时,它不会返回任何错误 p = Runtime.get

我想了解终端历史 所以我做了这个

Runtime rt = Runtime.getRuntime();
pr = rt.exec("/bin/bash -c \"history -c\"");
pr.waitFor();
rt.exec("/usr/bin/xterm");
但是pr=rt.exec/bin/bash-c\history-c\;,存在问题,它没有清除xterm以前的历史记录,也没有清除我的正常终端

此外,当我试图打印历史记录时,它不会返回任何错误

p = Runtime.getRuntime().exec("/bin/bash -c \"history\"");
p.waitFor();
BufferedReader reader = 
         new BufferedReader(new InputStreamReader(p.getInputStream()));

String line = "";       
System.out.println("printing");
while ((line = reader.readLine())!= null) {
    output.append(line + "\n");
}
我也试过了

String[] commands = new String[]{"/bin/sh","-c", "history -c" ,"xterm"};
try {
        Process proc = new ProcessBuilder(commands).start();
        BufferedReader stdInput = new BufferedReader(new 
                    InputStreamReader(proc.getInputStream()));

        BufferedReader stdError = new BufferedReader(new 
                        InputStreamReader(proc.getErrorStream()));
    } catch (IOException e1) {
        e1.printStackTrace();
    }

仍然没有清除历史记录。

您可以通过获取$HISTFILE环境变量自己删除历史记录文件。这将始终获得不同Shell的正确历史记录文件。我相信您遇到的问题是,您可能正在使用不同的shell,或者更改了历史文件的位置

import java.io.BufferedReader;
import java.io.InputStreamReader;

public class RemoveShellHistory {

    public static void main(String[] args) {
        RemoveShellHistory obj = new RemoveShellHistory();

        final String shellPath = System.getenv("SHELL");
        String shell = shellPath.substring(shellPath.lastIndexOf("/")+1, shellPath.length());
        final String home  = System.getenv("HOME");

        String command = "rm -v " + home + "/." + shell + "_history";
        String output = obj.executeCommand(command);

        System.out.println(output);
    }

    private String executeCommand(String command) {
        StringBuffer output = new StringBuffer();
        Process p;
        try {
            p = Runtime.getRuntime().exec(command);
            p.waitFor();
            BufferedReader reader =
            new BufferedReader(new InputStreamReader(p.getInputStream()));
            String line = "";
            while ((line = reader.readLine())!= null) {
                output.append(line + "\n");
            }
            } catch (Exception e) {
            e.printStackTrace();
        }
        return output.toString();
    }
}

假设java应用程序由拥有.bash_历史文件的同一用户运行:

删除

new File(System.getProperty("user.home"), ".bash_history").delete();
若要清除,请随意处理选中的异常

    FileWriter writer = new FileWriter(
        new File(System.getProperty("user.home"), ".bash_history"));
    writer.write("");
    writer.close();

我认为历史和-c应该是两个论点。这意味着调用rt.execnew字符串[]{history,-c}。@Pavelholl这样做了'rt.execnew字符串[]{/bin/bash,-c,history,-c};rt.exec/usr/bin/xterm;'仍然不工作您是在eclipse或其他IDE中运行应用程序吗?还是从命令行?@ElliottFrisch尝试了这个文件File=new File$HOME/.bash\u history;iffile.delete{System.out.printlnfile.getName+已删除!;}否则{System.out.printlnDelete操作失败。}进入else循环。Eclipse不会使用与bash命令行相同的环境设置运行。您需要模拟~/.bash_配置文件中的设置,或者您用于执行程序的运行配置的环境选项卡中碰巧使用的任何设置。不工作没有错误。System.getenvHISTFILE正在返回null。$HISTFILE似乎是一个shell变量而不是一个env变量。我更新了脚本以仅删除.shell_历史文件。shell在退出时仍将写出一个新副本。