sudo su -c ls 输出末尾的额外“:”,仅当使用 globbing 时

Extra ":" at the end of output from sudo su -c ls, only when globbing is used

在 android 设备上使用 adb shell 到 运行 命令,当 运行ning ls 使用或不使用通配符 (通配,即 * ).

当运行ning ls 不带通配符时,最后的路径会正确显示。当 运行ning ls 带有通配符时,出于某种原因,路径末尾显示为 :。实际文件的路径中没有 :

我的问题特别针对最后一个文件:/data/data/com.kauf.wrapmyFaceFunphotoeditor/files/DV-com.com.kauf.wrapmyFaceFunphotoeditor-2020-05-17-17-44-30-DEBUG.txt:

最后有一个 : 不应该在那里

为什么在 ls 中使用通配符会向结果路径添加字符?

编辑,环境详情:Windows10 / Android7,代码是运行ning on sh。我已经 运行 adb shell 进入此命令提示符,并在一行中完成(即 adb shell su -c ls ...) returns 类似的结果,与 adb shell command ... 相同;也澄清了问题。

Why you shouldn't parse the output of ls 中所述,ls 的行为并不总是明确定义的。使用 NUL(如果您对文件名没有任何控制权或知识)或换行符(如果您有理由确定文件名不能包含它们)直接分隔 [= 发出的值列表通常更安全34=]。那么考虑一下:

# output is separated by NULs, which cannot possibly exist in filenames
printf '%s[=10=]' /data/data/com.kauf.wrapmyfacefunphotoeditor/files/DV-*

...或...

# output is separated by newlines; beware of a file named DV-evil<newline>something-else
printf '%s\n' /data/data/com.kauf.wrapmyfacefunphotoeditor/files/DV-*

请注意,如果您要通过额外的非转义层传递它,则可能需要将反斜杠加倍——如果您在文件中看到 0s 或 ns 分隔文件名输出,这是相同的证据。


另请注意,如果 不存在 匹配文件,则 glob 将扩展为自身,因此您可以获得仅包含文字字符串 /data/data/com.kauf.wrapmyfacefunphotoeditor/files/DV-* 的输出;在 bash 中,这可以用 shopt -s nullglob 来抑制,但是在 /bin/sh 中(特别是最小的 busybox 版本更有可能在 Android 上可用)这可能不可用。解决此问题的一种方法是使用类似于以下的代码:

# set list of files into , , etc
set -- /data/data/com.kauf.wrapmyfacefunphotoeditor/files/DV-*
# exit immediately if  does not exist
if [ "$#" -le 1 ] && [ ! -e "" ]; then
  exit
fi
# otherwise, print the list in our desired format
printf '%s[=12=]' "$@"