簡體   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