我可以在Unix内运行jshell吗?

问题描述 投票:2回答:1

我想使用expect重定向jshell输入,这样我就可以模拟录制的演示中的输入。但是虽然我可以从expect脚本中生成一个jshell进程,它也可以识别jshell提示符,之后没有任何效果。期望输出看起来像控制序列,如^[[24;9R,我没有看到任何来自jshell的输出。不同的终端类型产生不同的字符序列,但它们都不起作用。这种行为在Ubuntu和Mac OS上的期望之间是一致的。有关如何调查此问题的任何建议都将受到欢迎。 expect -d没有帮助。

这是我要模拟的jshell会话的记录

$ jshell
|  Welcome to JShell -- Version 9.0.1
|  For an introduction type: /help intro

jshell> 3
$1 ==> 3

jshell> 

这是我认为应该这样做的脚本:

#!/usr/bin/expect -f
spawn jshell
expect jshell>
send "3\r"
expect jshell>

当我运行该脚本时(在Mac OS 10.11.6上,但我在Ubuntu上得到非常相似的结果),我看到这个输出

spawn jshell
|  Welcome to JShell -- Version 9.0.1
|  For an introduction type: /help intro

jshell> ^[[24;9R

然后期望超时,并且最后一行输出被shell提示覆盖(因此看起来好像在超时时会写入更多的控制字符)。

-d添加到脚本的第1行中的标志中会导致此输出:

expect version 5.45
argv[0] = /usr/bin/expect  argv[1] = -d  argv[2] = -f  argv[3] = ./expectscript
set argc 0
set argv0 "./expectscript"
set argv ""
executing commands from command file ./expectscript
spawn jshell
parent: waiting for sync byte
parent: telling child to go ahead
parent: now unsynchronized from child
spawn: returns {19712}

expect: does "" (spawn_id exp8) match glob pattern "jshell>"? no
|  Welcome to JShell -- Version 9.0.1
|  For an introduction type: /help intro

expect: does "|  Welcome to JShell -- Version 9.0.1\r\n|  For an introduction type: /help intro\r\n" (spawn_id exp8) match glob pattern "jshell>"? no

jshell>
expect: does "|  Welcome to JShell -- Version 9.0.1\r\n|  For an introduction type: /help intro\r\n\r\njshell> " (spawn_id exp8) match glob pattern "jshell>"? yes 
expect: set expect_out(0,string) "|  Welcome to JShell -- Version 9.0.1\r\n|  For an introduction type: /help intro\r\n\r\njshell> "
expect: set expect_out(spawn_id) "exp8"
expect: set expect_out(buffer) "|  Welcome to JShell -- Version 9.0.1\r\n|  For an introduction type: /help intro\r\n\r\njshell> "
send: sending "3\r" to { exp8 }

expect: does "" (spawn_id exp8) match glob pattern "jshell>"? no

expect: does "\u001b[6n" (spawn_id exp8) match glob pattern "jshell>"? no
^[[32;1Rexpect: timed out
terminal expect tty pty jshell
1个回答
3
投票

管理使其工作(使用jshell 9.0和Expect 5.45在Debian 9.3上测试):

[STEP 103] # cat jshell.exp
proc expect_prompt {} {
    upvar spawn_id spawn_id

    expect -ex "jshell> "

    # the CPR (cursor position report) code
    expect -ex "\x1b\[6n"

    # read the CPR result and send it the application
    expect_tty -re {\x1b\[[0-9]+;[0-9]+R}
    send $expect_out(0,string)
}

stty raw; # give tty's full control to jshell since it's crazy

spawn jshell
expect_prompt

send "3\r"
expect_prompt

send "/exit\n"
expect eof
[STEP 104] # expect jshell.exp
spawn jshell
|  Welcome to JShell -- Version 9.0.1
|  For an introduction type: /help intro

jshell> 3
$1 ==> 3

jshell> /exit
|  Goodbye
[STEP 105] #

魔术是关于CPR (cursor position report)(在页面上搜索CPR)。

  1. ^[[6n^[ == ESC == 0x1b == \u001b)是CPR请求(由jshell发送)。
  2. ^[[32;1R(第32行,第1列)这样的字符串是当前光标位置(由终端驱动程序生成并由jshell读回)。
© www.soinside.com 2019 - 2024. All rights reserved.