我有 bash 命令:
adb shell '/bin/sh -c "CLASSPATH=\$(pm path androidx.test.services) app_process / androidx.test.services.shellexecutor.ShellMain am instrument -w --no-window-animation -e clearPackageData false -e annotation '$annotationsString' -e targetInstrumentation ru.hh.hhh.test/com.kaspersky.kaspresso.runner.KaspressoRunner androidx.test.orchestrator/.AndroidTestOrchestrator"'
当annotationsString是我得到的字符串时是这样的:
IFS=','
annotationsString=''
read -ra annotations <<< "$MARATHON_ALLOW_ANNOTATIONS"
pos=$((${#annotations[*]} - 1))
last=${annotations[$pos]}
for annotation in "${annotations[@]}";
do
annotationsString="${annotationsString}com.salute.tests.utils.${annotation}"
if [[ $annotation != $last ]]
then
annotationsString="${annotationsString} "
fi
done
annotationsString=$(echo ${annotationsString} | tr ' ' ',')
And this string equals: com.hh.tests.utils.ONE_ANNOTATION,com.hh.tests.utils.TWO_ANNOTATION but on output i have error: adb shell '/bin/sh -c "CLASSPATH=\$(pm path androidx.test.services) app_process / androidx.test.services.shellexecutor.ShellMain am instrument -w --no-window-animation -e clearPackageData false -e annotation com.hh.tests.utils.ONE_ANNOTATION' 'com.hh.tests.utils.TWO_ANNOTATION -e targetInstrumentation ru.hh.hhh.test/com.kaspersky.kaspresso.runner.KaspressoRunner androidx.test.orchestrator/.AndroidTestOrchestrator"'
可以看到,没有逗号,但是出现了' '
如何在 bash 命令中输入带逗号的注释字符串?
IFS=',' adb shell 'blabla'$annotationsString'blabla'
您将
IFS
设置为逗号,并且 $annotationsString
不带引号,因此该命令在逗号上拆分为 two 逗号上的单词。
如何在 bash 命令中输入带逗号的注释字符串?
您应该引用扩展内容
adb shell 'blabla'"$annotationsString"'blabla'
。您应该使用 shellcheck 检查您的脚本,它将检测到此类错误。