java - 将 kotlin 程序编译为采用 args[0] 并将其打印在控制台中的 java jar 后,如何在 bash 中转义 * 乘法符号?

标签 java bash kotlin escaping

我的 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

我尝试应用 this answer 中的建议但也失败了

我的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: "*"

关于java - 将 kotlin 程序编译为采用 args[0] 并将其打印在控制台中的 java jar 后,如何在 bash 中转义 * 乘法符号?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/59660809/

相关文章:

java - 哪个工具可以收集和显示应用程序跟踪和异常?

java - Java中的不合格名称

bash - 有没有办法通过 Shell 脚本、AppleScript 或 Automator 工作流程触发热键/键盘快捷键?

bash - 须藤: command not found while using plink

android - java.lang.IllegalArgumentException : Illegal character (U+0) in writing xml file android 异常

菜单上的java Action 监听器,而不是菜单项上的

java - 如何通过JPA中的注释在一个实体中与另一个实体类型的最新实体建立关系?

bash - 如何在单个命令行中运行包含多个语句的 shell 脚本?

Android Java 项目在 Android Studio 中转换为 Kotlin

android - BigDecimal比较的正确方法是什么