我的 Bash 終端有問題。

我的 Bash 終端有問題。

我的 Bash 終端有問題。

我的細節問題:

是的,我知道,你可以手動完成。但如果每個用戶都必須手動執行此操作,那就太痛苦了。我也用“echo”嘗試過,但它沒有像我想要的那樣工作。

如果您能告訴我如何使用 java 運行 bash 文件,我也會很高興。 (不過我可以自己弄清楚。)

基本上 Linux bash 終端機的顏色 0a。

如果可能的話,不需要任何特殊軟體。

也許這會有所幫助。但我無法執行它。

PS1="\[\033[34m\][\$(date +%H%M)][\u@\h:\w]$ "

我可以在終端機中使用“exec”命令執行。然而,顏色等命令。在 ping 命令之後或 ping 命令之前也不起作用。感謝你的回覆!

答案1

我的問題的解決方案是:

public static final String ANSI_RESET = "\u001B[0m";

public static final String ANSI_BLACK = "\u001B[30m";

public static final String ANSI_RED = "\u001B[31m";

public static final String ANSI_GREEN = "\u001B[32m";

public static final String ANSI_YELLOW = "\u001B[33m";

public static final String ANSI_BLUE = "\u001B[34m";

public static final String ANSI_PURPLE = "\u001B[35m";

public static final String ANSI_CYAN = "\u001B[36m";

public static final String ANSI_WHITE = "\u001B[37m";

https://stackoverflow.com/questions/5923436/change-color-of-java-console-output

首先你聲明上面的顏色。然後,你寫:

System.out.println(ANSI_RED + "hello World");

然後你運行你的程式和你的好處。

最初是從: https://stackoverflow.com/questions/5923436/change-color-of-java-console-output

乾杯。

答案2

您可以非常輕鬆地透過 Java 運行 shell 腳本:

Runtime.getRuntime().exec(myCommand);

只要 shell 腳本不是互動式的(並且位於適用的路徑中),這就會起作用。當互動時,事情會變得很奇怪,你必須使用 Process Builder。

答案3

我相信這應該可行(您還需要轉義轉義序列 - 所以它們會被執行並且java不會擴展它們):

 p = Runtime.getRuntime().exec("/bin/bash -c 'echo -ne \"\\e[40m\\e[32m\"; ping " + b + "; echo -ne \"\\e[0m\"'");

相關內容