本文介绍了我可以在Unix中运行jshell吗?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

I'd like to redirect jshell input using expect, so that I can simulate typing in recorded demonstrations. But although I can spawn a jshell process from an expect script, which can also recognise the jshell prompt, after that nothing works. expect outputs what looks like a control sequence, like ^[[24;9R, and I don't see any output from jshell. Different terminal types produce different character sequences, but none of them work. This behaviour is consistent between expect on Ubuntu and Mac OS. Any suggestions for how to investigate this problem would be welcome. expect -d doesn't help.

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

Here's a transcript of the jshell session I want to simulate

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

jshell> 3
$1 ==> 3

jshell>

这是我认为应该执行的脚本:

and here's the script that I think should do it:

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

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

When I run that script (on Mac OS 10.11.6, but I get very similar results on Ubuntu), I see this output

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

jshell> ^[[24;9R

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

Then expect times out, and the last line of output is overwritten by the shell prompt (so it looks as though at timeout more control characters are being written).

在脚本的第1行中将-d添加到期望的标志中,结果如下:

Adding -d to the flags for expect in line 1 of the script results in this output:

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

推荐答案

设法使其正常运行(已在使用jshell 9.0和Expect 5.45的Debian 9.3上进行了测试):

Managed to make it work (tested on Debian 9.3 with jshell 9.0 and 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 "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(光标位置报告)(搜索CPR在页面上.)

The magic is about CPR (cursor position report) (search CPR on the page).

  1. ^[[6n(^[ == == 0x1b == \u001b)是CPR请求(由jshell发送).
  2. ^[[32;1R这样的字符串(第32行,第1列)是当前光标位置(由终端驱动程序生成并由jshell读取).
  1. The ^[[6n (^[ == == 0x1b == \u001b) is the CPR request (sent by jshell).
  2. Strings like ^[[32;1R (row 32, column 1) is the current cursor position (generated by terminal driver and read back by jshell).

这篇关于我可以在Unix中运行jshell吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

06-22 09:32