Как экранировать * умножить символ в bash после компиляции программы kotlin в java jar, который принимает args [0] и печатает его в консоли? - PullRequest
4 голосов
/ 09 января 2020

Моя 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
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

1 Ответ

0 голосов
/ 09 января 2020

Не решение, а средство для определения источника проблемы.

Позволяет создать hello.sh в Bash, чтобы сравнить, как он обрабатывает массив аргументов.

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

Теперь создайте test.sh для сравнения вывода того же самого приветствия в Kotlin и Bash:

#!/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: "*"
Добро пожаловать на сайт PullRequest, где вы можете задавать вопросы и получать ответы от других членов сообщества.
...