如何从shell或windows命令行执行java jshell命令作为内联 [英] How to execute java jshell command as inline from shell or windows commandLine

查看:169
本文介绍了如何从shell或windows命令行执行java jshell命令作为内联的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

Are there any way to execute java command on REPL (jshell) as inline command without launching it?

例如。 Perl内联命令

$perl -e 'printf("%06d", 19)'
000019

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

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

我发现了类似的问题此处,但为单个命令创建单独的 jsh 文件并不可行。

I found similar question here, but it's not feasible solution to create a separate jsh file for individual command.

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

Another solution on same post is: echo "1+2"|jshell

temp=`echo 'String.format("%06d", 19)'|jshell`
echo $temp

Ouch输出

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

我期待 $ temp 只打印 000019

推荐答案

默认正常反馈模式用于与 JShell 的交互,此模式打印命令输出,声明,命令和提示。阅读简介有关更多详细信息,请参阅jshell反馈模式

By default the normal feedback mode is used in your interaction with JShell and this mode prints command output, Declaration, Commands and Prompt. Read Introduction to jshell feedback modes for more details.

获取命令输出的步骤:


  1. 运行 jshell 简明反馈模式下跳过命令和声明细节,它仍会打印提示和值。

  1. Run jshell in concise feedback mode to skip Command and Declaration details, it will still print prompt and value.

 $ echo 'String.format("%06d", 19)' | jshell --feedback concise
   jshell> String.format("%06d", 19)
   $1 ==> "000019"


  • 使用 sed从结果中过滤第二行 -

    echo 'String.format("%06d", 19)' | jshell --feedback concise | sed -n '2p'
    


  • 仅提取值并排除其他详细信息 -

  • Extract only value and exclude other details-

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


  • 这篇关于如何从shell或windows命令行执行java jshell命令作为内联的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

    查看全文
    登录 关闭
    扫码关注1秒登录
    发送“验证码”获取 | 15天全站免登陆