有趣的 Shell 输出:[01;32mtestfile.txt[00m 而不是 testfile.txt [英] Funny Shell Output: [01;32mtestfile.txt[00m instead of testfile.txt

查看:30
本文介绍了有趣的 Shell 输出:[01;32mtestfile.txt[00m 而不是 testfile.txt的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

问题:我从使用 ChannelShell 我想知道如何获得您在 PuTTY 会话中看到的常规输出.

Question: I'm getting "funny" character output from commands I send when using a ChannelShell and I'm wondering how to get just the regular output you see in a PuTTY session.

有趣"的意思是我应该看到这个:

By "funny" I mean I should see this:

testfile.txt

我看到了:

[01;32mtestfile.txt[00m

这类似于这个问题 除了答案不能满足我的需要.答案是在 ChannelShell 上调用 setPty(false) 以完全删除伪终端",但我需要实时获得 ChannelShell 的输出.这是我正在做的一个例子:

This is similar to the problem in this question except the answer does not satisfy my need. The answer there is to call setPty(false) on the ChannelShell which removes the "pseudo-terminal" entirely, but I need to have the output from the ChannelShell in real time. Here's an example of what I'm doing:

ChannelShell channel = (ChannelShell) session.openChannel("shell");
channel.setOutputStream(new PrintStream(
    new ByteArrayOutputStream(), true, "UTF-8") {
        @Override
        public void write(byte[] b, int off, int len) {
            super.write(b, off, len);
            String output = new String(b, off, len);
            System.out.print(output);
            sendNextCommand(output); //Execution of the next command depends on the output here, this is why I need it to not have the funny characters.
        }
    });
PipedInputStream in = new PipedInputStream();
channelInput = new PipedOutputStream(in);
channel.setInputStream(in);
channel.connect();

while(!channel.isClosed() && channelWaitRetries++ < MAX_CHANNEL_WAIT_RETRIES) {
    //Wait for additional output... Sort of a timeout deal. Kind of a hack...
    sleep(2500); //Calls Thread.sleep. I just don't want the try/catch here.
    System.out.println("Channel not yet closed. Retried " + channelWaitRetries + " of " + MAX_CHANNEL_WAIT_RETRIES);
}

sendNextCommand 方法用于检查输出是否与下一个要执行的命令需要显示的内容相匹配.所以基本上,当我看到这样的东西时: [user@server ~]$ 然后执行这个: ls 应该返回这个: testfile.txt相反它返回这个:[01;32mtestfile.txt[00m(注意:我不能复制和粘贴第一个字符,但它是一个带有27 的字符代码,我认为是转义字符).

The sendNextCommand method is where the check is done to see if the output matches the what needs to be showing for the next command to execute. So basically, when I see something like this: [user@server ~]$ then execute this: ls which should return this: testfile.txt but instead it is returning this: [01;32mtestfile.txt[00m (NOTE: I can't copy and paste the first character, but it's a box with the char code of 27 which I think is an escape character).

现在我通常会逃避这种事情,但我宁愿把它做​​对,而且似乎有许多奇怪的变体.所以我来了.希望能帮到你 :)

Now I would normally just escape this kind of thing, but I'd rather get it done right, and it also seems like there are a number of variants to the weirdness. So here I am. I hope you can help :)

注意:我通过我的 IDE(一个 eclipse rip-off)在我的 Windows 机器上运行它,但我已经尝试过调试并且变量 output 实际上显示了有趣的"字符.我还尝试在 JOptionPane.messageDialog 中显示它,以确保它不仅仅是 IDE 并且它仍然具有字符.谢谢!

Note: I'm running this through my Windows machine through my IDE (an eclipse rip-off), but I've tried debugging and the variable output actually shows the "funny" characters. I've also tried showing it in a JOptionPane.messageDialog just to make sure it's not just the IDE and it still has the characters. Thanks!

推荐答案

是的,这些是 ANSI 转义序列,在 ls 的情况下,用于控制颜色.

Yes, these are ANSI escape sequences, in the case of ls, to control the colour.

ls 有一个 --color=never 选项,可以解决特定的 ls 问题.

ls has a --color=never option which should resolve the specific ls problem.

这是否发生在所有命令上,还是仅发生在某些命令上?

Is this happening on all commands, or just some?

调用 ChannelShell.setPtyType("dumb") 也有帮助(未经测试).您可能需要四处寻找禁用转义序列的终端类型.

Calling ChannelShell.setPtyType("dumb") could also help (untested). You may need to fiddle around to find a terminal type that disables the escape sequences.

您对 ls 进行脱壳而不是使用内置 Java 方法来检查目录内容的任何特殊原因?

Any particular reason you are shelling ls rather than using built-in Java methods to check for directory contents?

这篇关于有趣的 Shell 输出:[01;32mtestfile.txt[00m 而不是 testfile.txt的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆