1

当前目录中有大约 10 个容器映像文件,我想将它们加载到使用 containerd 作为 CRI 的 Kubernetes 集群中。

[root@test tmp]# ls -1
test1.tar
test2.tar
test3.tar
...

我尝试使用 xargs 一次加载它们,但得到以下结果:

[root@test tmp]# ls -1 | xargs nerdctl load -i
unpacking image1:1.0 (sha256:...)...done
[root@test tmp]#

第一个 tar 文件已成功加载,但命令退出,剩余的 tar 文件未处理。

我已确认命令nerdctl load -i成功,退出代码为 0。

[root@test tmp]# nerdctl load -i test1.tar
unpacking image1:1.0 (sha256:...)...done
[root@test tmp]# echo $?
0

有人知道原因吗?

4

1 回答 1

1

您通过管道传输的实际ls命令xargs被视为单个参数,其中文件名由空字节分隔(简而言之......请参阅本文以进行更好的深入分析)。如果您的版本xargs支持它,您可以使用该-0选项来考虑这一点:

ls -1 | xargs -0 nerdctl load -i

同时,这并不安全,您应该明白为什么在 shell中循环输出不是一个好主意ls

我宁愿将上面的内容转换为以下命令:

for f in *.tar; do
  nerdctl load -i "$f"
done
于 2022-02-01T07:59:25.973 回答