如何从shell或windows内联命令执行java jshell命令
问题描述:
是否有任何方法可以将REPL(jshell
)上的java命令作为内联命令执行而不启动它?如何从shell或windows内联命令执行java jshell命令
E.g. 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"
我发现类似的问题here,但它不是可行的解决方案,以创建一个单独的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
。
答
默认情况下,normal
反馈模式用于与JShell
的交互,此模式打印命令输出,声明,命令和提示。阅读Introduction to jshell feedback modes了解更多详情。
步骤获得命令的输出:jshell
在concise
反馈模式跳过命令和声明的细节
-
运行,它仍然会打印提示和值。从结果使用
sed
$ echo 'String.format("%06d", 19)' | jshell --feedback concise jshell> String.format("%06d", 19) $1 ==> "000019"
-
过滤器第2行 -
echo 'String.format("%06d", 19)' | jshell --feedback concise | sed -n '2p'
-
只提取价值和排除其他详情 -
echo 'String.format("%06d", 19)' | jshell --feedback concise | sed -n '2p' |sed -En 's/[^>]*>(.+)/\1/gp'
的可能的复制[如何运行JShell文件?](https://stackoverflow.com/questions/46426526/how-to-run-a-jshell-file) – nullpointer
感谢您的建议。我检查了建议的帖子,为单个命令创建单独的文件是不可行的。人们可以在不同的地方执行多少个命令。 –
[回复之一](https://stackoverflow.com/a/46426620/1746118)在同一篇文章中建议使用'echo',但不需要脚本。你尝试过吗? *命令在不同的地方执行。*不同的地方在这里意味着什么? – nullpointer