如何从 shell 或 windows 命令行以内联方式执行 java jshell 命令

Rah*_*rma 7 java java-9 jshell

有没有办法在 REPL ( jshell) 上执行 java 命令作为内联命令而不启动它?

例如 Perl 内联命令

$perl -e 'printf("%06d", 19)'
000019
Run Code Online (Sandbox Code Playgroud)

我必须启动 jshell 才能运行任何命令:

$jshell
|  Welcome to JShell -- Version 9
|  For an introduction type: /help intro
jshell> String.format("%06d", 19)
$1 ==> "000019"
Run Code Online (Sandbox Code Playgroud)

我在这里发现了类似的问题,但为单个命令创建单独的文件并不是可行的解决方案jsh

同一篇文章的另一个解决方案是:echo "1+2"|jshell

temp=`echo 'String.format("%06d", 19)'|jshell`
echo $temp
Run Code Online (Sandbox Code Playgroud)

哎哟输出

| Welcome to JShell -- Version 9 | For an introduction type: /help intro jshell> String.format("%06d", 19) $1 ==> "000019" jshell>
Run Code Online (Sandbox Code Playgroud)

$temp只期待打印000019

Rah*_*rma 1

默认情况下,normal在交互中使用反馈模式JShell,此模式打印命令输出、声明、命令和提示。有关更多详细信息,请阅读jshell 反馈模式简介。

获取命令输出的步骤:

  1. jshell在反馈模式下运行concise以跳过命令和声明详细信息,它仍然会打印提示和值。

     $ echo 'String.format("%06d", 19)' | jshell --feedback concise
       jshell> String.format("%06d", 19)
       $1 ==> "000019"
    
    Run Code Online (Sandbox Code Playgroud)
  2. sed使用-从结果中过滤第二行

    echo 'String.format("%06d", 19)' | jshell --feedback concise | sed -n '2p'
    
    Run Code Online (Sandbox Code Playgroud)
  3. 仅提取值并排除其他细节 -

    echo 'String.format("%06d", 19)' | jshell --feedback concise | sed -n '2p' |sed -En 's/[^>]*>(.+)/\1/gp'
    
    Run Code Online (Sandbox Code Playgroud)