簡體   English   中英

將 kotlin 程序編譯為采用 args[0] 並在控制台中打印的 java jar 后,如何在 bash 中轉義 * 乘法符號?

[英]How to escape * multiply symbol in bash after compiling a kotlin program to java jar that takes args[0] and prints it in the console?

我的 bash 版本

$ bash --version
GNU bash, version 4.4.23(1)-release (x86_64-pc-msys)

我的java版本

$ java --version
openjdk 13.0.1 2019-10-15
OpenJDK Runtime Environment (build 13.0.1+9)
OpenJDK 64-Bit Server VM (build 13.0.1+9, mixed mode, sharing)

我的 kotlinc 版本

$ kotlinc -version
OpenJDK 64-Bit Server VM warning: Options -Xverify:none and -noverify were deprecated in JDK 13 and will likely be removed in a future release.
info: kotlinc-jvm 1.3.50 (JRE 13.0.1+9)

我試過的

$ echo *
chap_six.kt clean.sh hello.jar hello.kt README.md start.sh
$ echo \*
*

我的簡單打印程序

// hello.kt
fun main(args: Array<String>) {
    println(args[0])
}

編譯我的簡單打印程序后

kotlinc hello.kt -include-runtime -d hello.jar

我希望在運行 java .jar 時在控制台中看到 * 符號

這是我得到的

$ java -jar hello.jar *
chap_six.kt
$ java -jar hello.jar \*
.git
$ java -jar hello.jar "*"
.git
$ java -jar hello.jar "\*"
\$Recycle.Bin

我嘗試應用此答案中的建議,但也失敗了

我的hello.sh

#!/bin/bash
FOO="*"
set +f
GLOBIGNORE=*
java -jar hello.jar $FOO

和嘗試

$ ./hello.sh
.git

我的hello2.sh也失敗了

#!/bin/bash
FOO="*"
set -f
java -jar hello.jar $FOO

和嘗試

$ ./hello2.sh 
.git

我的商店看起來像這樣

$ shopt
autocd          off
cdable_vars     off
cdspell         off
checkhash       off
checkjobs       off
checkwinsize    off
cmdhist         on
compat31        off
compat32        off
compat40        off
compat41        off
compat42        off
compat43        off
completion_strip_exe    off
complete_fullquote      on
direxpand       off
dirspell        off
dotglob         off
execfail        off
expand_aliases  on
extdebug        off
extglob         off
extquote        on
failglob        off
force_fignore   on
globasciiranges off
globstar        off
gnu_errfmt      off
histappend      off
histreedit      off
histverify      off
hostcomplete    on
huponexit       off
inherit_errexit off
interactive_comments    on
lastpipe        off
lithist         off
login_shell     off
mailwarn        off
no_empty_cmd_completion off
nocaseglob      off
nocasematch     off
nullglob        off
progcomp        on
promptvars      on
restricted_shell        off
shift_verbose   off
sourcepath      on
xpg_echo        off

我的 set -o 看起來像這樣

$ set -o
allexport       off
braceexpand     on 
emacs           on
errexit         off
errtrace        off
functrace       off
hashall         on
histexpand      on
history         on
igncr           off
ignoreeof       off
interactive-comments    on
keyword         off
monitor         on
noclobber       off
noexec          off
noglob          off
nolog           off
notify          off
nounset         off
onecmd          off
physical        off
pipefail        off
posix           off
privileged      off
verbose         off
vi              off
xtrace          off

不是解決方案,而是界定問題根源的手段。

讓我們在 Bash 中創建hello.sh來比較它如何處理參數數組。

#!/usr/bin/env bash
# hello.sh
echo "$1"

現在創建一個test.sh來比較 Kotlin 和 Bash 中相同 hello 的輸出:

#!/usr/bin/env bash

for arg in '*' '\*' "'*'" '"*"'; do
  kotlinout="$(java -jar hello.jar $arg)"
  bashout="$(bash hello.sh $arg)"
  if [ "$kotlinout" = "$bashout" ]; then
    comp='are same';
  else
    comp='DIFFERS !!'
  fi
  printf 'Argument: %s\tKotink and Bash outputs %s\n' "$arg" "$comp";
  printf 'Kotlin output: %s\n' "$kotlinout";
  printf 'Bash output: %s\n' "$bashout";
done

這是它在我的環境中的作用:

$ bash test.sh 
Argument: * Kotink and Bash outputs are same
Kotlin output: chap_six.kt
Bash output: chap_six.kt
Argument: \*    Kotink and Bash outputs are same
Kotlin output: \*
Bash output: \*
Argument: '*'   Kotink and Bash outputs are same
Kotlin output: '*'
Bash output: '*'
Argument: "*"   Kotink and Bash outputs are same
Kotlin output: "*"
Bash output: "*"

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM