Terminal 我可以在Unix expect中运行jshell吗?

Terminal 我可以在Unix expect中运行jshell吗?,terminal,expect,tty,pty,jshell,Terminal,Expect,Tty,Pty,Jshell,我想使用expect重定向jshell输入,以便在录制的演示中模拟键入。但是,尽管我可以从expect脚本生成jshell进程,该脚本也可以识别jshell提示符,但在这之后,一切都不起作用。expect输出类似于控制序列的内容,如^[[24;9R,我看不到jshell的任何输出。不同的终端类型产生不同的字符序列,但它们都不起作用。这种行为在Ubuntu上的expect和Mac OS上是一致的。任何关于如何调查这个问题的建议都是受欢迎的。expect-d没有帮助 这是我想要模拟的jshell会话

我想使用expect重定向jshell输入,以便在录制的演示中模拟键入。但是,尽管我可以从expect脚本生成jshell进程,该脚本也可以识别jshell提示符,但在这之后,一切都不起作用。expect输出类似于控制序列的内容,如
^[[24;9R
,我看不到jshell的任何输出。不同的终端类型产生不同的字符序列,但它们都不起作用。这种行为在Ubuntu上的expect和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>
当我运行这个脚本时(在MacOS10.11.6上,但在Ubuntu上得到非常相似的结果),我看到了这个输出

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

jshell> ^[[24;9R
然后expect超时,最后一行输出将被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.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在Debian 9.3上测试,预期为5.45):

[步骤103]#cat jshell.exp
proc expect_prompt{}{
upvar spawn\u id spawn\u id
expect-ex“jshell>”
#CPR(光标位置报告)代码
expect-ex“\x1b\[6n”
#阅读CPR结果并将其发送至应用程序
期望_tty-re{\x1b\[[0-9]+;[0-9]+R}
发送$expect\u(0,字符串)
}
stty raw;#让tty完全控制jshell,因为它太疯狂了
繁殖jshell
期待你的提示
发送“3\r”
期待你的提示
发送“/退出\n”
预期eof
[步骤104]#期待jshell.exp
繁殖jshell
|欢迎使用JShell——9.0.1版
|对于介绍类型:/help intro
jshell>3
$1 ==> 3
jshell>/exit
|再见
[步骤105]#
神奇之处在于(在页面上搜索
CPR

  • ^[[6n
    ^[/code>==ESC=
    0x1b
    =
    \u001b
    )是CPR请求(由
    jshell
    发送)
  • ^[[32;1R
    (第32行,第1列)这样的字符串是当前光标位置(由终端驱动程序生成,由
    jshell
    读回)