繁体   English   中英

如何从Shell或Windows CommandLine以内联方式执行Java jshell命令

[英]How to execute java jshell command as inline from shell or windows commandLine

有什么方法可以在REPL( jshelljshell Java命令作为内联命令执行而无需启动它?

例如,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文件不是可行的解决方案。

同一帖子上的另一个解决方案是: echo "1+2"|jshell

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

哎呀输出

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

我期望$temp仅打印000019

默认情况下,在与JShell交互时使用normal反馈模式,该模式将打印命令输出,声明,命令和提示。 阅读jshell反馈模式简介以获取更多详细信息。

获取命令输出的步骤:

  1. concise反馈模式下运行jshell可以跳过“命令”和“声明”详细信息,它仍会显示提示和值。

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

     echo 'String.format("%06d", 19)' | jshell --feedback concise | sed -n '2p' 
  3. 仅提取值并排除其他详细信息-

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

如果使用System.out.println,则不需要第二个sed

echo "System.out.println(\"$JAVA_HOME\");" | jshell --feedback concise | sed -n '2p'

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM