gpt4 book ai didi

java - Linux shell 命令与 Runtime.exec()(屏幕命令)的行为很奇怪

转载 作者:塔克拉玛干 更新时间:2023-11-03 00:27:41 24 4
gpt4 key购买 nike

我坐在这里是因为至少一直在解决一个真正让我失望的问题!

我正在编写一个简单的小型 Java 程序,它可以轻松地将任何命令传递给 (linux) 屏幕 session 。

Shell command: bash -c "screen -p 0 -S sessionname -X eval 'stuff \"some command\"\015'"

当我在命令 shell 中输入它时,一切正常!但是,如果我让 Java 程序执行此操作,则什么也不会发生!甚至没有消息、错误或提示!简直什么都没有!我让程序回显生成的屏幕命令,如果我获取该输出并将其粘贴到 shell 中,它就会工作。

这是我的 Java 代码:



static public void screenCmd() throws IOException
{
String command = "bash -c \"screen -p 0 -S screenname -X eval 'stuff \\\"cmd\\\"\\015'\"";

System.out.println("debug: '" + command + "'"); //output would work
//when copy and paste it to the shell

InputStreamReader isr = new InputStreamReader(
Runtime.getRuntime().exec(command).getInputStream()
);

//for debug output
BufferedReader br = new BufferedReader(isr);
String line = "";
while ((line = br.readLine()) != null)
System.out.println(line);
}

最有趣的是,所有其他 shell 命令都可以正常工作。我尝试使用 Java 执行 tail、whomi、cp、ls...,没有任何问题。为什么不筛选?

Java 程序由启动屏幕 session 的同一用户启动。

如果有人有任何小想法,请报告!我在这里疯了!

p.s.: screen session还运行了一个java程序! (这可能是问题的一部分吗?)

谢谢。

最佳答案

关于java - Linux shell 命令与 Runtime.exec()(屏幕命令)的行为很奇怪,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/9031738/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com