我可以在 Unix 中运行 jshell 吗?



我想使用 expect 重定向 jshell 输入,以便我可以模拟录制演示中的打字。但是,尽管我可以从期望脚本生成一个 jshell 进程,该脚本也可以识别 jshell 提示符,但之后没有任何效果。期望输出看起来像控制序列的东西,比如^[[24;9R,我没有看到 jshell 的任何输出。不同的终端类型会产生不同的字符序列,但它们都不起作用。这种行为在 Ubuntu 和 Mac OS 上的 expect 是一致的。欢迎就如何调查此问题提出任何建议。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 "3r"
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 的标志会导致以下输出:

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.1rn|  For an introduction type: /help introrn" (spawn_id exp8) match glob pattern "jshell>"? no
jshell>
expect: does "|  Welcome to JShell -- Version 9.0.1rn|  For an introduction type: /help intrornrnjshell> " (spawn_id exp8) match glob pattern "jshell>"? yes 
expect: set expect_out(0,string) "|  Welcome to JShell -- Version 9.0.1rn|  For an introduction type: /help intrornrnjshell> "
expect: set expect_out(spawn_id) "exp8"
expect: set expect_out(buffer) "|  Welcome to JShell -- Version 9.0.1rn|  For an introduction type: /help intrornrnjshell> "
send: sending "3r" 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

设法让它工作(在 Debian 9.3 上使用 jshell 9.0 和 Expect 5.45 进行测试):

[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 "3r"
expect_prompt
send "/exitn"
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(光标位置报告)(页面上的搜索CPR)。

  1. ^[[6n(^[==ESC==0x1b==u001b) 是心肺复苏术请求(由jshell发送)。
  2. ^[[32;1R这样的字符串(第 32 行,第 1 列)是当前光标位置(由终端驱动程序生成并由jshell读回)。

相关内容

  • 没有找到相关文章

最新更新